python使用自定义钉钉机器人

1.添加自定义机器人

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

结果:

 

整体代码:


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)

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要发送图片到钉钉机器人,可以使用钉钉机器人自定义机器人API接口。下面是一个发送图片的示例代码: ```python import requests url = 'https://oapi.dingtalk.com/robot/send?access_token=<your_access_token>' headers = { 'Content-Type': 'application/json;charset=utf-8' } data = { "msgtype": "image", "image": { "media_id": "<media_id>" } } response = requests.post(url, headers=headers, json=data) print(response.text) ``` 其中,`<your_access_token>` 是你的钉钉机器人的访问令牌,`<media_id>` 是你要发送的图片的media_id。可以使用钉钉提供的上传图片接口(https://oapi.dingtalk.com/media/upload?access_token=ACCESS_TOKEN&type=image)上传图片并获取media_id。 除了发送图片,还可以发送文本、链接、Markdown等不同类型的消息。下面是一个发送Markdown消息的示例代码: ```python import requests url = 'https://oapi.dingtalk.com/robot/send?access_token=<your_access_token>' headers = { 'Content-Type': 'application/json;charset=utf-8' } data = { "msgtype": "markdown", "markdown": { "title": "标题", "text": "## 标题二 \n\n **加粗文本** \n\n *斜体文本* \n\n [链接](http://www.baidu.com)" } } response = requests.post(url, headers=headers, json=data) print(response.text) ``` 其中,`title` 是消息标题,`text` 是Markdown格式的消息内容。 以上是发送图片和Markdown消息的示例代码,你可以根据自己的需求修改代码和消息类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值