-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
66 lines (52 loc) · 1.66 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import math
import json
import random
import requests
import datetime
# 获取天气和温度
def get_weather():
url = "http://autodev.openspeech.cn/csp/api/v2.1/weather?openId=aiuicus&clientType=android&sign=android&city=" + city
res = requests.get(url).json()
weather = res['data']['list'][0]
return weather['weather'], math.floor(weather['temp'])
# 每日一句
def get_words():
words = requests.get("https://api.shadiao.pro/chp")
if words.status_code != 200:
return get_words()
return words.json()['data']['text']
# 字体随机颜色
def get_random_color():
return "#%06x" % random.randint(0, 0xFFFFFF)
def send_msg(token_dd, msg, at_all=False):
"""
通过钉钉机器人发送内容
@param date_str:
@param msg:
@param at_all:
@return:
"""
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + token_dd
headers = {'Content-Type': 'application/json;charset=utf-8'}
content_str = "早上好!\n\n{0}\n".format(msg)
data = {
"msgtype": "text",
"text": {
"content": content_str
},
"at": {
"isAtAll": at_all
},
}
res = requests.post(url, data=json.dumps(data), headers=headers)
print(res.text)
return res.text
if __name__ == '__main__':
city = os.environ['CITY']
token_dd = os.environ['TOKEN_DD']
# city = "北京"
# token_dd = '你自己的webhook后面的access_token复制在此'
wea, temperature = get_weather()
note_str = "当前城市:{0}\n今日天气:{1}\n当前温度:{2}\n{3}".format(city, wea, temperature, get_words())
send_msg(token_dd, note_str, True)