Python第三方包之DingDingBot釘釘機(jī)器人
這個(gè)是作者自己封裝的一個(gè)釘釘機(jī)器人的包,目前只支持發(fā)文本格式、鏈接格式、markdown格式的消息,我們可以在很多場(chǎng)景用到這個(gè),比如告警通知等
安裝
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:測(cè)試數(shù)據(jù)')) # 發(fā)送鏈接消息 print(dd.Send_Link_Msg(Content='test',Title='測(cè)試數(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="# 測(cè)試數(shù)據(jù)\n" + "> testone", Title='測(cè)試數(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ī)號(hào)
: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)題
# 一級(jí)標(biāo)題
## 二級(jí)標(biāo)題
### 三級(jí)標(biāo)題
#### 四級(jí)標(biāo)題
##### 五級(jí)標(biāo)題
###### 六級(jí)標(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: 這個(gè)Markdown的標(biāo)題
:param atMobiles: @指定的人,這里必須是列表,且參數(shù)為手機(jī)號(hào)
: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
到此這篇關(guān)于Python第三方包之DingDingBot釘釘機(jī)器人的文章就介紹到這了,更多相關(guān)Python DingDingBot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python創(chuàng)建文本文件的簡(jiǎn)單方法
在本篇內(nèi)容里小編給大家整理分享的是一篇關(guān)于python創(chuàng)建文本文件的簡(jiǎn)單方法,有需要的朋友們可以參考學(xué)習(xí)下。2020-08-08
解決pycharm:unused import statement錯(cuò)誤的問題
這篇文章主要介紹了解決pycharm:unused import statement錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Python 調(diào)用VC++的動(dòng)態(tài)鏈接庫(DLL)
Python下調(diào)用VC++的動(dòng)態(tài)鏈接庫的腳本2008-09-09
Python與AI分析時(shí)間序列數(shù)據(jù)
預(yù)測(cè)給定輸入序列中的下一個(gè)是機(jī)器學(xué)習(xí)中的另一個(gè)重要概念.本章為您提供有關(guān)分析時(shí)間序列數(shù)據(jù)的詳細(xì)說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-05-05
基于python tornado實(shí)現(xiàn)圖床功能
因?yàn)橘I了阿里/騰訊的云服務(wù)器,但是使用云存儲(chǔ)還需要收費(fèi),又加上家里正好有一臺(tái)nas,又加上閑的沒事,所以搞了一個(gè)小腳本,這個(gè)項(xiàng)目主要功能是為typora增加一個(gè)自定義圖床,本文給大家介紹基于python tornado實(shí)現(xiàn)圖床功能,感興趣的朋友一起看看吧2023-08-08
如何解決pycharm中用matplotlib畫圖不顯示中文的問題
這篇文章主要介紹了如何解決pycharm中用matplotlib畫圖不顯示中文的問題,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06

