淺談Python 釘釘報警必備知識系統講解
本章所講內容:
1、釘釘報警設置
2、釘釘報警腳本運行。
1、釘釘報警設置
釘釘,關于webhook的報警需求,釘釘報警也是我們在公司中常見的報警系統,在這里主要是結合zabbix二次開發使用,來達到完美報警的使用。
1.1、釘釘報警第一步,創建群機器人
接口地址:
https://oapi.dingtalk.com/robot/send?access_token=a25324cafc5b0f2bb239b5e56c71e7f378f570a3d281160dbec9e4f8c4a7e493
文檔地址:
https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
1.2 發送信信息
1.2.1 發送@所有人的消息
1、發送普通的消息
import requestsimport jsonurl = ’https://oapi.dingtalk.com/robot/send?access_token=a25324cafc5b0f2bb239b5e56c71e7f378f570a3d281160dbec9e4f8c4a7e493 ’headers = { 'Content-Type': 'application/json', 'Chartset': 'utf-8'}#要發送的文本是json格式request_data = { #此消息的類型為固定的text 'msgtype': 'text', 'text': { #消息的內容 'content': '大家新年快樂' }, '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)
第二步進行接口開發
2、修改結構,具體操作
import sysimport jsonimport requestsurl = ’https://oapi.dingtalk.com/robot/send?access_token=a25324cafc5b0f2bb239b5e56c71e7f378f570a3d281160dbec9e4f8c4a7e493 ’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 = sys.argv[1] WriteLogByDing(content)
1.2.2 發送帶有鏈接的文檔
import requestsimport jsonurl = ’https://oapi.dingtalk.com/robot/send?access_token=a25324cafc5b0f2bb239b5e56c71e7f378f570a3d281160dbec9e4f8c4a7e493 ’headers = { 'Content-Type': 'application/json', 'Chartset': 'utf-8'}#要發送的文本是json格式request_data = { #發送鏈接類型的數據 'msgtype': 'link', 'link': { #鏈接提示 'text':'群機器人是釘釘群的高級擴展功能。群機器人可以將第三方服務的信息聚合到群聊中,實現自動化的信息同步。例如:通過聚合GitHub,GitLab等源碼管理服務,實現源碼更新同步;通過聚合Trello,JIRA等項目協調服務,實現項目信息同步。不僅如此,群機器人支持Webhook協議的自定義接入,支持更多可能性,例如:你可將運維報警提醒通過自定義機器人聚合到釘釘群。', #鏈接標題 'title': '自定義機器人協議', #圖片url地址 'picUrl': 'http://p3.so.qhmsg.com/sdr/200_200_/t013d7a21145c708288.jpg', #信息的鏈接跳轉 'messageUrl': 'https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Rqyvqo&treeId=257&articleId=105735&docType=1' }}#把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 發送makedown文檔
import requestsimport jsonurl = ’https://oapi.dingtalk.com/robot/send?access_token=a25324cafc5b0f2bb239b5e56c71e7f378f570a3d281160dbec9e4f8c4a7e493 ’headers = { 'Content-Type': 'application/json', 'Chartset': 'utf-8'}#要發送的文本是json格式request_data = { 'msgtype': 'markdown', 'markdown': {'title':'杭州天氣', 'text':'#### 杭州天氣 n > 9度, 西北風1級,空氣良89,相對溫度73%nn > n > ###### 10點20分發布 [天氣](http://www.thinkpage.cn/) ' }, '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)
#要發送的文本是json格式發送整體跳轉的actionCard類型request_data = { 'actionCard': { 'title': '喬布斯 20 年前想打造一間蘋果咖啡廳,而它正是 Apple Store 的前身', 'text': ' n #### 喬布斯 20 年前想打造的蘋果咖啡廳 nn Apple Store 的設計正從原來滿滿的科技感走向生活化,而其生活化的走向其實可以追溯到 20 年前蘋果一個建立咖啡館的計劃', 'hideAvatar': '0', 'btnOrientation': '0', 'singleTitle' : '閱讀全文', 'singleURL' : 'https://www.dingtalk.com/' }, 'msgtype': 'actionCard'}
{ 'feedCard': { 'links': [ {'title': '時代的火車向前開','messageURL': 'https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI','picURL': 'https://www.dingtalk.com/' }, {'title': '時代的火車向前開2','messageURL': 'https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI','picURL': 'https://www.dingtalk.com/' } ] }, 'msgtype': 'feedCard'}
總結:
1、釘釘報警設置
2、釘釘報警腳本運行。
到此這篇關于淺談Python 釘釘報警必備知識系統講解的文章就介紹到這了,更多相關Python 釘釘報警內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
