SoFunction
Updated on 2024-12-10

Using Python to realize enterprise weibo notification function case study

preamble

Common notification methods are: email, phone, SMS, WeChat. SMS and phone: usually charged, less used; email: suitable for notification with document type, more positive

style, archive use; WeChat: suitable for alarm type notification, more convenient. The WeChat mentioned here is the enterprise WeChat.

Purpose of this article: to send messages to enterprise members through the enterprise WeChat application.

How to implement enterprise WeChat notifications?

1、New application

Log in to the web version of Enterprise WeChat (), click Application Management → Applications → Create Application.

Upload the logo of the application, enter the application name (bond typing new), and then select the visible range to successfully create an alert application

2、Get Secret

Sending an alert request using Python really only uses two interfaces:

Get Token:

/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}

Send request:

/cgi-bin/message/send?access_token={token}

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

corpid: uniquely identifying your business

secret: application-level key, with which the program knows which application of the enterprise you want to send the

corpid can be obtained from My Business → Business Information → Business id.

The secret can be retrieved by clicking on the newly created application (Bond Refresh) → View Secret → Send.

Finally, put corpid and secret into the constants below.

3、Code realization

import json
import time
import requests
'''
This document mainly realizes sending messages to enterprise members through enterprise WeChat application

CORP_ID = "xxxx"
SECRET = "xxxx"

class WeChatPub:
    s = ()

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

    def get_token(self):
        url = f"/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={SECRET}"
        rep = (url)
        if rep.status_code != 200:
            print("request failed.")
            return
        return ()['access_token']

    def send_msg(self, content):
        url = "/cgi-bin/message/send?access_token=" + 
        header = {
            "Content-Type": "application/json"
        }
        form_data = {
            "touser": "FengXianMei",#Receiver
            "toparty": "1",# Receiving department
            "totag": " TagID1 | TagID2 ",#addressbook tag id
            "msgtype": "textcard",
            "agentid": 1000002,#Application ID
            "textcard": {
                "title": "Bond Refreshment Alert.",
                "description": content,
                "url": "URL",
                "btntxt": "More"
            },
            "safe": 0
        rep = (url, data=(form_data).encode('utf-8'), headers=header)
        return ()

if __name__ == "__main__":
    wechat = WeChatPub()
    timenow = ("%Y-%m-%d %H:%M:%S",())
    wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">take note of!</div><div class=\"highlight\">There are new bonds today,Stick with the new one.!</div>")
    print('Message sent!')

4. Realize the effect:

Today to share with you the use of Python to realize the enterprise WeChat notification of the small case here to you to do out, a thousand words, are not as good as their own hand operation to the actual.

This article on the use of Python to achieve enterprise WeChat notification function case study is introduced to this article, more related Python enterprise WeChat notification content, please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!