|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created by PyCharm. |
| 5 | +File Name: LinuxBashShellScriptForOps:pySendMessageToDingTalk.py |
| 6 | +Version: 0.0.1 |
| 7 | +Author: dgden |
| 8 | +Author Email: dgdenterprise@gmail.com |
| 9 | +URL: https://github.com/DingGuodong/LinuxBashShellScriptForOps |
| 10 | +Download URL: https://github.com/DingGuodong/LinuxBashShellScriptForOps/tarball/master |
| 11 | +Create Date: 2021/3/5 |
| 12 | +Create Time: 8:50 |
| 13 | +Description: send message to external or internal chat group of "DingTalk" App using OpenAPI |
| 14 | +Long Description: |
| 15 | +References: |
| 16 | +Prerequisites: pip install requests |
| 17 | +Development Status: 3 - Alpha, 5 - Production/Stable |
| 18 | +Environment: Console |
| 19 | +Intended Audience: System Administrators, Developers, End Users/Desktop |
| 20 | +License: Freeware, Freely Distributable |
| 21 | +Natural Language: English, Chinese (Simplified) |
| 22 | +Operating System: POSIX :: Linux, Microsoft :: Windows |
| 23 | +Programming Language: Python :: 2.6 |
| 24 | +Programming Language: Python :: 2.7 |
| 25 | +Topic: Utilities |
| 26 | + """ |
| 27 | +import base64 |
| 28 | +import hashlib |
| 29 | +import hmac |
| 30 | +import json |
| 31 | + |
| 32 | +try: |
| 33 | + from urllib.parse import quote_plus |
| 34 | +except ImportError: |
| 35 | + from urllib import quote_plus |
| 36 | + |
| 37 | +import time |
| 38 | +import requests |
| 39 | + |
| 40 | +DEBUG = True |
| 41 | + |
| 42 | + |
| 43 | +def print_if_debug(value): |
| 44 | + if DEBUG: |
| 45 | + print(value) |
| 46 | + |
| 47 | + |
| 48 | +def send_message_to_dingtalk(message): |
| 49 | + """ |
| 50 | + 使用钉钉群自定义机器人发送消息到钉钉群的步骤: |
| 51 | + 前提:注册企业微信,目前无需企业实名认证 |
| 52 | + 1. 创建钉钉群聊(至少1个人,可以添加无关人再删除无关人达到至少1人的情况),使用PC端登录打开群聊,添加机器人 |
| 53 | + 2. 建议使用“加签”安全设置,记录下签名密钥,如:SECc10***dc2 |
| 54 | + 3. 创建完成后,记录好webhook地址,如:https://oapi.dingtalk.com/robot/send?access_token=40d***c37 |
| 55 | + 4. webhook地址中有access_token,后续会使用到。 |
| 56 | + 参考文档:[自定义机器人接入](https://developers.dingtalk.com/document/app/custom-robot-access) |
| 57 | +
|
| 58 | + :param message: str |
| 59 | + :type message: |
| 60 | + :return: status code |
| 61 | + :rtype: bool |
| 62 | + """ |
| 63 | + |
| 64 | + timestamp = str(int(round(time.time() * 1000))) |
| 65 | + secret = 'SECc10***dc2' |
| 66 | + secret_enc = secret.encode('utf-8') |
| 67 | + string_to_sign = '{}\n{}'.format(timestamp, secret) |
| 68 | + string_to_sign_enc = string_to_sign.encode('utf-8') |
| 69 | + hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() |
| 70 | + sign = quote_plus(base64.b64encode(hmac_code)) |
| 71 | + |
| 72 | + url = "https://oapi.dingtalk.com/robot/send" |
| 73 | + |
| 74 | + querystring = { |
| 75 | + "access_token": '40d***c37', |
| 76 | + "timestamp": timestamp, |
| 77 | + "sign": sign |
| 78 | + } |
| 79 | + |
| 80 | + payload_type_markdown = { |
| 81 | + "msgtype": "markdown", |
| 82 | + "markdown": { |
| 83 | + "title": "需要关注的消息", |
| 84 | + "text": "#### 发现可能需要注意的消息\n{message}".format(message=message), |
| 85 | + }, |
| 86 | + "at": { |
| 87 | + "atMobiles": ["1***2", ], |
| 88 | + "isAtAll": False |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + payload = payload_type_markdown |
| 93 | + |
| 94 | + headers = { |
| 95 | + 'content-type': "application/json", |
| 96 | + } |
| 97 | + |
| 98 | + response = requests.request("POST", url, data=json.dumps(payload), headers=headers, params=querystring) |
| 99 | + |
| 100 | + result_string = response.text |
| 101 | + result_dict = json.loads(result_string) |
| 102 | + if result_dict["errcode"] == 0: |
| 103 | + print_if_debug("send ok") |
| 104 | + print_if_debug(json.dumps(result_dict, indent=4)) |
| 105 | + else: |
| 106 | + print_if_debug("send failed") |
| 107 | + print_if_debug(json.dumps(result_dict, indent=4)) |
| 108 | + |
| 109 | + return response.ok |
0 commit comments