python 钉钉机器人
选择自定义机器人
# python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json
# 加签
timestamp = str(round(time.time() * 1000))
secret = '加签的SECXXX'
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))
token = '机器人Webhook:获取access_token==XXX'
def dingmessage():
# 请求的URL,WebHook地址
webhook = f"https://oapi.dingtalk.com/robot/send?access_token={token}×tamp={timestamp}&sign={sign}"
# 构建请求头部
header = {"Content-Type": "application/json", "Charset": "UTF-8"}
# 循环生成器并发送消息
content = 'haha'
message = {
"msgtype": "text",
"text": {"content": content},
"at": {
# @ 所有人
"isAtAll": True
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header, verify=False) # 打印返回的结果
# print(info.text)
if __name__ == "__main__":
dingmessage()
haha
错误汇总
// 消息内容中不包含任何关键词
{
"errcode":310000,
"errmsg":"keywords not in content"
}
// timestamp 无效
{
"errcode":310000,
"errmsg":"invalid timestamp"
}
// 签名不匹配
{
"errcode":310000,
"errmsg":"sign not match"
}
// IP地址不在白名单
{
"errcode":310000,
"errmsg":"ip X.X.X.X not in whitelist"
}
本文作者: 永生
本文链接: https://yys.zone/detail/?id=249
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
发表评论
评论列表 (0 条评论)
暂无评论,快来抢沙发吧!