Source: https://blog.csdn.net/shammy_feng/article/details/123711347
1. What are the notification methods?
Common notification methods are: email, phone, SMS, WeChat. SMS and phone calls: usually chargeable and rarely used; email: suitable for notification with file type, more formal, and used for archiving; WeChat: suitable for alarm type notification, more convenient. The WeChat mentioned here is enterprise WeChat.
The purpose of this article is to send messages to corporate members through the corporate WeChat application.
2. How to realize enterprise WeChat notification?
1. Create a new application
Log in to the web version of Enterprise WeChat (https://work.weixin.qq.com), click Application Management → Application → Create Application
Upload the logo of the application, enter the application name (new bond), and then select the visible range to successfully create an alarm application
2. Get Secret
Using Python to send alert requests, in fact, only two interfaces are used:
Get Token : https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}
Send request: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}
As you can see, the most important are the corpid and secret:
Corpid: Uniquely identifies your business
secret: the application-level key, with which the program knows which application of the enterprise you want to send
Corpid can be obtained through my enterprise → enterprise information → enterprise id
The secret can be obtained by clicking on the newly created application (new bond) → view secret → send
Finally fill in the following constants with corpid and secret.
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"<div class=\"gray\">{timenow}</div> <div class=\"normal\">注意!</div><div class=\"highlight\">今日有新债,坚持打新!</div>")
print('消息已发送!')
4. Achieve the effect:
In the end, we developed a crawler class to take you from 0 to 1 to learn Python crawler. You can take orders as a sideline or as a crawler engineer for your main business, which can improve efficiency. Welcome to scan the code to learn more.
Check it out: finally here, Peng Tao Python Crawler Training Camp!
Long press to scan the code, you can listen to it. If you want to know more about the crawler, you can add " 257735 "
Scan the code to note " Crawler "