1、需求说明
需要在工作日向部门钉钉群发送特定信息,本次是每日待办任务更新,同时有明天地区的天气预报以及打工寄语
2、获取钉钉机器人
在项目群中获取钉钉机器人, 主要是获取Webhook值,通过Webhook值向机器人发送消息。我的钉钉机器人认证方式为加密认证,所以需要特定的进行处理,获取url地址。请根据自己需求变更secret及url信息。
def get_url():
timestamp = str(round(time.time() * 1000))
secret = '你的SEC证书'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = "钉钉机器人url地址×tamp={}&sign={}".format(timestamp,sign)
return url
三、获取本地明日天气预报
本人只获取了明天的天气预报,可获取未来5天天气预报,同时可以根据自己需要提醒明日是否需要带雨伞,不过本人并未添加。
#获取天气,可以根据data获取未来五日天气预报,本次只获取明日天气
def getWeather(name):
url = 'http://wthrcdn.etouch.cn/weather_mini'
response = requests.get(url, {
'city': name})
result = json.loads(response.content.decode())
data = result.get('data').get('yesterday')
data = result.get('data').get('forecast')
i=data[1]
temper = "气温范围:"+i.get('low').split(" ")[1] + "~" +i.get('high').split(" ")[1] +" "+i.get('type')+" "+i.get('fengli').replace('<![CDATA[', '').replace(']]>', &