Table of Contents
1. Create a new application
2. Obtain Secret
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?

May 02, 2023 pm 10:55 PM
python WeChat

                                                                                                                                                                                                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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

Company security software causes common applications to fail to run. How to troubleshoot the compatibility issues of HUES security software? Company security software causes common applications to fail to run. How to troubleshoot the compatibility issues of HUES security software? Apr 02, 2025 am 06:21 AM

Compatibility issues with company security software and application and troubleshooting. Many companies will install security software in order to ensure internal network security. However, sometimes security software...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

See all articles