python使用自定义钉钉机器人的示例代码


Posted in Python onJune 24, 2020

1.添加自定义机器人

python使用自定义钉钉机器人的示例代码

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 第一步得到的签名值

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

第三步,发送请求

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)

结果:

 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×tamp={}&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 相关文章推荐
Python进程通信之匿名管道实例讲解
Apr 11 Python
Python实现的文本简单可逆加密算法示例
May 18 Python
Python 文件操作的详解及实例
Sep 18 Python
Python如何生成树形图案
Jan 03 Python
python调用OpenCV实现人脸识别功能
May 25 Python
解决PyCharm不运行脚本,而是运行单元测试的问题
Jan 17 Python
解决Django中调用keras的模型出现的问题
Aug 07 Python
pycharm 安装JPype的教程
Aug 08 Python
python数字类型math库原理解析
Mar 02 Python
python使用建议与技巧分享(二)
Aug 17 Python
python给list排序的简单方法
Dec 10 Python
python绘制箱型图
Apr 27 Python
pytorch中的weight-initilzation用法
Jun 24 #Python
pytorch查看模型weight与grad方式
Jun 24 #Python
pytorch  网络参数 weight bias 初始化详解
Jun 24 #Python
可视化pytorch 模型中不同BN层的running mean曲线实例
Jun 24 #Python
python3.x中安装web.py步骤方法
Jun 23 #Python
python如何删除文件、目录
Jun 23 #Python
TensorFlow保存TensorBoard图像操作
Jun 23 #Python
You might like
Terran兵种介绍
2020/03/14 星际争霸
PHP 中英文混合排版中处理字符串常用的函数
2007/04/12 PHP
php5数字型字符串加解密代码
2008/04/24 PHP
PHP实现批量重命名某个文件夹下所有文件的方法
2017/09/04 PHP
php使用fullcalendar日历插件详解
2019/03/06 PHP
如何在Laravel之外使用illuminate组件详解
2020/09/20 PHP
javascript学习笔记(五) Array 数组类型介绍
2012/06/19 Javascript
js触发select onchange事件的小技巧
2014/08/05 Javascript
jQuery实现切换字体大小的方法
2015/03/10 Javascript
js获取及修改网页背景色和字体色的方法
2015/12/29 Javascript
手机端js和html5刮刮卡效果
2020/09/29 Javascript
原生js仿淘宝网商品放大镜效果
2017/02/28 Javascript
使用命令行工具npm新创建一个vue项目的方法
2017/12/27 Javascript
vue中渲染对象中属性时显示未定义的解决
2020/07/31 Javascript
[55:18]Liquid vs Chaos 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
使用Nginx+uWsgi实现Python的Django框架站点动静分离
2016/03/21 Python
python逆序打印各位数字的方法
2018/06/25 Python
numpy.std() 计算矩阵标准差的方法
2018/07/11 Python
使用Python和Prometheus跟踪天气的使用方法
2019/05/06 Python
python3.7 使用pymssql往sqlserver插入数据的方法
2019/07/08 Python
python 梯度法求解函数极值的实例
2019/07/10 Python
python getpass实现密文实例详解
2019/09/24 Python
Pytorch 实现focal_loss 多类别和二分类示例
2020/01/14 Python
python对接ihuyi实现短信验证码发送
2020/05/10 Python
使用sublime text3搭建Python编辑环境的实现
2021/01/12 Python
canvas绘制树形结构可视图形的实现
2020/04/03 HTML / CSS
戴尔美国官方折扣店:Dell Outlet
2018/02/13 全球购物
Tirendo比利时:在线购买轮胎
2018/10/22 全球购物
成教毕业生自我鉴定
2013/10/23 职场文书
《我的伯父鲁迅先生》教学反思
2014/02/12 职场文书
2014年高中教师工作总结
2014/12/19 职场文书
文员岗位职责
2015/02/04 职场文书
先进工作者个人总结
2015/02/15 职场文书
2016大一新生军训心得体会
2016/01/11 职场文书
导游词之山海关
2019/12/10 职场文书
Lombok的详细使用及优缺点总结
2021/07/15 Java/Android