当前位置: 首页 > news >正文

微信公众号推送文字消息与模板消息

要实现对微信公众好粉丝消息的推送,首先要知道推送粉丝的openid,然后根据openid向粉丝推送消息。
支持多种类型,包括一段文字,也包括有结构的模板消息。

1、必要信息

需要用户先关注公众号
首先要查询到公众号的appid、secret信息
如果发送模板消息,还需要查询模板id:templete_id
并设置模板:

字段1 {{string1.DATA}}
字段2 {{string2.DATA}}
字段3 {{string3.DATA}}

2、openid的获取

def get_access_token(appid, secret):url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"response = requests.get(url)return response.json()['access_token']## 获取要推送用户的openid
def get_openid():next_openid = ''  # 第一个拉取的OPENID,不填默认从头开始拉取 ,一次只能获取10000条url_openid = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s' % (get_access_token(appid, secret), next_openid)ans = requests.get(url_openid)openid = json.loads(ans.content)['data']['openid']return openid

3、推送文字消息

def send_text_message(access_token, openid, content):url = f"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={access_token}"text_message = {"touser": openid,"template_id": template_id,"url": "用户点击后的跳转链接","data": {"content": "1"}}response = requests.post(url, json=text_message)return response.json()

4、推送模板消息


def send_template_message(openid, content):print(content)client = WeChatClient(appid, secret)template_message = {"user_id": openid,"template_id": template_id,"url": "访问链接","data": {"string1": {"value": "string1","color": "#173177"},"string2": {"value": "string2","color": "#173177"},"string3": {"value": "string3","color": "#173177"}}}try:res = client.message.send_template(**template_message)print("✅ 模板消息发送成功!")print("返回结果:", res)return resexcept WeChatException as e:print("❌ 模板消息发送失败!")print("错误码:", e.errcode)print("错误信息:", e.errmsg)

5、代码实现

整体代码实现逻辑

appid = "替换"
secret = "替换"
template_id="替换"
def get_access_token(appid, secret):url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"response = requests.get(url)return response.json()['access_token']## 获取要推送用户的openid
def get_openid():next_openid = ''  # 第一个拉取的OPENID,不填默认从头开始拉取 ,一次只能获取10000条url_openid = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s' % (get_access_token(appid, secret), next_openid)ans = requests.get(url_openid)openid = json.loads(ans.content)['data']['openid']return openiddef send_text_message(access_token, openid, content):url = f"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={access_token}"text_message = {"touser": openid,"template_id": template_id,"url": "用户点击后的跳转链接","data": {"content": "1"}}response = requests.post(url, json=text_message)return response.json()def send_template_message(openid, content):print(content)client = WeChatClient(appid, secret)template_message = {"user_id": openid,"template_id": template_id,"url": "url","data": {"string1": {"value": "string3","color": "#173177"},"string2": {"value": "string3","color": "#173177"},"string3": {"value": "string3","color": "#173177"}}}try:res = client.message.send_template(**template_message)print("✅ 模板消息发送成功!")print("返回结果:", res)return resexcept WeChatException as e:print("❌ 模板消息发送失败!")print("错误码:", e.errcode)print("错误信息:", e.errmsg)#推送到微信公众号
def sendmes(msg):openid = get_openid()# 发送模板result = send_template_message(openid, msg)# 发送文字#access_token = get_access_token(appid, secret)#result = send_text_message(access_token, openid[0], msg)print(result)
http://www.xdnf.cn/news/1298755.html

相关文章:

  • 加密货币交易所开发:如何打造安全、高并发的数字资产交易平台?
  • 开源安全云盘存储:Hoodik 实现端到端数据加密,Docker快速搭建
  • 深度学习-卷积神经网络CNN-膨胀卷积、可分离卷积(空间可分离、深度可分离)、分组卷积
  • 【保姆级教程】CentOS 7 部署 FastDFS 全流程(避坑指南)
  • 具身智能Scaling Law缺失:机器人界的“摩尔定律“何时诞生?
  • CVPR 2025 | 机器人操控 | RoboGround:用“掩码”中介表示,让机器人跨场景泛化更聪明
  • 3D商品展示:技术狂欢下的普及困局
  • DataHub OPC Gateway:实现OPC UA与OPC DA无缝集成的高性能网关
  • 【代码随想录day 20】 力扣 108.将有序数组转换为二叉搜索树
  • 京东【自主售后】物流信息获取_影刀RPA源码解读
  • Docker部署美化SunPanel导航页
  • python+vue扫盲
  • anaconda创建pytorch1.10.0和pytorch2.0.0的GPU环境
  • java:创建指定容器类型(如ImmutableSet)的Collector对象
  • Redis (REmote DIctionary Server) 高性能数据库
  • 设计模式笔记_行为型_状态模式
  • OpenAI 的浏览器将使用 ChatGPT Agent 来控制浏览器
  • 记录一些奇奇怪怪的面试题
  • 【慕伏白】CTFHub 技能树学习笔记 -- 基础知识 签到
  • AI 编程实践:用 Trae 快速开发 HTML 贪吃蛇游戏
  • 【软考中级网络工程师】知识点之常用网络诊断和配置命令
  • 机器学习核心概念与实践笔记
  • 解刨HashMap的put流程 <二> JDK 1.8
  • Redis 03 redis 缓存异常
  • Oracle commit之后做了什么
  • OS设备UDID查看方法
  • word——删除最后一页空白页
  • centos部署chrome和chromedriver
  • 【C++】细说继承(2w字详解)
  • OpenCV对椒盐处理后的视频进行均值滤波处理