当前位置:   article > 正文

python微信公众号推送消息

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   每日情话

代码

  1. import json, random, requests
  2. class WechatSendmes():
  3. def __init__(self,appid,secret,template_id):
  4. self.appid = appid
  5. self.secret = secret
  6. self.template_id = template_id
  7. def get_token(self):
  8. '''
  9. # 获取access_token
  10. :return:
  11. '''
  12. url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(
  13. self.appid, self.secret)
  14. headers = {
  15. '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'
  16. }
  17. res = requests.get(url, headers=headers).json()
  18. access_token = res['access_token']
  19. return access_token
  20. def get_openid(self):
  21. '''
  22. # 获取要推送用户的openid
  23. :return:
  24. '''
  25. next_openid = '' # 第一个拉取的OPENID,不填默认从头开始拉取 ,一次只能获取10000条
  26. url_openid = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s' % (
  27. self.get_token(), next_openid)
  28. ans = requests.get(url_openid)
  29. openid = json.loads(ans.content)['data']['openid']
  30. return openid
  31. def get_shici(self):
  32. '''
  33. # 随机诗词句获取
  34. :return:
  35. '''
  36. url = 'https://v1.jinrishici.com/'
  37. headers = {"Content-Type": "application/json; charset=utf-8"}
  38. res = requests.get(url, headers=headers)
  39. api_lists = res.json()["list"]
  40. url_apis = api_lists[random.randint(0, len(api_lists))]
  41. url_api = list(url_apis.values())[0]
  42. res_shici = requests.get(url=url_api, headers=headers)
  43. shici_data = res_shici.json()
  44. zuozhe = shici_data["author"]
  45. shi_name = shici_data["origin"]
  46. nr = shici_data["content"]
  47. return zuozhe, shi_name, nr
  48. def sendmes(self):
  49. '''
  50. 推送到微信公众号
  51. :return:
  52. '''
  53. author, origin, content = self.get_shici()
  54. url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(self.get_token())
  55. headers = {"Content-type": "application/json"}
  56. for index, template_id in enumerate(self.get_openid()):
  57. body = {
  58. "touser": template_id, # 接收内容的用户openid
  59. "template_id": self.template_id, # 模板ID
  60. "url": f"https://baike.baidu.com/item/{origin}", # 可跳转的链接
  61. # "topcolor": "#FF0000",
  62. "data": {
  63. "city": {"value": '中国-西安', "color": '#00EC00'}, # 颜色功能已失效,2023年5月4日已改版,不支持颜色
  64. 'author': {'value': author},
  65. 'origin': {'value': origin},
  66. 'content': {'value': content}
  67. }
  68. }
  69. res = requests.post(url, json=body, headers=headers)
  70. print('推送成功') if res.json()['errmsg'] == 'ok' else print(f'用户{template_id}推送失败')
  71. if __name__ == '__main__':
  72. push = WechatSendmes(appid_data,secret_data,template_id_data)
  73. push.sendmes()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/70689
推荐阅读
相关标签
  

闽ICP备14008679号