久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

python使用自定義釘釘機器人的示例代碼

瀏覽:3日期:2022-07-19 16:22:03

1.添加自定義機器人

python使用自定義釘釘機器人的示例代碼

python使用自定義釘釘機器人的示例代碼

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

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

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

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

參數(shù)

說明

timestamp

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

secret

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

import requests #python 3.8import timeimport hmacimport hashlibimport base64import 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

第一步得到的簽名值

https://oapi.dingtalk.com/robot/send?access_token=XXXXXX&timestamp=XXX&sign=XXX

第三步,發(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)

結(jié)果:

python使用自定義釘釘機器人的示例代碼

整體代碼:

import requests #python 3.8import timeimport hmacimport hashlibimport base64import 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)

到此這篇關(guān)于python使用自定義釘釘機器人的示例代碼的文章就介紹到這了,更多相關(guān)python 自定義釘釘機器人內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: 釘釘 Python
相關(guān)文章:
主站蜘蛛池模板: 满洲里市| 盱眙县| 台安县| 沧州市| 金乡县| 东乡族自治县| 平远县| 东莞市| 张家界市| 隆回县| 和顺县| 延安市| 仁布县| 建始县| 奎屯市| 百色市| 繁峙县| 垦利县| 娄烦县| 新昌县| 綦江县| 西乌珠穆沁旗| 合山市| 台北县| 高平市| 融水| 阿拉善左旗| 唐河县| 留坝县| 沙坪坝区| 赫章县| 丁青县| 囊谦县| 美姑县| 博爱县| 张家口市| 广昌县| 虹口区| 彩票| 上林县| 灵丘县|