Python調用釘釘機器人發送消息
在日常工作中,需要用到告警通知,而常用的手段有郵件、短信、企業微信、飛書及釘釘,此處結合公司使用的釘釘,就記錄整理Python調用釘釘機器人發送告警的信息。此處使用常用的兩種方式即可,一種發送文本信息,另一中發送markdown信息。
更多機器人使用方式可參考官網:https://developers.dingtalk.com/document/robots/custom-robot-access.
注意,在發送消息時,需要符合創建機器人的接收消息認證類型,比如認證方式為關鍵字,則需要在發送消息時包含此關鍵字;若認證方式為IP的方式,則發消息的客戶端IP需要需添加的一致等。
發送文本信息
import requests import json url = 'https://oapi.dingtalk.com/robot/send?access_token=5000e7622081417901bc4bd78e97260291d36ccec706266dd3080fb0e0c' headers = {'Content-Type': 'application/json;charset=utf-8'} data = { "msgtype": "text", "text": { "content": "通知:Just for a test..." }, "at": { "atMobiles": [ '150xxxxxxxx' ], "isAtAll": False } } r = requests.post(url=url,headers=headers,data=json.dumps(data)) print(r.json())
發送結果
發送markdown類型消息
import requests import json url = 'https://oapi.dingtalk.com/robot/send?access_token=5000e7622081417901bc4bd78e97260e76291d36ccec706266dd3080fb0e0c' headers = {'Content-Type': 'application/json;charset=utf-8'} data = { "msgtype": "markdown", "markdown": { "title":"武漢天氣", "text": "#### 武漢天氣 @150xxxxxxxx \n" "> 通知:9度,西北風1級,空氣良89,相對溫度73%\n" + "> \n" + "> ###### 10點20分發布 [天氣](https://www.dingtalk.com) \n" }, "at": { "atMobiles": [ "150xxxxxxxx" ], "isAtAll": False } } r = requests.post(url=url,headers=headers,data=json.dumps(data)) print(r.json())
發送結果