在钉钉群中添加Webhook自定义机器人,
复制Webhook地址保存:
https://oapi.dingtalk.com/robot/send?access_token=xxxxxx
安全设置:选择加签方式,复制加签保存
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parsetimestamp = 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)headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx×tamp=' + timestamp + "&sign=" + signdef msg(text):json_text= {"msgtype": "text","at": {"atMobiles": ["135xxxxxxx"],"isAtAll": False},"text": {"content": text}}print (requests.post(api_url,json.dumps(json_text),headers=headers).content)if __name__ == '__main__':text = "我就是我, 是不一样的烟火"msg(text)