Python釘釘報警及Zabbix集成釘釘報警的示例代碼
釘釘報警設(shè)置
創(chuàng)建群機器人


接口地址

發(fā)送短消息
發(fā)送普通消息
import requests
import json
url = 'https://oapi.dingtalk.com/robot/send?access_token=71638980426c030'
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
# 要發(fā)送的文本是json格式
request_data = {
# 此消息的類型為固定的text
"msgtype": "text",
"text": {
# 消息的內(nèi)容
"content": "大家新年快樂test"
},
"at": {
# 被@人的手機號
"atMobiles": [],
# 控制@所有人
"isAtAll": True
}
}
# 把json轉(zhuǎn)變?yōu)樽址袷綌?shù)據(jù)
send_data = json.dumps(request_data)
# 這個是發(fā)送post請求,請求釘釘接口
response = requests.post(url=url, headers=headers, data=send_data)
# 講求成功后返回的數(shù)據(jù)
content = response.content.decode()
# 打印
# 課程 vip 標(biāo)準(zhǔn)
# 替換 視頻
print(content)

接口開發(fā)
修改結(jié)構(gòu)
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)
發(fā)送帶鏈接的文檔
import requests
import json
url = 'https://oapi.dingtalk.com/robot/send?access_token=7164d45fa912dc12ed721522371ecf7428ad912740e87a63c3bf38980426c030'
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
# 要發(fā)送的文本是json格式
request_data = {
# 發(fā)送鏈接類型的數(shù)據(jù)
"msgtype": "link",
"link": {
# 鏈接提示
"text": "群機器人是釘釘群的高級擴展功能。群機器人可以將第三方服務(wù)的信息聚合到群聊中,實現(xiàn)自動化的信息同步。例如:通過聚合GitHub,GitLab等源碼管理服務(wù),實現(xiàn)源碼更新同步;通過聚合Trello,JIRA等項目協(xié)調(diào)服務(wù),實現(xiàn)項目信息同步。不僅如此,群機器人支持Webhook協(xié)議的自定義接入,支持更多可能性,例如:你可將運維報警提醒通過自定義機器人聚合到釘釘群。",
# 鏈接標(biāo)題
"title": "自定義機器人協(xié)議test",
# 圖片url地址
"picUrl": "http://p3.so.qhmsg.com/sdr/200_200_/t013d7a21145c708288.jpg",
# 信息的鏈接跳轉(zhuǎn)
"messageUrl": "http://www.zcj.net.cn"
}
}
# 把json轉(zhuǎn)變?yōu)樽址袷綌?shù)據(jù)
send_data = json.dumps(request_data)
# 這個是發(fā)送post請求,請求釘釘接口
response = requests.post(url=url, headers=headers, data=send_data)
# 講求成功后返回的數(shù)據(jù)
content = response.content.decode()
# 打印
# 課程 vip 標(biāo)準(zhǔn)
# 替換 視頻
print(content)

發(fā)送makedown
import requests
import json
url = 'https://oapi.dingtalk.com/robot/send?access_token=7164d45fa912dc12ed721522371ecf7428ad912740e87a63c3bf38980426c030'
headers = {
"Content-Type": "application/json",
"Chartset": "utf-8"
}
# 要發(fā)送的文本是json格式
request_data = {
"msgtype": "markdown",
"markdown": {"title": "杭州天氣",
"text": "#### 杭州天氣 \n > 9度, 西北風(fēng)1級,空氣良89,相對溫度73%\n\n > \n > ###### 10點20分發(fā)布 [天氣](http://www.thinkpage.cn/) test"
},
"at": {
"atMobiles": [],
"isAtAll": False
}
}
# 把json轉(zhuǎn)變?yōu)樽址袷綌?shù)據(jù)
send_data = json.dumps(request_data)
# 這個是發(fā)送post請求,請求釘釘接口
response = requests.post(url=url, headers=headers, data=send_data)
# 講求成功后返回的數(shù)據(jù)
content = response.content.decode()
# 打印
print(content)

zabbix集成釘釘報警
釘釘報警python腳本
https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
http://m.fzitv.net/article/193470.htm
#!/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)+" "+"發(fā)送成功"+"\n"+str(text))
f.close()
else:
f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "發(fā)送失敗" + "\n" + str(text))
f.write(str(x)+str(x.json()))
f.close()
到此這篇關(guān)于Python釘釘報警及Zabbix集成釘釘報警的示例代碼的文章就介紹到這了,更多相關(guān)Python釘釘報警 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python遠(yuǎn)程桌面協(xié)議RDPY安裝使用介紹
這篇文章主要介紹了Python遠(yuǎn)程桌面協(xié)議RDPY安裝使用介紹,本文講解了RDPY的安裝、RDPY的簡單使用兩部份內(nèi)容,需要的朋友可以參考下2015-04-04
python中import與from方法總結(jié)(推薦)
這篇文章主要介紹了python中import與from方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Python+PIL實現(xiàn)批量在圖片上寫上自定義文本
Pillow 是一個 Python 的圖像處理庫,它是 Python Imaging Library (PIL) 的一個分支,并且增加了更多的功能,下面我們看看如何利用它實現(xiàn)批量在圖片上寫上自定義的文本吧2024-11-11
Python判斷和循環(huán)語句的分析與應(yīng)用
判斷語句是用來篩選條件,過濾條件的。循環(huán)語句是用來解決重復(fù)性代碼的問題,提高工作效率。今天的知識點不多,耐心看完吧2022-07-07

