python automatically sends results to enterprise wechat

Keywords: Python JSON Excel encoding

With the progress of science and technology, more and more people feel that python is powerful and lucky, especially some automatic detection, etc., which automates some mechanized work, and finally sends the results to wechat or email. If you are too lazy to read wechat or email, you can call to inform you if you are OK, doesn't it feel magical??? Ha ha, actually it's very simple, directly enter the theme——
In the early stage, I wrote a script to send e-mail. This program can send e-mail to the target user (can send e-mail to multiple users) (can bring attachments (Excel, Word, JPG)). You can download it for reference if you are interested: https://blog.csdn.net/Luzaofa/article/details/83065747
Today is the last night of the last day of February 2019, and the next February 28 will be 2021. In order to commemorate this special day, we specially send a small program to send information to the enterprise wechat.
Direct code:
First in the current directory will be a: CONFIG.conf file, fill in the following information. For specific configuration, please refer to: https://work.weixin.qq.com/api/doc × 90000 / 90135 / 90664

[mass]
CORPID = 'XXXX'
CORPSECRET = 'XXXX'
AGENTID = 'XXXX'
TOUSER = 'XXXX'  # Recipient user name

2,New Wechat.py , enter the following code

#!/usr/bin/env python
# encoding: utf-8
# Time    : 2/28/2019 4:19 PM
# Author  : Luzaofa

import time
import requests
import json
import ConfigParser

class Config(object):
    '''Resolve profile'''

    def get_config(self, lable, value):
        cf = ConfigParser.ConfigParser()
        cf.read("CONFIG.conf")
        config_value = cf.get(lable, value)
        return config_value

class WeChat(Config):
	'''Send information to enterprise wechat'''

    def __init__(self):
    	'''Initialize configuration'''
    	super(Config, self).__init__()
        self.CORPID = self.get_config("mass", "CORPID")
        self.CORPSECRET = self.get_config("mass", "CORPSECRET")
        self.AGENTID = self.get_config("mass", "AGENTID")
        self.TOUSER = self.get_config("mass", "TOUSER")

    def _get_access_token(self):
    	'''Initiate request'''
        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)
        print data
        return data["access_token"]

    def get_access_token(self):
    	'''Obtain token,Save to local'''
        try:
            with open('access_token.conf', 'r') as f:
                t, access_token = f.read().split()
        except Exception:
            with open('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('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 message'''
        msg = message.encode('utf-8')
        send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
        send_data = '{"msgtype": "text", "safe": "0", "agentid": %s, "touser": "%s", "text": {"content": "%s"}}' % (
            self.AGENTID, self.TOUSER, msg)
        r = requests.post(send_url, send_data)
        print r.content
        return r.content


if __name__ == '__main__':
    wx = WeChat()
    wx.send_data("test")

To execute the script, you only need to change and initialize the relevant information according to your own enterprise wechat information. After the configuration, you can send the test to the designated user's enterprise wechat to share with you.

 

157 original articles published, 33 praised, 20000 visitors+
Private letter follow

Posted by rubenc on Wed, 19 Feb 2020 06:18:05 -0800