How to Implement Enterprise WeChat Notifications with Python
This guide explains how to create a custom Enterprise WeChat application, obtain the required corp ID and secret, and use a Python script that calls the WeChat API to send text‑card notifications to specified users, departments or tags.
Enterprise WeChat provides several notification channels such as email, SMS, phone and WeChat; the latter (especially the corporate version) is most convenient for alert‑type messages. This article demonstrates how to send such alerts to enterprise members using a Python program.
1. Create an Application
Log in to the web version of Enterprise WeChat (https://work.weixin.qq.com), navigate to Application Management → Application → Create Application , upload a logo, set the application name (e.g., "Bond New Issue"), and define the visible scope.
After creation, you can view the corp ID (enterprise identifier) and the application secret (app‑level key) in the application details page.
2. Obtain Token and Send Requests
The two essential API endpoints are:
Get token: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}
Send message: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}
The corpid uniquely identifies the enterprise, while the secret tells the API which application to use.
3. Python Implementation
<code>import json
import time
import requests
CORP_ID = "xxxx"
SECRET = "xxxx"
class WeChatPub:
s = requests.session()
def __init__(self):
self.token = self.get_token()
def get_token(self):
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={SECRET}"
rep = self.s.get(url)
if rep.status_code != 200:
print("request failed.")
return
return json.loads(rep.content)["access_token"]
def send_msg(self, content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + self.token
header = {"Content-Type": "application/json"}
form_data = {
"touser": "FengXianMei", # recipient
"toparty": "1", # department
"totag": " TagID1 | TagID2 ", # tag IDs
"msgtype": "textcard",
"agentid": 1000002, # application ID
"textcard": {
"title": "债券打新提醒",
"description": content,
"url": "URL",
"btntxt": "更多"
},
"safe": 0
}
rep = self.s.post(url, data=json.dumps(form_data).encode('utf-8'), headers=header)
if rep.status_code != 200:
print("request failed.")
return
return json.loads(rep.content)
if __name__ == "__main__":
wechat = WeChatPub()
timenow = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">注意!</div><div class=\"highlight\">今日有新债,坚持打新!</div>")
print('消息已发送!')
</code>4. Result
The script sends a formatted text‑card message to the specified users; screenshots of the successful notification are shown below.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.