<Python启发式自动化>之钉钉推送
如果你心愿通过
钉钉
接管信息告诉(留神不是钉钉邮件,所有邮件操作曾经在上一大节讲过)
在钉钉上设置一个群机器人
,钉钉集体权限
所致。
如果为企业权限请具体参考 https://developers.dingtalk.com/document
- 增加一个
自定义
机器人并指定群组
- 失去一个
Webhook
地址 - 出于平安思考,增加
签名
,即secret
依据 https://developers.dingtalk.com/document/app/custom-robot-access?spm=ding_open_doc.document.0.0.6d9d28e1ji2ImR#topic-2026027 应用 Python
实现一个机器人推送性能
文本
推送
# -*- coding: utf-8 -*-import requestsimport jsonimport timeimport hmacimport hashlibimport base64import urllib.parsedef dingMessage(): timestamp = str(round(time.time() * 1000)) secret = 'SEC21022...cf06a65a761c86eb4027b' 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地址 webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4...7bc0ec08354cc181a×tamp={timestamp}&sign={sign}" # 构建申请头部 header = { "Content-Type": "application/json", "Charset": "UTF-8" } # 构建申请数据 tex = "呼叫0号呼叫0号" message = { "msgtype": "text", "text": { "content": f" @137....97091 {tex}" }, # todo 是否@所有人 "at": { "atMobiles": [ "137...7091" ], "isAtAll": False } } # 对申请的数据进行json封装 message_json = json.dumps(message) # 发送申请 info = requests.post(url=webhook, data=message_json, headers=header) # 打印返回的后果 print(info.text)if __name__ == "__main__": dingMessage()
做了什么
secret
机器人加签webhook
机器人API
地址,参数组成:timestamp
(工夫戳)sign
(sha256
加密后base64
编码生成的签名)tex
文本信息message
主体对象,抉择@
相干的人(这里依据手机号码)
markdown
推送
# -*- coding: utf-8 -*-import jsonimport requestsdef dingmessage(): import time import hmac import hashlib import base64 import urllib.parse timestamp = str(round(time.time() * 1000)) secret = 'SEC21022b79....9cf06a65a761c86eb4027b' 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地址 webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfc....4800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}" # 构建申请头部 header = { "Content-Type": "application/json", "Charset": "UTF-8" } message = { "msgtype": "markdown", # "text": { # "content": f" @137...97091 {tex}" # }, "markdown": { "title": "杭州天气", "text": "#### 杭州天气 @155...773 \n> 900度,西北风1级,空气良8009,绝对温度673%\n> [](https://developers.dingtalk.com/document/app/send-normal-messages)\n> ###### 10点20分公布 [天气](https://www.dingtalk.com) \n" }, # todo 是否@所有人 "at": { "atMobiles": [ "15....773" ], "isAtAll": False } } # 对申请的数据进行json封装 message_json = json.dumps(message) # 发送申请 info = requests.post(url=webhook, data=message_json, headers=header) # 打印返回的后果 print(info.text)if __name__ == "__main__": dingmessage()
网页
链接
# -*- coding: utf-8 -*-import jsonimport requestsdef dingmessage(): import time import hmac import hashlib import base64 import urllib.parse timestamp = str(round(time.time() * 1000)) secret = 'SEC21022b792577....8639cf06a65a761c86eb4027b' 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地址 webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4....800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}" header = { "Content-Type": "application/json", "Charset": "UTF-8" } message = { "msgtype": "link", "link": { "text": "这个行将公布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大降级,产品经理们都会取一个应景的代号,这一次,为什么是红树林", "title": "时代的火车向前开", "picUrl": "", "messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI" }, # todo 是否@所有人 "at": { "atMobiles": [ "15....8773" ], "isAtAll": False } } message_json = json.dumps(message) info = requests.post(url=webhook, data=message_json, headers=header) print(info.text)if __name__ == "__main__": dingmessage()
卡片
推送
整体跳转ActionCard
类型
# -*- coding: utf-8 -*-import jsonimport requestsdef dingmessage(): import time import hmac import hashlib import base64 import urllib.parse timestamp = str(round(time.time() * 1000)) secret = 'SEC21022b7925....064668639cf06a65a761c86eb4027b' 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地址 webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb.....4800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}" header = { "Content-Type": "application/json", "Charset": "UTF-8" } message = { "msgtype": "actionCard", "actionCard": { "text": " \n\n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实能够追溯到 20 年前苹果一个建设咖啡馆的打算", "title": "时代的火车向前开", "btnOrientation": "0", "singleTitle" : "浏览全文", "singleURL" : "https://www.dingtalk.com/" }, # todo 是否@所有人 "at": { "atMobiles": [ "155....8773" ], "isAtAll": False } } message_json = json.dumps(message) info = requests.post(url=webhook, data=message_json, headers=header) print(info.text)if __name__ == "__main__": dingmessage()
独立跳转ActionCard
类型
# -*- coding: utf-8 -*-import jsonimport requestsdef dingmessage(): import time import hmac import hashlib import base64 import urllib.parse timestamp = str(round(time.time() * 1000)) secret = 'SEC21022b792.....39cf06a65a761c86eb4027b' 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地址 webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4.....0ec08354cc181a×tamp={timestamp}&sign={sign}" header = { "Content-Type": "application/json", "Charset": "UTF-8" } message = { "msgtype": "actionCard", "actionCard": { "text": " \n\n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实能够追溯到 20 年前苹果一个建设咖啡馆的打算", "title": "时代的火车向前开", "hideAvatar": "0", "btnOrientation": "1", "btns": [ { "title": "内容不错", "actionURL": "https://www.dingtalk.com/" }, { "title": "不感兴趣", "actionURL": "https://www.dingtalk.com/" } ] }, # todo 是否@所有人 "at": { "atMobiles": [ "155....8773" ], "isAtAll": False } } message_json = json.dumps(message) info = requests.post(url=webhook, data=message_json, headers=header) print(info.text)if __name__ == "__main__": dingmessage()
以上就是钉钉
群机器人
告诉的几种类型。总而言之钉钉机器人推送是一种十分不错的抉择,足以应答日常工作中的各类场景 (
定时揭示
、阈值告警
、事件告诉
等等)