钉钉报警设置
创建群机器人
接口地址
发送短消息
发送普通消息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import requests import json url = 'https://oapi.dingtalk.com/robot/send?access_token=71638980426c030' headers = { "Content-Type" : "application/json" , "Chartset" : "utf-8" } # 要发送的文本是json格式 request_data = { # 此消息的类型为固定的text "msgtype" : "text" , "text" : { # 消息的内容 "content" : "大家新年快乐test" }, "at" : { # 被@人的手机号 "atMobiles" : [], # 控制@所有人 "isAtAll" : True } } # 把json转变为字符串格式数据 send_data = json.dumps(request_data) # 这个是发送post请求,请求钉钉接口 response = requests.post(url = url, headers = headers, data = send_data) # 讲求成功后返回的数据 content = response.content.decode() # 打印 # 课程 vip 标准 # 替换 视频 print (content) |
接口开发
修改结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import requests import json import sys url = 'https://oapi.dingtalk.com/robot/send?access_token=71389c030' def WriteLogByDing(content): headers = { "Content-Type" : "application/json" , "Chartset" : "utf-8" } request_data = { "msgtype" : "text" , "text" : { "content" : content }, "at" : { "atMobiles" : [], "isAtAll" : True } } sendData = json.dumps(request_data) response = requests.post(url = url, headers = headers, data = sendData) content = response.content.decode() print (content) if __name__ = = "__main__" : content = input ( '请输入想要的信息: ' ) content + = "test" # content = sys.argv[1] WriteLogByDing(content) |
发送带链接的文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import requests import json url = 'https://oapi.dingtalk.com/robot/send?access_token=7164d45fa912dc12ed721522371ecf7428ad912740e87a63c3bf38980426c030' headers = { "Content-Type" : "application/json" , "Chartset" : "utf-8" } # 要发送的文本是json格式 request_data = { # 发送链接类型的数据 "msgtype" : "link" , "link" : { # 链接提示 "text" : "群机器人是钉钉群的高级扩展功能。群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。例如:通过聚合GitHub,GitLab等源码管理服务,实现源码更新同步;通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。不仅如此,群机器人支持Webhook协议的自定义接入,支持更多可能性,例如:你可将运维报警提醒通过自定义机器人聚合到钉钉群。" , # 链接标题 "title" : "自定义机器人协议test" , # 图片url地址 "picUrl" : "http://p3.so.qhmsg.com/sdr/200_200_/t013d7a21145c708288.jpg" , # 信息的链接跳转 "messageUrl" : "http://www.zcj.net.cn" } } # 把json转变为字符串格式数据 send_data = json.dumps(request_data) # 这个是发送post请求,请求钉钉接口 response = requests.post(url = url, headers = headers, data = send_data) # 讲求成功后返回的数据 content = response.content.decode() # 打印 # 课程 vip 标准 # 替换 视频 print (content) |
发送makedown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import requests import json url = 'https://oapi.dingtalk.com/robot/send?access_token=7164d45fa912dc12ed721522371ecf7428ad912740e87a63c3bf38980426c030' headers = { "Content-Type" : "application/json" , "Chartset" : "utf-8" } # 要发送的文本是json格式 request_data = { "msgtype" : "markdown" , "markdown" : { "title" : "杭州天气" , "text" : "#### 杭州天气 \n > 9度, 西北风1级,空气良89,相对温度73%\n\n > ![screenshot](http://i01.lw.aliimg.com/media/lALPBbCc1ZhJGIvNAkzNBLA_1200_588.png)\n > ###### 10点20分发布 [天气](http://www.thinkpage.cn/) test" }, "at" : { "atMobiles" : [], "isAtAll" : False } } # 把json转变为字符串格式数据 send_data = json.dumps(request_data) # 这个是发送post请求,请求钉钉接口 response = requests.post(url = url, headers = headers, data = send_data) # 讲求成功后返回的数据 content = response.content.decode() # 打印 print (content) |
zabbix集成钉钉报警
钉钉报警python脚本
https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
http://www.zzvips.com/article/94957.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/env python #coding:utf-8 #zabbix钉钉报警 import requests,json,sys,os,datetime webhook = "https://oapi.dingtalk.com/robot/send?access_token=ce0d39251" user = sys.argv[ 1 ] text = sys.argv[ 3 ] data = { "msgtype" : "text" , "text" : { "content" : "test" }, "at" : { "atMobiles" : [ # user ], "isAtAll" : False } } headers = { 'Content-Type' : 'application/json' } x = requests.post(url = webhook,data = json.dumps(data),headers = headers) if os.path.exists( "/usr/local/zabbix_server/logs/dingding.log" ): f = open ( "/usr/local/zabbix_server/logs/dingding.log" , "a+" ) else : f = open ( "/usr/local/zabbix_server/logs/dingding.log" , "w+" ) f.write( "\n" + "--" * 30 ) if x.json()[ "errcode" ] = = 0 : f.write( "\n" + str (datetime.datetime.now()) + " " + str (user) + " " + "发送成功" + "\n" + str (text)) f.close() else : f.write( "\n" + str (datetime.datetime.now()) + " " + str (user) + " " + "发送失败" + "\n" + str (text)) f.write( str (x) + str (x.json())) f.close() |
到此这篇关于Python钉钉报警及Zabbix集成钉钉报警的示例代码的文章就介绍到这了,更多相关Python钉钉报警 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/you-men/p/13514532.html