如果你希望通過
釘釘
接收信息通知(注意不是釘釘郵件合住,所有郵件操作已經(jīng)在上一小節(jié)講過)
在釘釘上設(shè)置一個(gè)群機(jī)器人
帘饶,釘釘個(gè)人權(quán)限
所致哑诊。
如果為企業(yè)權(quán)限請(qǐng)?jiān)敿?xì)參考 https://developers.dingtalk.com/document
- 添加一個(gè)
自定義
機(jī)器人并指定群組
- 得到一個(gè)
Webhook
地址 - 出于安全考慮,添加
簽名
及刻,即secret
根據(jù) https://developers.dingtalk.com/document/app/custom-robot-access?spm=ding_open_doc.document.0.0.6d9d28e1ji2ImR#topic-2026027 使用
Python
實(shí)現(xiàn)一個(gè)機(jī)器人推送功能
文本
推送
# -*- coding: utf-8 -*-
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse
def dingMessage():
timestamp = str(round(time.time() * 1000))
secret = 'SEC21022...cf06a65a761c86eb4027b'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
# 請(qǐng)求的URL镀裤,WebHook地址
webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4...7bc0ec08354cc181a×tamp={timestamp}&sign={sign}"
# 構(gòu)建請(qǐng)求頭部
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
# 構(gòu)建請(qǐng)求數(shù)據(jù)
tex = "呼叫0號(hào)呼叫0號(hào)"
message = {
"msgtype": "text",
"text": {
"content": f" @137....97091 {tex}"
},
# todo 是否@所有人
"at": {
"atMobiles": [
"137...7091"
],
"isAtAll": False
}
}
# 對(duì)請(qǐng)求的數(shù)據(jù)進(jìn)行json封裝
message_json = json.dumps(message)
# 發(fā)送請(qǐng)求
info = requests.post(url=webhook, data=message_json, headers=header)
# 打印返回的結(jié)果
print(info.text)
if __name__ == "__main__":
dingMessage()
做了什么
-
secret
機(jī)器人加簽 -
webhook
機(jī)器人API
地址,參數(shù)組成:timestamp
(時(shí)間戳)sign
(sha256
加密后base64
編碼生成的簽名) -
tex
文本信息 -
message
主體對(duì)象缴饭,選擇@
相關(guān)的人(這里根據(jù)手機(jī)號(hào)碼)
markdown
推送
# -*- coding: utf-8 -*-
import json
import requests
def dingmessage():
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'SEC21022b79....9cf06a65a761c86eb4027b'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
# 請(qǐng)求的URL暑劝,WebHook地址
webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfc....4800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}"
# 構(gòu)建請(qǐng)求頭部
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "markdown",
# "text": {
# "content": f" @137...97091 {tex}"
# },
"markdown": {
"title": "杭州天氣",
"text": "#### 杭州天氣 @155...773 \n> 900度,西北風(fēng)1級(jí)颗搂,空氣良8009担猛,相對(duì)溫度673%\n> [![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)](https://developers.dingtalk.com/document/app/send-normal-messages)\n> ###### 10點(diǎn)20分發(fā)布 [天氣](https://www.dingtalk.com) \n"
},
# todo 是否@所有人
"at": {
"atMobiles": [
"15....773"
],
"isAtAll": False
}
}
# 對(duì)請(qǐng)求的數(shù)據(jù)進(jìn)行json封裝
message_json = json.dumps(message)
# 發(fā)送請(qǐng)求
info = requests.post(url=webhook, data=message_json, headers=header)
# 打印返回的結(jié)果
print(info.text)
if __name__ == "__main__":
dingmessage()
網(wǎng)頁
鏈接
# -*- coding: utf-8 -*-
import json
import requests
def dingmessage():
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'SEC21022b792577....8639cf06a65a761c86eb4027b'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
# 請(qǐng)求的URL,WebHook地址
webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4....800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "link",
"link": {
"text": "這個(gè)即將發(fā)布的新版本,創(chuàng)始人xx稱它為紅樹林傅联。而在此之前先改,每當(dāng)面臨重大升級(jí),產(chǎn)品經(jīng)理們都會(huì)取一個(gè)應(yīng)景的代號(hào)蒸走,這一次仇奶,為什么是紅樹林",
"title": "時(shí)代的火車向前開",
"picUrl": "",
"messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
},
# todo 是否@所有人
"at": {
"atMobiles": [
"15....8773"
],
"isAtAll": False
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header)
print(info.text)
if __name__ == "__main__":
dingmessage()
卡片
推送
整體跳轉(zhuǎn)ActionCard
類型
# -*- coding: utf-8 -*-
import json
import requests
def dingmessage():
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'SEC21022b7925....064668639cf06a65a761c86eb4027b'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
# 請(qǐng)求的URL,WebHook地址
webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb.....4800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "actionCard",
"actionCard": {
"text": "![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n\n #### 喬布斯 20 年前想打造的蘋果咖啡廳 \n\n Apple Store 的設(shè)計(jì)正從原來滿滿的科技感走向生活化载碌,而其生活化的走向其實(shí)可以追溯到 20 年前蘋果一個(gè)建立咖啡館的計(jì)劃",
"title": "時(shí)代的火車向前開",
"btnOrientation": "0",
"singleTitle" : "閱讀全文",
"singleURL" : "https://www.dingtalk.com/"
},
# todo 是否@所有人
"at": {
"atMobiles": [
"155....8773"
],
"isAtAll": False
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header)
print(info.text)
if __name__ == "__main__":
dingmessage()
獨(dú)立跳轉(zhuǎn)ActionCard
類型
# -*- coding: utf-8 -*-
import json
import requests
def dingmessage():
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'SEC21022b792.....39cf06a65a761c86eb4027b'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
# 請(qǐng)求的URL猜嘱,WebHook地址
webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4.....0ec08354cc181a×tamp={timestamp}&sign={sign}"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "actionCard",
"actionCard": {
"text": "![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n\n #### 喬布斯 20 年前想打造的蘋果咖啡廳 \n\n Apple Store 的設(shè)計(jì)正從原來滿滿的科技感走向生活化衅枫,而其生活化的走向其實(shí)可以追溯到 20 年前蘋果一個(gè)建立咖啡館的計(jì)劃",
"title": "時(shí)代的火車向前開",
"hideAvatar": "0",
"btnOrientation": "1",
"btns": [
{
"title": "內(nèi)容不錯(cuò)",
"actionURL": "https://www.dingtalk.com/"
},
{
"title": "不感興趣",
"actionURL": "https://www.dingtalk.com/"
}
]
},
# todo 是否@所有人
"at": {
"atMobiles": [
"155....8773"
],
"isAtAll": False
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header)
print(info.text)
if __name__ == "__main__":
dingmessage()
以上就是釘釘
群機(jī)器人
通知的幾種類型嫁艇。
總而言之釘釘機(jī)器人推送是一種非常不錯(cuò)的選擇,足以應(yīng)對(duì)日常工作中的各類場(chǎng)景 (
定時(shí)提醒
弦撩、閾值告警
步咪、事件通知
等等)
本篇文章由一文多發(fā)平臺(tái)ArtiPub自動(dòng)發(fā)布