Skip to content

Commit c21b7ae

Browse files
committed
update and make code easy to use.
1 parent f6f198b commit c21b7ae

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

projects/WeChatOps/pySendMessageByWeChatWithoutSQL/sendMessageByWeChatApi.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
References:
1515
[微信公众平台-企业号开发者中心-接口文档](http://qydev.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5)
1616
[发送接口说明](http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E)
17-
17+
update@202104
18+
[企业微信发送应用消息](https://work.weixin.qq.com/api/doc/90000/90135/90236)
1819
Prerequisites: [shelve, json,]
1920
"""
2021
import datetime
@@ -118,6 +119,25 @@ def reset_data(self):
118119
self.get_access_token()
119120

120121
def sender_config(self, content, to_user="", to_party="", to_tag="", msg_type="text", agent_id=1, safe=1):
122+
"""
123+
124+
:param content:
125+
:type content:
126+
:param to_user: 成员名称使用该成员在企业微信中的账号名(并非显示名称),账号名可以在企业微信后台查到
127+
:type to_user: str
128+
:param to_party:
129+
:type to_party:
130+
:param to_tag:
131+
:type to_tag:
132+
:param msg_type:
133+
:type msg_type:
134+
:param agent_id:
135+
:type agent_id:
136+
:param safe:
137+
:type safe:
138+
:return:
139+
:rtype:
140+
"""
121141
# parameters instruction see this URL as follows.
122142
# http://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F
123143
self.message_data['touser'] = to_user
@@ -129,10 +149,22 @@ def sender_config(self, content, to_user="", to_party="", to_tag="", msg_type="t
129149
self.message_data['text']['content'] = content
130150
self.message_data['safe'] = safe
131151

132-
def send(self):
152+
def send(self, debug=False):
153+
"""
154+
该方法不同于企业微信机器人,此方法是将信息通过企业微信管理后台中"应用管理"中创建的"应用",
155+
发送给该应用中的成员,成员名称使用该成员在企业微信中的账号名(并非显示名称),账号名可以在企业微信后台查到
156+
:return:
157+
:rtype:
158+
"""
133159
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
134160

135-
querystring = {"access_token": self.access_token}
161+
if debug:
162+
querystring = {
163+
"access_token": self.access_token,
164+
"debug": "1"
165+
}
166+
else:
167+
querystring = {"access_token": self.access_token}
136168

137169
payload = json.dumps(self.message_data, encoding='utf-8', ensure_ascii=False)
138170

@@ -146,8 +178,12 @@ def send(self):
146178

147179

148180
if __name__ == '__main__':
149-
CorpID = 'your corp id'
150-
Secret = 'your secret for your corp id'
181+
# [企业微信管理后台](https://work.weixin.qq.com/wework_admin/)
182+
CorpID = 'your_corp_id' # 企业ID,可通过企业微信后台"我的企业"|"企业ID"查看
183+
Secret = 'your_secret_of_this_app' # 企业微信中自建企业应用的Secret
184+
AgentID = 1000002 # 企业微信中自建企业应用的AgentId
185+
ToUser = 'DingGuodong' # 企业微信企业成员的账号名(非显示名)
186+
151187
w = WeChatMessageSender(CorpID, Secret)
152-
w.sender_config("message body to send.", to_user='username in your corp\'s app', agent_id=2)
188+
w.sender_config("message body to send.", to_user=ToUser, agent_id=AgentID)
153189
w.send()

0 commit comments

Comments
 (0)