Operations 5 min read

How to Send Real-Time Alerts via Enterprise WeChat Using Python

This guide explains how to set up an Enterprise WeChat alert application, obtain the required corpid and secret, and use a Python script with WeChat Work APIs to send real‑time notification messages to all users on mobile devices.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
How to Send Real-Time Alerts via Enterprise WeChat Using Python

Common alert channels include email, phone, SMS, and WeChat; the author prefers Enterprise WeChat for its immediacy.

To create an alert application, log into the web version of Enterprise WeChat, navigate to Application Management → Application → Create Application, upload a logo, set an application name, and define the visible range.

After the app is created, obtain the corporate ID (corpid) from “My Enterprise → Enterprise Information” and retrieve the secret by viewing the app’s secret page.

The integration relies on two API endpoints: one to get an access token ( https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret} ) and another to send a message ( https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token} ).

The following Python code defines a WeChatPub class that fetches the token and sends a text‑card message via the above APIs.

<code>import json<br/>import datetime<br/>import requests<br/><br/>CORP_ID = ""
SECRET = ""
<br/>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}&amp;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": "@all",
            "toparty": " PartyID1 | PartyID2 ",
            "totag": " TagID1 | TagID2 ",
            "msgtype": "textcard",
            "agentid": 1000002,
            "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)
</code>

Instantiate the class, format the current time, and call send_msg with an HTML‑styled message; the notification will appear instantly on the mobile device if the Enterprise WeChat account has permission.

<code>wechat = WeChatPub()
now = datetime.datetime.now()
timenow = now.strftime('%Y年%m月%d日 %H:%M:%S')
wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">阿里云 cookie 已失效</div><div class=\"highlight\">请尽快更换新的 cookie</div>")
</code>

This simple integration enables real‑time alerting through Enterprise WeChat and is recommended for users who already have a WeChat Work account.

PythonautomationEnterprise MessagingWeChat WorkAlert
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.