小編給大家分享一下Python第三方包之DingDingBot釘釘機(jī)器人,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

這個是作者自己封裝的一個釘釘機(jī)器人的包,目前只支持發(fā)文本格式、鏈接格式、markdown格式的消息,我們可以在很多場景用到這個,比如告警通知等
安裝
pip install DingDingBot
使用方法
from DingDingBot.DDBOT import DingDing # 初始話DingDingBOt webhook是釘釘機(jī)器人所必須的 dd = DingDing(webhook='https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx') # 發(fā)送文本消息 print(dd.Send_Text_Msg(Content='test:測試數(shù)據(jù)')) # 發(fā)送鏈接消息 print(dd.Send_Link_Msg(Content='test',Title='測試數(shù)據(jù)',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')) # 發(fā)送Markdown格式的消息 print(dd.Send_MardDown_Msg(Content="# 測試數(shù)據(jù)\n" + "> testone", Title='測試數(shù)據(jù)'))
源碼
#!/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: 要發(fā)送的內(nèi)容
:param atMobiles: @指定的人,這里必須是列表,且參數(shù)為手機(jī)號
: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: 鏈接的內(nèi)容
:param title: 鏈接的標(biāo)題
:param MsgUrl: 待跳轉(zhuǎn)頁面的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格式的文本,僅支持下面的格式
'''
標(biāo)題
# 一級標(biāo)題
## 二級標(biāo)題
### 三級標(biāo)題
#### 四級標(biāo)題
##### 五級標(biāo)題
###### 六級標(biāo)題
引用
> 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的標(biāo)題
:param atMobiles: @指定的人,這里必須是列表,且參數(shù)為手機(jī)號
: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以上是“Python第三方包之DingDingBot釘釘機(jī)器人”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:Python第三方包之DingDingBot釘釘機(jī)器人-創(chuàng)新互聯(lián)
鏈接分享:http://chinadenli.net/article32/pojsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、微信公眾號、用戶體驗、企業(yè)網(wǎng)站制作、域名注冊、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)