通過釘釘?shù)拈_放API接口,可以很容易的將消息發(fā)送到釘釘dingtalk,比起郵件發(fā)送更穩(wěn)定,及時
文檔
- 官網(wǎng):https://www.dingtalk.com/
- API Explorer調(diào)試 https://open-dev.dingtalk.com/apiExplorer
方式一:webhook方式
文檔:https://open.dingtalk.com/document/robots/custom-robot-access
使用場景:發(fā)送消息到聊天群
前期準備:需要新建一個群聊,在群聊中添加機器人
# -*- coding: utf-8 -*-
"""
@File : demo.py
@Date : 2023-06-22
"""
import requests
url = 'https://oapi.dingtalk.com/robot/send?access_token=xxx'
data = {
"msgtype": "text",
"text": {
"content": "監(jiān)控報警: 服務(wù)異常"
}
}
res = requests.post(url, json=data)
方式二:發(fā)送工作通知
文檔:https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages
使用場景:發(fā)送通知類的消息
前期準備:需要準備以下參數(shù)
- 釘釘開放平臺 創(chuàng)建企業(yè)內(nèi)部應(yīng)用/釘釘應(yīng)用,獲取應(yīng)用憑證(AgentId、AppKey、AppSecret)
- 釘釘管理后臺 通訊錄/成員管理,獲取員工的UserID
示例代碼文章來源:http://www.zghlxwxcb.cn/news/detail-505974.html
# -*- coding: utf-8 -*-
"""
@File : dingtalk_api.py
@Date : 2023-03-08
"""
import requests
def get_access_token(appkey, appsecret):
"""
獲取access_token
https://open.dingtalk.com/document/orgapp/obtain-orgapp-token
:param appkey: 應(yīng)用的唯一標識key
:param appsecret: 應(yīng)用的密鑰
:return:
{
"errcode": 0,
"access_token": "96fc7a7axxx",
"errmsg": "ok",
"expires_in": 7200
}
"""
url = 'https://oapi.dingtalk.com/gettoken'
params = {
'appkey': appkey,
'appsecret': appsecret
}
res = requests.get(url, params=params)
return res.json()
def send_message(access_token, body):
"""
發(fā)送應(yīng)用消息
https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages
:param access_token:
:param body: 消息體
:return:
{
"errcode":0,
"task_id":256271667526,
"request_id":"4jzllmte0wau"
}
"""
url = 'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2'
params = {
'access_token': access_token,
}
res = requests.post(url, params=params, json=body)
return res.json()
if __name__ == '__main__':
# 應(yīng)用的唯一標識key
appkey = ''
# 應(yīng)用的密鑰
appsecret = ''
# 發(fā)送消息時使用的微應(yīng)用的AgentID
agent_id = ''
# 接收者的userid列表
userid_list = ''
token = get_access_token(appkey, appsecret)
ret = send_message(token['access_token'], {
"agent_id": agent_id,
"userid_list": userid_list,
"msg": {
"msgtype": "text",
"text": {
"content": "你好,釘釘"
},
},
})
文章來源地址http://www.zghlxwxcb.cn/news/detail-505974.html
到了這里,關(guān)于Python:使用釘釘dingtalk發(fā)送通知消息的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!