机器人编程python代码_进修python运用自定义钉钉机器人的示例代码_后端开发

2020081317113879013.jpg

1.增加自定义机器人

18c7ffa46ee2a9c6ab3e61a186f2af6c-0.png

036ff095ab2895f373804be2e7b4dc28-1.png

相干进修引荐:python视频教程

2.编写python代码要求钉钉机器人所给的webhook

钉钉自定义机器人官方文档

平安体式格局运用加签的体式格局:

第一步,把timestamp+"\n"+密钥当作署名字符串,运用HmacSHA256算法盘算署名,然后举行Base64 encode,末了再把署名参数再举行urlEncode,获得终究的署名(须要运用UTF-8字符集)。

参数

申明

timestamp

当前时候戳,单元是毫秒,与要求挪用时候误差不能超过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中。

参数

申明

timestamp

第一步运用到的时候戳

sign

第一步获得的署名值

第三步,发送要求

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)

效果:

dafcf12614b3e8f5a61743dfd4dcc5cc-2.png

团体代码:

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运用自定义钉钉机器人的示例代码的细致内容,更多请关注ki4网别的相干文章!

收藏 | 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值