python微信公众号推送消息

目录

准备数据

接口

代码


微信公众号平台:微信公众平台 (qq.com)

微信公众号开发文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html

准备数据

1、微信公众号注册:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

2、注册成功后可生成属于自己的appID和appsecret

​3、想要查看推送的效果,需先关注当前的测试账号,关注成功后,可在列表查看当前的粉丝数和具体的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

5、其他构造数据的接口推荐

古诗词·一言API - 可以随机获取一句古诗词名句的接口   可免费使用

古诗词文名句接口文档 — SaintIC Docs 文档

唐诗大全API接口 - 天行数据TianAPI

今日诗词 - 一言API - 诗词实时智能推荐 - 今日诗词开放接口 - 今日诗词 API - 个人文章分享

天气网 (weather.com.cn)      使用爬虫进行获取

(10条消息) 中国天气城市代码编号_101010100_it_zujun的博客-CSDN博客

‘’使用方法:f'http://www.weather.com.cn/weather{城市编号‘’使用方法:'http://www.weather.com.cn/weather{}/.shtml'

https://api.lovelive.tools/api/SweetNothings/Serialization/Json   每日情话

代码

import json, random, requestsclass WechatSendmes():def __init__(self,appid,secret,template_id):self.appid = appidself.secret = secretself.template_id = template_iddef get_token(self):'''# 获取access_token:return:'''url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(self.appid, self.secret)headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'}res = requests.get(url, headers=headers).json()access_token = res['access_token']return access_tokendef get_openid(self):'''# 获取要推送用户的openid:return:'''next_openid = ''  # 第一个拉取的OPENID,不填默认从头开始拉取 ,一次只能获取10000条url_openid = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s' % (self.get_token(), next_openid)ans = requests.get(url_openid)openid = json.loads(ans.content)['data']['openid']return openiddef get_shici(self):'''# 随机诗词句获取:return:'''url = 'https://v1.jinrishici.com/'headers = {"Content-Type": "application/json; charset=utf-8"}res = requests.get(url, headers=headers)api_lists = res.json()["list"]url_apis = api_lists[random.randint(0, len(api_lists))]url_api = list(url_apis.values())[0]res_shici = requests.get(url=url_api, headers=headers)shici_data = res_shici.json()zuozhe = shici_data["author"]shi_name = shici_data["origin"]nr = shici_data["content"]return zuozhe, shi_name, nrdef sendmes(self):'''推送到微信公众号:return:'''author, origin, content = self.get_shici()url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(self.get_token())headers = {"Content-type": "application/json"}for index, template_id in enumerate(self.get_openid()):body = {"touser": template_id,  # 接收内容的用户openid"template_id": self.template_id,  # 模板ID"url": f"https://baike.baidu.com/item/{origin}",  # 可跳转的链接# "topcolor": "#FF0000","data": {"city": {"value": '中国-西安', "color": '#00EC00'},  # 颜色功能已失效,2023年5月4日已改版,不支持颜色'author': {'value': author},'origin': {'value': origin},'content': {'value': content}}}res = requests.post(url, json=body, headers=headers)print('推送成功') if res.json()['errmsg'] == 'ok' else print(f'用户{template_id}推送失败')if __name__ == '__main__':push = WechatSendmes(appid_data,secret_data,template_id_data)push.sendmes()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部