python
import requests
import json
from config import *
def sendDing():
"""
给钉钉发送消息,使用markdown格式在发送的消息后,需要手动@一次
"""
text = msg + ' '.join([f"@{i}" for i in at_list])
data = {
"msgtype": "markdown",
"markdown": {
"title": title,
"text": text
},
"at": {
"atMobiles": at_list, "isAtAll": isAtAll
}
}
headers = {'Content-Type': 'application/json;charset=UTF-8'}
send_data = json.dumps(data).encode('utf-8')
result = requests.post(url=token, data=send_data, headers=headers)
# print(result.json())
# 判断发送是否成功
if result.json()['errcode'] == 0:
print('发送成功')
else:
print('发送失败')
if __name__ == '__main__':
sendDing()
python
token = "https://oapi.dingtalk.com/robot/send?access_token=" \
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # 钉钉机器人的token
title = "发送的主题名称"
# @群成员的手机号
at_list = ['181xxxxxxxx', '181xxxxxxxx']
# 是否@所有人
isAtAll = False
# 消息内容
msg = "消息内容\n\n"
执行 send.py
脚本
shell
python3 send.py