要实现对微信公众好粉丝消息的推送,首先要知道推送粉丝的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 res
except 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 openid
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()
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 res
except 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)