当前位置: 首页 > 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.lryc.cn/news/621010.html

相关文章:

  • ActionChains 鼠标操作笔记
  • 恐鬼症 单机+联机(Phasmophobia)免安装中文版
  • SQL181 第二快/慢用时之差大于试卷时长一半的试卷
  • 【昇腾】VirtualBox虚拟机下搭建Ubuntu 22.04环境给TF卡制卡报读写IO错误的问题处理_20250814
  • 自动化测试|持续集成Git使用详解
  • elasticsearch冷热数据读写分离!
  • 快速搭建python HTTP Server测试环境
  • gitlab的ci/cd变量如何批量添加
  • STL算法【常用的算数生成算法】
  • 分享10个ai生成ppt网站(附ai生成ppt入口)
  • 力扣top100(day03-02)--图论
  • 回流(Reflow)与重绘(Repaint):浏览器渲染性能优化核心
  • SpringMVC请求与响应
  • 【Linux】库制作与原理
  • PyTorch回忆(三)U-net
  • java 学习 贪心 + 若依 + 一些任务工作
  • FTP服务器搭建(Linux)
  • opencv:傅里叶变换有什么用?怎么写傅里叶变换?
  • 软件著作权产生与登记关键点
  • 从单机到分布式:用飞算JavaAI构建可扩展的TCP多人聊天系统
  • 算法基础 第3章 数据结构
  • 数学建模-非线性规划模型
  • 深入理解提示词工程:从入门到精通的AI对话艺术
  • Mybatis实现页面增删改查
  • 数仓分层架构设计全解析:从理论到实践的深度思考
  • 一台联想 ThinkCentre M7100z一体机开机黑屏无显示维修记录
  • 【跨越 6G 安全、防御与智能协作:从APT检测到多模态通信再到AI代理语言革命】
  • 解决“Win7共享文件夹其他电脑网络无法发现共享电脑名称”的问题
  • 机器视觉之图像处理篇
  • c/c++ UNIX 域Socket和共享内存实现本机通信