1. 自定义机器人
注:记住关键词与地址
pip3 install requests
#全部人
In [1]: import requests
In [2]: content = {
...: "msgtype": "text",
...: "text": {
...: "content": "qf 切克闹!" # 这里必须包含之前定义关键字
...: },
...: "at": {
...: "isAtAll": True # 发送给群里的所有人
...: }
...: }
In [7]: headers = {"Content-Type": "application/json;charset=utf-8"}
...:
In [8]: url = "https://oapi.dingtalk.com/robot/send?access_token=d7d
...: 3dddd4a5bff8c85485ce321221c4467d3646d429ad8c7fa6367646e8f572
...: 2"
r = requests.post(url=url,headers=headers,json=content)
print(r.content.decode()) # 打印结果
#指定用户
import requests
content = {
"msgtype": "text",
"text": {
"content": "qf 切克闹!"
},
"at": {
"atMobiles": [
"131xxxxxx811", # 单独 @ 某个人,使用绑定的手机号
"137xxxxxxxxx" # 多个人用户英文逗号隔开
]
}
}
# 这个是 API 要求必须添加的
headers = {"Content-Type": "application/json;charset=utf-8"}
url = "https://oapi.dingtalk.com/robot/send?access_token=你自己的 token"
r = requests.post(url=url,headers=headers,json=content)
print(r.content.decode(encoding="utf-8")) # 指定字符编码
创建脚本:
import sys
import requests
if len(sys.argv) >= 2:
atter_msg = sys.argv[1]
else:
print("参数传递有误")
print("使用方法: {} ".format(sys.argv[0]) + "参数1 参数2 ...")
exit()
c = "zabbix {}!"
c = c.format(atter_msg)
headers = {"Content-Type": "application/json;charset=utf-8"}
url = 'https://oapi.dingtalk.com/robot/send?access_token=d7d3dddd4a5bff8c85485ce321221c4467d3646d429ad8c7fa6367646e8f5722'
content = {
"msgtype": "text",
"text": {
"content": c # 这里必须包含之前定义关键字
},
"at": {
# 发送给群里的所有人
"isAtAll": True
}
}
r = requests.post(url=url,headers=headers,json=content)
print("发送信息的结果:",r.content.decode())