Home > Backend Development > Python Tutorial > In just three steps, how to send notifications to WeChat using Python?

In just three steps, how to send notifications to WeChat using Python?

WBOY
Release: 2023-05-02 22:55:05
forward
2194 people have browsed it

                                                                                                                                                                                                In just three steps, how to send notifications to WeChat using Python?

Hello everyone, I am a rookie.

Common notification methods include: email, phone call, text message, and WeChat. SMS and phone calls: usually charged and rarely used; email: suitable for file-type notifications, more formal, and used for archiving; WeChat: suitable for alarm-type notifications, more convenient. The WeChat mentioned here is Enterprise WeChat.

Purpose of this article: Send messages to corporate members through the corporate WeChat application. 2. How to implement corporate WeChat notifications?

1. Create a new application

Log in to the web version of Enterprise WeChat (https://work.weixin.qq.com), click Application Management → Application → Create an application

In just three steps, how to send notifications to WeChat using Python?

Upload the application's logo, enter the application name (bond renewal), and then select the visible range to successfully create an alarm application

In just three steps, how to send notifications to WeChat using Python?

2. Obtain Secret

Use Python to send an alarm request. In fact, only two interfaces are used:

Get Token: https:// qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}

Send request: ​https://www. php.cn/link/8123b781e08f4d9e89ea88f53e6431a9​

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

corpid: Uniquely identifies your enterprise

#secret: Application-level key, with which the program will know which application of the enterprise you want to send

corpid You can pass My Enterprise→Enterprise Information→Enterprise id Get

In just three steps, how to send notifications to WeChat using Python?

secret You can get

In just three steps, how to send notifications to WeChat using Python?In just three steps, how to send notifications to WeChat using Python?# by clicking on the newly created application (bond creation) → View secret → Send

##Finally fill in corpid and secret into the constants below.

3. Code implementation

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",#接收人
            "toparty": "1",#接收部门
            "totag": " TagID1 | TagID2 ",#通讯录标签id
            "msgtype": "textcard",
            "agentid": 1000002,#应用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"{timenow} 注意!今日有新债,坚持打新!")
    print('消息已发送!')


Copy after login

4. Implementation effect:

In just three steps, how to send notifications to WeChat using Python?

In just three steps, how to send notifications to WeChat using Python?

In just three steps, how to send notifications to WeChat using Python?


##

The above is the detailed content of In just three steps, how to send notifications to WeChat using Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template