


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
Upload the application's logo, enter the application name (bond renewal), and then select the visible range to successfully create an alarm application
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
secret You can get
# by clicking on the newly created application (bond creation) → View secret → Send
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:
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('消息已发送!')
##
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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...

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

About Pythonasyncio...

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...

Using python in Linux terminal...

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

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

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