溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python使用自定義釘釘機器人的案例

發(fā)布時間:2020-08-15 10:55:02 來源:億速云 閱讀:236 作者:小新 欄目:編程語言

這篇文章主要介紹了python使用自定義釘釘機器人的案例,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

1.添加自定義機器人

python使用自定義釘釘機器人的案例

python使用自定義釘釘機器人的案例

相關學習推薦:python視頻教程

2.編寫python代碼請求釘釘機器人所給的webhook

釘釘自定義機器人官方文檔

安全方式使用加簽的方式:

第一步,把timestamp+"\n"+密鑰當做簽名字符串,使用HmacSHA256算法計算簽名,然后進行Base64 encode,最后再把簽名參數(shù)再進行urlEncode,得到最終的簽名(需要使用UTF-8字符集)。

參數(shù)

說明

timestamp

當前時間戳,單位是毫秒,與請求調(diào)用時間誤差不能超過1小時

secret

密鑰,機器人安全設置頁面,加簽一欄下面顯示的SEC開頭的字符串

import requests
 
 
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
 
timestamp = str(round(time.time() * 1000))
secret = '加簽時生成的密鑰'
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

第一步使用到的時間戳

sign

第一步得到的簽名值


第三步,發(fā)送請求

url='生成的Webhook&timestamp={}&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)

結果:

python使用自定義釘釘機器人的案例

整體代碼:

import requests
 
 
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
 
timestamp = str(round(time.time() * 1000))
secret = '加簽時生成的密鑰'
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&timestamp={}&sign={}'.format(timestamp, sign)
 
 
print (url)
headers={
 'Content-Type':'application/json'
}
json={"msgtype": "text",
 "text": {
  "content": "測試"
 } }
resp=requests.post(url=url,headers=headers,json=json)
print (resp.text)

感謝你能夠認真閱讀完這篇文章,希望小編分享python使用自定義釘釘機器人的案例內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI