Home > Article > Backend Development > 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?
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
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
##Finally fill in corpid and secret into the constants below. 3. Code implementationimport 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!