环境:Python3.8
运行库:requests,json,hmac,urllib,time,base64,hashlib,sys
将下面的代码保存为DingTalk_Alarm.py
调用方式:python DingTalk_Alarm.py 发送的消息 True
# !/usr/bin/env python3 # -*- coding: utf-8 -*- # 添加所需的运行库 import requests,json,hmac,urllib,time,base64,hashlib,sys # 加签 def Signature_Url(webhook_url,secret): # 生成当前时间戳,单位是毫秒,与请求调用时间误差不能超过1小时 timestamp = str(round(time.time() * 1000)) # 修改编码格式为utf-8 secret_enc = secret.encode('utf-8') # 将timestamp和secret合并 string_to_sign = '{}\n{}'.format(timestamp, secret) # 修改编码格式为utf-8 string_to_sign_enc = string_to_sign.encode('utf-8') # 将字段进行加密,加密类型采用sha256 hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) # 生成请求的URL,WebHook地址 Post_Url = ("%s×tamp=%s&sign=%s" % (webhook_url, timestamp, sign)) return Post_Url # 发送消息 def DingMessage(Post_Url,Content,People): # 构建请求头部 header = { "Content-Type": "application/json", "Charset": "UTF-8" } # 构建请求数据 message ={ "msgtype": "text", "text": { "content": Content }, "at": { # "atMobiles":[ # "13510236914", # ], "isAtAll": People } } # 对请求的数据进行json封装、定义头部文件并发送请求 info = requests.post(url=Post_Url,data=json.dumps(message),headers=header) # 判断是否发送成功 if json.loads(info.text)['errmsg'] == "ok": print("发送成功!") else: print(json.loads(info.text)) if __name__=="__main__": # 群机器人webhook链接 webhook_url = "https://oapi.dingtalk.com/robot/send?" access_token = "****************************************************************" # 密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串 secret = '*******************************************************************' # 需要发送的消息 Content = sys.argv[1] # 是否@所有人,是输入True否则False People = sys.argv[2] # 调用DingMessage填入需要传递的参数发送消息 # 格式:python DingTalk_Alarm.py 发送的消息 True DingMessage(Signature_Url(webhook_url,secret),Content,People)
帅斌 says:
啊啊啊啊,爱了爱了。