Skip to content

Commit 9b8bfb9

Browse files
committed
send message to WeiXin Work or DingTalk App using OpenAPI
1 parent c0218ad commit 9b8bfb9

2 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created by PyCharm.
5+
File Name: LinuxBashShellScriptForOps:pySendMessageToWeixinWork.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:49
13+
Description: send message to internal chat group of "WeiXin Work" 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 json
28+
29+
import requests
30+
31+
DEBUG = True
32+
33+
34+
def print_if_debug(value):
35+
if DEBUG:
36+
print(value)
37+
38+
39+
def send_message_to_work_weixin(message):
40+
"""
41+
使用企业微信群机器人发送消息到企业内部群的步骤:
42+
前提:注册企业微信,目前无需企业实名认证
43+
1. 创建企业微信内部群聊,
44+
2. 添加群机器人,记录下Webhook地址即可
45+
注意:企业微信群聊机器人只能用在企业微信内部群聊,不能添加到外部群聊
46+
配置参考:[如何配置群机器人](https://work.weixin.qq.com/help?person_id=1&doc_id=13376&helpType=undefined)
47+
48+
:param message:
49+
:type message: str
50+
:return:
51+
:rtype: bool
52+
"""
53+
url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
54+
55+
querystring = {
56+
"key": 'ca9***5b6',
57+
}
58+
59+
payload_type_markdown = {
60+
"msgtype": "markdown",
61+
"markdown": {
62+
"content": "#### 发现可能需要注意的消息\n{message}".format(message=message)
63+
}
64+
}
65+
66+
payload = payload_type_markdown
67+
68+
headers = {
69+
'content-type': "application/json",
70+
}
71+
72+
response = requests.request("POST", url, data=json.dumps(payload), headers=headers, params=querystring)
73+
74+
result_string = response.text
75+
result_dict = json.loads(result_string)
76+
if result_dict["errcode"] == 0:
77+
print_if_debug("send ok")
78+
print_if_debug(json.dumps(result_dict, indent=4))
79+
else:
80+
print_if_debug("send failed")
81+
print_if_debug(json.dumps(result_dict, indent=4))
82+
83+
return response.ok

0 commit comments

Comments
 (0)