python使用自定義釘釘機(jī)器人的示例代碼
1.添加自定義機(jī)器人


2.編寫python代碼請(qǐng)求釘釘機(jī)器人所給的webhook
釘釘自定義機(jī)器人官方文檔
安全方式使用加簽的方式:
第一步,把timestamp+"\n"+密鑰當(dāng)做簽名字符串,使用HmacSHA256算法計(jì)算簽名,然后進(jìn)行Base64 encode,最后再把簽名參數(shù)再進(jìn)行urlEncode,得到最終的簽名(需要使用UTF-8字符集)。
|
參數(shù) |
說明 |
|
timestamp |
當(dāng)前時(shí)間戳,單位是毫秒,與請(qǐng)求調(diào)用時(shí)間誤差不能超過1小時(shí) |
|
secret |
密鑰,機(jī)器人安全設(shè)置頁面,加簽一欄下面顯示的SEC開頭的字符串 |
import requests
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = '加簽時(shí)生成的密鑰'
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)
第二步,把 timestamp和第一步得到的簽名值拼接到URL中。
|
參數(shù) |
說明 |
|
timestamp |
第一步使用到的時(shí)間戳 |
|
sign |
第一步得到的簽名值 |
https://oapi.dingtalk.com/robot/send?access_token=XXXXXX×tamp=XXX&sign=XXX
第三步,發(fā)送請(qǐng)求
url='生成的Webhook×tamp={}&sign={}'.format(timestamp, sign)
print (url)
headers={
'Content-Type':'application/json'
}
json={"msgtype": "text",
"text": {
"content": "888"
} }
resp=requests.post(url=url,headers=headers,json=json)
print (resp.text)
結(jié)果:

整體代碼:
import requests
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = '加簽時(shí)生成的密鑰'
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)
url='生成的Webhook×tamp={}&sign={}'.format(timestamp, sign)
print (url)
headers={
'Content-Type':'application/json'
}
json={"msgtype": "text",
"text": {
"content": "測(cè)試"
} }
resp=requests.post(url=url,headers=headers,json=json)
print (resp.text)
到此這篇關(guān)于python使用自定義釘釘機(jī)器人的示例代碼的文章就介紹到這了,更多相關(guān)python 自定義釘釘機(jī)器人內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch中model.named_parameters()與model.parameters()解讀
這篇文章主要介紹了pytorch中model.named_parameters()與model.parameters()使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Numpy創(chuàng)建數(shù)組和隨機(jī)數(shù)組的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Numpy創(chuàng)建數(shù)組和隨機(jī)數(shù)組的方法小結(jié),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,具有一定的參考價(jià)值,需要的可以參考一下2023-11-11
關(guān)于jupyter lab安裝及導(dǎo)入tensorflow找不到模塊的問題
這篇文章主要介紹了關(guān)于jupyter lab安裝及導(dǎo)入tensorflow找不到模塊的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python操作PostgreSql數(shù)據(jù)庫的方法(基本的增刪改查)
這篇文章主要介紹了Python操作PostgreSql數(shù)據(jù)庫(基本的增刪改查),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Python中用pycurl監(jiān)控http響應(yīng)時(shí)間腳本分享
這篇文章主要介紹了Python中用pycurl監(jiān)控http響應(yīng)時(shí)間腳本分享,本文腳本實(shí)現(xiàn)監(jiān)控http相應(yīng)碼,響應(yīng)大小,建立連接時(shí)間,準(zhǔn)備傳輸時(shí)間,傳輸?shù)谝粋€(gè)字節(jié)時(shí)間,完成時(shí)間,需要的朋友可以參考下2015-02-02
解決mnist數(shù)據(jù)集下載的相關(guān)問題
這篇文章主要介紹了解決mnist數(shù)據(jù)集下載的相關(guān)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

