preparation:
Register an enterprise on the official website of enterprise wechat; log in to the background of enterprise wechat, create a "self built" application, and obtain the three necessary parameters of enterprise ID, agentid and secret; create multiple test accounts in the address book of enterprise wechat; install the "enterprise wechat" APP on the mobile terminal, log in to enterprise wechat using the test account, and prepare to receive messages.
Program code:
Enterprise wechat provides API development interface, interacts with the background of enterprise wechat through GET and POST methods of HTTPS, and completes the operations of obtaining token, sending data and obtaining data.
Python code mainly uses requests library to encapsulate enterprise wechat API, simulate https GET and POST operations, and send enterprise wechat messages to designated users.
#!/usr/bin/env python # -*- coding: utf-8 -*- import time import requests import json class WeChat: def __init__(self): self.CORPID = 'ww2e1234567895498f5498f' #Enterprise ID, obtained in the management background self.CORPSECRET = 'xy11234567898hk_ecJ123456789DhKy4_1y12345OI'#The secret of self built applications. Each self built application has a separate secret self.AGENTID = '1000002' #Application ID, obtained in background application self.TOUSER = "maomao|dingding" #User name of the receiver, multiple users are divided by | def _get_access_token(self): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' values = {'corpid': self.CORPID, 'corpsecret': self.CORPSECRET, } req = requests.post(url, params=values) data = json.loads(req.text) return data["access_token"] def get_access_token(self): try: with open('./tmp/access_token.conf', 'r') as f: t, access_token = f.read().split() except: with open('./tmp/access_token.conf', 'w') as f: access_token = self._get_access_token() cur_time = time.time() f.write('\t'.join([str(cur_time), access_token])) return access_token else: cur_time = time.time() if 0 < cur_time - float(t) < 7260: return access_token else: with open('./tmp/access_token.conf', 'w') as f: access_token = self._get_access_token() f.write('\t'.join([str(cur_time), access_token])) return access_token def send_data(self, message): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token() send_values = { "touser": self.TOUSER, "msgtype": "text", "agentid": self.AGENTID, "text": { "content": message }, "safe": "0" } send_msges=(bytes(json.dumps(send_values), 'utf-8')) respone = requests.post(send_url, send_msges) respone = respone.json() #When the returned data is a json string, you can directly use. json to convert the response to a dictionary return respone["errmsg"] if __name__ == '__main__': wx = WeChat() wx.send_data("This is the first message sent by the program!\n Python Program calling enterprise wechat API,Message sent to administrator from self built application "alarm test application"!") wx.send_data("This is the second message sent by the program!")
Running screenshot:
Reference link:
python implementation sends messages through enterprise wechat
https://www.cnblogs.com/bluezms/p/8948187.html
python script -- sending information with enterprise wechat
https://blog.csdn.net/liyyzz33/article/details/86080936
Enterprise wechat background management:
Enterprise wechat API document:
https://work.weixin.qq.com/api/doc#90000/90003/90487