import json
import os
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import schedule
# 这里替换为复制的完整 webhook 地址
prefix = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxx11c38db9a589ace3b4ef28f3e63b5d485c7b1035b802xxxxxxxxxx"
# 这里替换为自己复制过来的加签秘钥
secret = "SECxxxxxxxxxx63c82a93c780a1038b4b7c2c448f17461078d3ed2b75xxxxxxxxxx"
def sendDingTk(msg):
headers = {"Content-Type": "application/json", "Charset": "UTF-8"}
timestamp = str(round(time.time() * 1000))
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 = f"{prefix}×tamp={timestamp}&sign={sign}"
# 钉钉消息格式,其中 msg 就是我们要发送的具体内容
data = {"at": {"isAtAll": True}, "text": {"content": msg}, "msgtype": "text"}
return requests.post(url=url, data=json.dumps(data), headers=headers).text
def sendWeeklyMeeting():
weekly_meeting_msg = ("会议主题:[周会] 数据开发部 \n"
"会议时间:10:30-11:00 \n\n"
"点击链接入会,或添加至会议列表: \n"
"https://meeting.tencent.com/dm/0H4H1xxxxxxx \n\n"
"腾讯会议:411-8888-8888\n")
sendDingTk(weekly_meeting_msg)
if __name__ == "__main__":
# 每周一上午10:20发送周会提醒
schedule.every().monday.at("10:25").do(sendWeeklyMeeting)
while True:
schedule.run_pending()
time.sleep(1)
后台执行:
python3 weekly_meeting.py &
会打印出进程ID,最好记录下,以便后面不用的时候停掉。