首页 > 编程语言 >python实现推送消息到微信公众号

python实现推送消息到微信公众号

时间:2022-08-26 17:47:16浏览次数:157  
标签:python 微信 self access token appid 推送 id

使用到库

Requests

实现方式

微信已开放了对应的接口,直接通过python的requests库,发起请求,实现推送消息到公众号

微信公众号准备:

1、没有注册微信公众号,可以使用微信提供的测试公众号,来测试公众号的推送

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

2、点击登录,使用自己微信账号,扫一扫登录

 

3、登录成功后,会生成一个自己的测试公众号,有测试号的appid、appsecret

4、要看推送的效果,要先关注当前的测试账号,关注成功后,可在列表查看当前的粉丝数和具体的open_id

使用微信公众号的接口

1、         获取微信公众号的授权token:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appsecret}

2、         获取当前公众号的粉丝的open_id:

https://api.weixin.qq.com/cgi-bin/user/get?access_token={self.token}&next_openid={next_openid}

3、         发送模板消息的接口:

https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={self.token}

4、         发送普通消息的接口:

https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=xxx

实现逻辑

发送消息的接口,需要验证token,和传入粉丝的open_id,进行发送,发送模板消息,需要传入模板id

微信公众号后台,模板管理,增加模板,模板内容需要配置对应

 

 

实现代码:

class WechatMessagePush:
    def __init__(self, appid, appsecret, temple_id):
        self.appid = appid
        self.appsecret = appsecret

        # 模板id,参考公众号后面的模板消息接口 -> 模板ID(用于接口调用):IG1Kwxxxx
        self.temple_id = temple_id

        self.token = self.get_Wechat_access_token()
   

    def get_Wechat_access_token(self):
        '''
        获取微信的access_token: 获取调用接口凭证
        :return:
        '''
        url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={self.appid}&secret={self.appsecret}"
        response = requests.get(url)

        res = response.json()
        if "access_token" in res:
            token = res["access_token"]
            return token

    def get_wechat_accout_fans_count(self):
        '''
        获取微信公众号所有粉丝的openid
        '''
        next_openid = ''
        url = f"https://api.weixin.qq.com/cgi-bin/user/get?access_token={self.token}&next_openid={next_openid}"
        response = requests.get(url)
        res = response.json()['data']['openid']

    def send_wechat_temple_msg(self, content):
        '''
        发送微信公众号的模板消息'''
        url = f"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={self.token}"

        fan_open_id = self.get_wechat_accout_fans_count()
        for open_id in fan_open_id:
            body = {
                "touser": open_id,
                'template_id': self.temple_id,    
                # 'url': 'http://www.jb51.net',  
                "topcolor": "#667F00",
                "data": {
                    "content": {"value": content}
                }
            }
            headers = {"Content-type": "application/json"}
            data = json.JSONEncoder().encode(body)
            res = requests.post(url=url, data=data, headers=headers)

调用方法,发送消息

if __name__ == '__main__':
    appid = "wx4d4xxxx"
    screct = "522xxxx4"
    template_id = 'IG1Kxxbxxxxxls'
    WechatMessagePush(appid, screct, template_id).send_wechat_txt_msg(msg="测试")

实现的推送的消息

标签:python,微信,self,access,token,appid,推送,id
From: https://www.cnblogs.com/cuitang/p/16628362.html

相关文章

  • Python中关于类与对象的创建
    classStudent:native_space='重庆'#在类里面直接定义变量,称为类属性#初始化方法def__init__(self,name,age):self.name=nameself.age......
  • 使用python批量爬取wallhaven.cc壁纸站壁纸
    偶然发现https://wallhaven.cc/这个壁纸站的壁纸还不错,尤其是更新比较频繁,用python写个脚本爬取点latest,按照更新先后排序,获得新地址,发现地址是分页展示的,每一页24张 ......
  • 学习:python 综合训练 超市商品管理系统 数据库版
                 ......
  • python3 函数 定义函数与切片
     如果我们要计算一个圆的面积,就是3.14*r*r,如果每次就算,则每次都要写一遍,就很麻烦,所以有了函数,我们就可以通过调用函数的方法,直接使用就行了。 这里我们可以访问 ......
  • 数据分析大作战,SQL V.S. Python,来看看这些考题你都会吗 ⛵
    ......
  • 在python程序中调用java代码
     Python是一门“胶水”语言,非常灵活多变,但是在一些特殊的时候,也需要调用其它语言来协助实现更多的功能;在公司使用python进行接口测试的时候,会遇到有些接口数据是由公司的......
  • python基本数据类型
    python基本数据类型python基本数据类型有:整型,浮点型,布尔型,复数型,字符串,列表,元组,字典,集合。六大基本数据类型:①.Number(数字)②.String(字符串)③.List(列表)④.Tuple(元组)......
  • python 读取json文件
    一个jason文件实例————fcc.json{"organization":"freeCodeCamp","website":"https://www.freecodecamp.org/","formed":2014,"founder":"QuincyLarson","ce......
  • Linux安装python并配置环境变量
    1.获取python3源码我们访问这个网址,就可以看到全部的python下载方式:【https://www.python.org/downloads/】   本人下载的是3.10.6版本   2.安装Python这......
  • python学习课后练习
    此次爬虫学习的资源是B站所找,具体如下:Python课程天花板,Python入门+Python爬虫+Python数据分析5天项目实操/Python基础,该课程留了课后练习,我把自己的代码和想法单独整成一......