How to send alarm notification to wechat in Python?

Keywords: Kubernetes

Recently, I became Alibaba cloud's Promotion Ambassador and brought 200 readers together to receive Alibaba cloud's servers for free. Everyone said "really fragrant".

PS: if you want to participate in receiving the free server, you can add me v: Hello WBM, and I'll send you the operation process.

In fact, there are still many problems in organizing the first phase of activities, mainly in the process.

In order to make the whole process more automated and more fluent, official account is used to integrate the whole process into self-help query of official account messages.

One step is to confirm the user's purchase qualifications, as long as I reply to the corresponding Ali cloud ID in the background of my official account, I will query the associated data of Ali cloud background on my side. But Ali's cookie will fail for several hours. This will be a bit embarrassing. The readers in the background are invalid. The readers who have participated in it have been checking. All the data found are unrelated data.

At this time, real-time alarm is very important. Common alarm methods include email, telephone, SMS and wechat.

SMS and phone calls are usually charged (if there is no charge, you can share it), and email is not so timely, so I finally chose wechat notification.

The wechat mentioned here is enterprise wechat, and I have used the license to register an individual before, so I can easily register my own enterprise wechat.

1. Create a new application

Log in to the web version of enterprise wechat( ), click application management - > Application - > create application

Upload the logo of the application, enter the application name, and then select the visible range to successfully create an alarm application

2. Obtain Secret

When Python is used to send alarm requests, only two interfaces are used

  • Get Token: {corpid}&corpsecret={secret}
  • Send request:{token}

As you can see, the most important are corpid and secret:

  • corpid: uniquely identify your business
  • secret: application level key. Only with the program can you know which application of the enterprise you want to send

corpid can be obtained through my enterprise - > enterprise information

It's a little more troublesome to obtain secret. Click to create an application and click to view secret

Then click send and it will be sent to your enterprise wechat

Finally, fill the following constants with corpid and secret.

import json
import datetime
import requests

CORP_ID = ""

class WeChatPub:
    s = requests.session()

    def __init__(self):
        self.token = self.get_token()

    def get_token(self):
        url = f"{CORP_ID}&corpsecret={SECRET}"
        rep = self.s.get(url)
        if rep.status_code != 200:
            print("request failed.")
        return json.loads(rep.content)['access_token']

    def send_msg(self, content):
        url = "" + self.token
        header = {
            "Content-Type": "application/json"
        form_data = {
            "touser": "@all",
            "toparty": " PartyID1 | PartyID2 ",
            "totag": " TagID1 | TagID2 ",
            "msgtype": "textcard",
            "agentid": 1000002,
            "textcard": {
                "title": "Service exception alarm",
                "description": content,
                "url": "URL",
                "btntxt": "more"
            "safe": 0
        rep =, data=json.dumps(form_data).encode('utf-8'), headers=header)
        if rep.status_code != 200:
            print("request failed.")
        return json.loads(rep.content)

Then you can send_msg function sent a message.

wechat = WeChatPub()
now =
timenow = now.strftime('%Y year%m month%d day %H:%M:%S')
wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">Alibaba cloud cookie Invalid</div><div class=\"highlight\">Please replace it with a new one as soon as possible cookie</div>")

As long as your enterprise wechat does not have the permission to close the notification, your mobile phone will pop up this alarm message immediately.

In a few simple steps, the enterprise wechat is connected to realize the real-time alarm function of the mobile phone. It is recommended to students with enterprise wechat.

Of course, there must be more and better implementation methods. I just chose one of them. If you have good ideas, you can also share them in the comment area.

At the end of the article, I will introduce three online documents written by myself:

First document: PyCharm Chinese guide 1.0 document

It took me more than two months to sort out the usage skills of 100 PyCharm. In order for novices to get started directly, I spent a lot of time recording hundreds of GIF moving pictures and went to the online documents to read them.

Second document: PyCharm dark magic guide 1.0 document

The system includes all kinds of Python popular knowledge, various playing methods of Python Shell, crazy Python technology operation, super detailed advanced knowledge interpretation of python, very practical Python development skills, etc.

Third document: Python Chinese guide 1.0 document

It took three months to write an all Chinese tutorial suitable for zero foundation introduction python. Combined with a large number of code cases, it gives beginners an intuitive feeling about the operation effect of the code. The tutorial has both depth and breadth. Each article will mark the difficulty of the content. It is basic or advanced. It can be selected by readers. It is a rare Python Chinese electronic tutorial.

Posted by Bifter on Sat, 06 Nov 2021 19:21:33 -0700