当前位置: 首页 > 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.dtcms.com/a/331721.html

相关文章:

  • 字节跳动 VeOmni 框架开源:统一多模态训练效率飞跃!
  • JAVA 抽象类可以实例化吗
  • 机器学习概述(一)
  • Spring Cloud系列—Alibaba Sentinel熔断降级
  • 第一章 随机事件与概率
  • 前端性能优化移动端网页滚动卡顿与掉帧问题实战
  • 前端开发常见问题及解决方案全解析
  • 解剖HashMap的put流程 <一> (JDK 1.8)
  • 22.Linux samba服务
  • USB 3.0 link command 定义
  • 知识的本质
  • 数域筛法GNFS---C语言实现
  • 20道CSS相关前端面试题及答案
  • Elasticsearch:如何使用 Qwen3 来做向量搜索
  • css中container和media的用法和区别
  • SRWare Iron:隐私保护与高效浏览的完美结合
  • C++ mutex的实现源码分析
  • Xsens动作捕捉与AI驱动人形机器人训练革新
  • WVP和ZLM部署与接入NVR指南环境准备
  • 【React】hooks 中的闭包陷阱
  • 三轴云台之脉宽调制技术篇
  • Qt基本槽
  • 链游(GameFi)开发破局:如何平衡可玩性与经济模型可持续性?
  • GraphRAG:AI理解复杂知识的未知领域,开启探索之旅
  • 《Python函数:从入门到精通,一文掌握函数编程精髓》
  • MySQL主从原理
  • Linux 文件系统简介
  • 解析 TrueType/OpenType 格式的可变字体(Variable Font),提取其所有命名实例(Named Instances) 的名称信息
  • ESP32S3的LVGL配置参数解释、动画播放优化(更新中)
  • 4.1vue3的setup()