• python3实现企业微信发送媒体消息和文字消息


     

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    class Winxin:
        GET_TOKEN_URL='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={ID}&corpsecret={SECRET}'
        ID='企业的id'
        APP_SECRET='app的secret'
        SHORT_TIME_MEDIA_URL = 'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={ACCESS_TOKEN}&type=file'
        MEG_SEND_URL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}"
        TOUSER = 'user的id(可以是一个列表的形式)'
        BUY_NOW_AGENTID = 1000002
        
        def __init__(self):
            self.app_token=self.get_token(self.APP_SECRET)
    
        def get_token(self,app_screat):
            secret=app_screat
            ask_url=self.GET_TOKEN_URL.format(ID=self.ID,SECRET=secret)
            r=requests.post(ask_url,json=True)
            return r.json()['access_token']
    
        def set_token(self,app_token,app_secret):
            app_token=self.get_token(app_secret)
        
        #上传临时素材,返回素材id
        def loadShortTimeMedia(self,mediaPath):
            url=self.SHORT_TIME_MEDIA_URL
            ask_url=url.format(ACCESS_TOKEN = self.app_token)
            with open(mediaPath,'rb') as f:
                r=requests.post(ask_url,files={'file':f},json=True)
            return json.loads(r.text)['media_id']
        
        
        def send_media_meg(self,media_id=None,mediaPath=None):
            if not media_id:
                media_id=self.loadShortTimeMedia(mediaPath)
                print(media_id)
            ask_url=self.MEG_SEND_URL.format(self.app_token)
            msg_content={}
            post_data={}
            msg_content['media_id'] = media_id
            post_data['touser'] = self.TOUSER
            post_data['msgtype'] = 'image'
            post_data['agentid'] = self.BUY_NOW_AGENTID
            post_data['image'] = msg_content
            post_data['safe'] = '0'
            r=requests.post(ask_url,json=post_data)
            
            return r.text
        
        def send_message(self, values):
            send_url = self.MEG_SEND_URL.format( self.app_token)
            respone = requests.post(send_url, json=values)
            # print(respone)
            x = json.loads(respone.text)['errcode']
            errmsg = json.loads(respone.text)['errmsg']
            if x == 0:
                print('Succesfully')
                return 200
            elif x in (40014, 42001):
                self.set_token(self.app_token,self.APP_SECRET)
                self.send_message(url, values)
                return errmsg
            else:
                return errmsg
    
        def messages(self, message):
            # 使用app发送message
            values = {
                "touser": self.TOUSER,
                "msgtype": 'text',
                "agentid": self.BUY_NOW_AGENTID,
                "text": {'content': message},
                "safe": 0
            }
            # self.token
            return self.send_message(values)
    obj=Winxin()
    
    # r=obj.loadShortTimeMedia( 'D:/qiyeweixinTest.png')
    # m_id='3fRUD1O0FioBa-YrrObz9R7p8d8xRzRFIQbDGCtIlPh52P1uv018wZGNgdvn8h0f'
    if __name__ == '__main__':
        # r=obj.send_media_meg('3llwGhfMJ9YpXxf-igSwp13Ql9qq2XN8gRdbqhZe0ubU')
        r = obj.messages('测试文本是否正常<a>www.baidu.com</a>')
        print(r)
  • 相关阅读:
    递归分治策略
    矩阵连乘问题
    棋盘覆盖问题
    选择排序
    Dijkstra的双栈算术表达式求值算法
    斐波那契数列
    二分算法
    Linux服务器上tengine的安装配置
    Excel Sheet Column Number
    Excel Sheet Column Title
  • 原文地址:https://www.cnblogs.com/yuanji2018/p/12832983.html
Copyright © 2020-2023  润新知