国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

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

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

1.添加自定義機器人

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

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

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

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

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

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

參數

說明

timestamp

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

secret

密鑰,機器人安全設置頁面,加簽一欄下面顯示的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中。

參數

說明

timestamp

第一步使用到的時間戳

sign

第一步得到的簽名值

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

第三步,發送請求

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.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)

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

標簽: 釘釘 Python
相關文章:
主站蜘蛛池模板: 彭山县| 肥城市| 阳城县| 瑞安市| 宜州市| 博白县| 阿坝| 桓仁| 清远市| 安泽县| 綦江县| 高阳县| 额尔古纳市| 晋州市| 池州市| 阿拉善盟| 洛阳市| 依安县| 三河市| 宣化县| 南川市| 蛟河市| 班玛县| 临夏市| 闸北区| 锡林郭勒盟| 清丰县| 平原县| 广德县| 盈江县| 彰化市| 柞水县| 邵武市| 思南县| 清河县| 桃园市| 闽清县| 定襄县| 辛集市| 南京市| 泸溪县|