一固额、打開釘釘群眠蚂,添加自定義機器人,記住創(chuàng)建機器人的webhook即可斗躏。
二逝慧、發(fā)送消息到釘釘群:
import json
import requests
# 步驟一的webhook
webhook = ""
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "link",
"link": {
"title": 標題,
"text": 文本內容,
"picUrl": 圖片鏈接,
"messageUrl": 文章鏈接
},
"at": {
"isAtAll": True
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json,headers=header)
# 消息是否發(fā)送成功查看
print(json.loads(info.text))
發(fā)送的消息類型參見釘釘文檔:
https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
三、定時發(fā)送消息到釘釘群
from apscheduler.schedulers.background import BlockingScheduler
import requests
import json
webhook = ""
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
def sendInfo():
message = {
"msgtype": "text",
"text": {
# 消息內容一定要包含釘釘群添加機器人時候的關鍵詞啄糙,否則無法發(fā)送
"content": "發(fā)送一條消息"
},
"at": {
"isAtAll": True
}
}
message_json = json.dumps(message, ensure_ascii=False)
info = requests.post(url=webhook, data=message_json, headers=header)
print('-'*20)
print(info.text)
if __name__ == "__main__":
scheduler = BlockingScheduler()
"""
sendInfo:發(fā)送消息函數(shù)
cron:定時發(fā)送
hour:發(fā)送時間
id:定時任務ID
"""
scheduler.add_job(sendInfo, 'cron', hour=10, id='operate')
scheduler.start()