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中getattr函数使用方法 getattr实现工厂模式
Jan 20 Python
高性能web服务器框架Tornado简单实现restful接口及开发实例
Jul 16 Python
Python 26进制计算实现方法
May 28 Python
【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
Nov 19 Python
python 显示数组全部元素的方法
Apr 19 Python
Python3.4 splinter(模拟填写表单)使用方法
Oct 13 Python
Python实现九宫格式的朋友圈功能内附“马云”朋友圈
May 07 Python
Django1.11配合uni-app发起微信支付的实现
Oct 12 Python
在keras里实现自定义上采样层
Jun 28 Python
pip install命令安装扩展库整理
Mar 02 Python
python urllib库的使用详解
Apr 13 Python
Python如何利用正则表达式爬取网页信息及图片
Apr 17 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
[EPIC] Larva vs Flash ZvT @ Crossing Field [2017-10-09]
2020/03/17 星际争霸
ThinkPHP之getField详解
2014/06/20 PHP
destoon在360浏览器下出现用户被强行注销的解决方法
2014/06/26 PHP
Laravel使用Caching缓存数据减轻数据库查询压力的方法
2016/03/15 PHP
CI框架常用函数封装实例
2016/11/21 PHP
thinkPHP5.0框架安装教程
2017/03/25 PHP
Add a Table to a Word Document
2007/06/15 Javascript
Jquery 选中表格一列并对表格排序实现原理
2012/12/15 Javascript
jQuery中:lt选择器用法实例
2014/12/29 Javascript
js生成随机数的过程解析
2015/11/24 Javascript
JS基于ocanvas插件实现的简单画板效果代码(附demo源码下载)
2016/04/05 Javascript
vue.js+boostrap项目实践(案例详解)
2016/09/21 Javascript
bootstrap制作jsp页面(根据值让table显示选中)
2017/01/05 Javascript
Bootstrap modal 多弹窗之叠加显示不出弹窗问题的解决方案
2017/02/23 Javascript
vue.js项目打包上线的图文教程
2017/11/16 Javascript
Vue打包部署到Nginx时,css样式不生效的解决方式
2020/08/03 Javascript
Python multiprocessing.Manager介绍和实例(进程间共享数据)
2014/11/21 Python
深入浅析python with语句简介
2018/04/11 Python
flask框架中勾子函数的使用详解
2018/08/01 Python
TensorFlow实现保存训练模型为pd文件并恢复
2020/02/06 Python
Python实现Wordcloud生成词云图的示例
2020/03/30 Python
HTML5制作3D爱心动画教程 献给女友浪漫的礼物
2014/11/05 HTML / CSS
html5中canvas图表实现柱状图的示例
2017/11/13 HTML / CSS
Under Armour西班牙官网:美国知名的高端功能性运动品牌
2018/12/12 全球购物
Travelstart沙特阿拉伯:廉价航班、豪华酒店和实惠的汽车租赁优惠
2019/04/06 全球购物
Pureology官网:为染色头发打造最好的产品
2019/09/13 全球购物
Derek Rose官网:英国高档睡衣、家居服和内衣品牌
2020/01/18 全球购物
西部世纪面试题
2014/12/05 面试题
工商管理专业应届生求职信
2013/11/04 职场文书
岗位竞聘演讲稿
2014/01/10 职场文书
社区学习雷锋活动总结
2014/04/25 职场文书
2015年预备党员自我评价
2015/03/04 职场文书
女方家长婚礼致辞
2015/07/27 职场文书
观看安全警示教育片心得体会
2016/01/15 职场文书
Log4j.properties配置及其使用
2021/08/02 Java/Android
vue使用element-ui按需引入
2022/05/20 Vue.js