Python第三方包之DingDingBot
這個是作者自己封裝的一個釘釘機器人的包,目前隻支援發文本格式、連結格式、markdown格式的消息,我們可以在很多場景用到這個,比如告警通知等
安裝
pip install DingDingBot
使用方法
from DingDingBot.DDBOT import DingDing
# 初始話DingDingBOt webhook是釘釘機器人所必須的
dd = DingDing(webhook='https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx')
# 發送文本消息
print(dd.Send_Text_Msg(Content='test:測試資料'))
# 發送連結消息
print(dd.Send_Link_Msg(Content='test',Title='測試資料',MsgUrl='https://www.baidu.com',PicUrl='https://cn.bing.com/images/search?q=outgoing%e6%9c%ba%e5%99%a8%e4%ba%ba&id=FEE700371845D9386738AAAA51DCC43DC54911AA&FORM=IQFRBA'))
# 發送Markdown格式的消息
print(dd.Send_MardDown_Msg(Content="# 測試資料\n" + "> testone", Title='測試資料'))
源碼
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@@@@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@@@@@@@@ @@
'''
import requests, json
class DingDing():
"""
# 釘釘官方文檔
Refer to official documentation: https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
"""
# 初始化
def __init__(self, webhook):
self.webhook = webhook
self.session = requests.session()
self.session.headers = {"Content-Type": "application/json;charset=utf-8"}
def Send_Text_Msg(self, Content: str, atMobiles: list = [], isAtAll: bool = False) -> dict:
"""
:param content: 要發送的内容
:param atMobiles: @指定的人,這裡必須是清單,且參數為手機号
:param isAtAll: @全體成員
:return:
"""
try:
data = {
"msgtype": "text",
"text": {
"content": Content
},
"at": {
"atMobiles": atMobiles,
"isAtAll": isAtAll
}
}
response = self.session.post(self.webhook, data=json.dumps(data))
if response.status_code == '200':
result = {"status": True, "message": "Message has been sent"}
return result
else:
return response.text
except Exception as error:
result = {"status": False, "message": f"Failed to send message,Error stack:{error}"}
return result
def Send_Link_Msg(self, Content: str, Title: str, MsgUrl: str, PicUrl: str = ''):
"""
:param Content: 連結的内容
:param title: 連結的标題
:param MsgUrl: 待跳轉頁面的url
:param PicUrl: 消息所展示的圖檔
:return:
"""
try:
data = {
"msgtype": "link",
"link": {
"text": Content,
"title": Title,
"picUrl": PicUrl,
"messageUrl": MsgUrl
}
}
response = self.session.post(self.webhook, data=json.dumps(data))
if response.status_code == '200':
result = {"status": True, "message": "Message has been sent"}
return result
else:
return response.text
except Exception as error:
result = {"status": False, "message": f"Failed to send message,Error stack:{error}"}
return result
def Send_MardDown_Msg(self, Content: str, Title: str, atMobiles: list = [], isAtAll: bool = False):
"""
:param Content: Markdown格式的文本,僅支援下面的格式
'''
标題
# 一級标題
## 二級标題
### 三級标題
#### 四級标題
##### 五級标題
###### 六級标題
引用
> A man who stands for nothing will fall for anything.
文字加粗、斜體
**bold**
*italic*
連結
[this is a link](http://name.com)
圖檔

無序清單
- item1
- item2
有序清單
1. item1
2. item2
'''
:param Title: 這個Markdown的标題
:param atMobiles: @指定的人,這裡必須是清單,且參數為手機号
:param isAtAll: @全體成員
:return:
"""
try:
data = {
"msgtype": "markdown",
"markdown": {
"title": Title,
"text": Content
},
"at": {
"atMobiles": atMobiles,
"isAtAll": isAtAll
}
}
response = self.session.post(self.webhook, data=json.dumps(data))
if response.status_code == '200':
result = {"status": True, "message": "Message has been sent"}
return result
else:
return response.text
except Exception as error:
result = {"status": False, "message": f"Failed to send message,Error stack:{error}"}
return result