Implementing Enterprise WeChat Notifications with Python
This guide explains how to create an Enterprise WeChat application, obtain the required corp ID and secret, and use a Python script to send notification messages via the WeChat API, complete with step‑by‑step instructions and sample code.
Notification Methods Overview
Common notification channels include email, phone, SMS, and WeChat; email is suitable for formal messages with attachments, while WeChat (specifically Enterprise WeChat) is ideal for alert‑type notifications.
How to Implement Enterprise WeChat Notifications
1. Create an Application
Log in to the Enterprise WeChat web portal, navigate to Application Management → Application → Create Application , upload a logo, set the application name (e.g., "Bond New Issue"), and define its visibility scope.
Upload the app logo, enter the name, and select the visible range to successfully create an alert application.
2. Obtain Secret
The Python implementation uses two API endpoints:
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 essential parameters are corpid (the unique enterprise identifier) and secret (the application‑level key). You can find corpid under My Enterprise → Enterprise Info → Enterprise ID and secret in the newly created application’s details.
3. Code Implementation
import json<br/>import time<br/>import requests<br/>"""<br/>本文件主要实现通过企业微信应用给企业成员发消息<br/>"""<br/>CORP_ID = "xxxx"<br/>SECRET = "xxxx"<br/><br/>class WeChatPub:<br/> s = requests.session()<br/><br/> def __init__(self):<br/> self.token = self.get_token()<br/><br/> def get_token(self):<br/> url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={SECRET}"<br/> rep = self.s.get(url)<br/> if rep.status_code != 200:<br/> print("request failed.")<br/> return<br/> return json.loads(rep.content)['access_token']<br/><br/> def send_msg(self, content):<br/> url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + self.token<br/> header = {"Content-Type": "application/json"}<br/> form_data = {<br/> "touser": "FengXianMei", # receiver<br/> "toparty": "1", # department<br/> "totag": " TagID1 | TagID2 ", # tag IDs<br/> "msgtype": "textcard",<br/> "agentid": 1000002, # app ID<br/> "textcard": {<br/> "title": "债券打新提醒",
"description": content,
"url": "URL",
"btntxt": "更多"
},
"safe": 0
}<br/> rep = self.s.post(url, data=json.dumps(form_data).encode('utf-8'), headers=header)<br/> if rep.status_code != 200:<br/> print("request failed.")<br/> return<br/> return json.loads(rep.content)<br/><br/>if __name__ == "__main__":<br/> wechat = WeChatPub()<br/> timenow = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())<br/> wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">注意!</div><div class=\"highlight\">今日有新债,坚持打新!</div>")<br/> print('消息已发送!')4. Result
The script sends a formatted text card to the specified users, displaying the current time and a custom alert message.
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.