python 实现公众号模板消息推送(亲测有效)

``APPID = 'xxxxxxxxx'
APPSECRET = 'xxxxxxxxxxxxxx'
response = requests.get(
'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(APPID, APPSECRET)
)
ACCESS_TOKEN = response.json()
DD = ACCESS_TOKEN.get('access_token')
result = requests.post(url=r'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(DD), headers = {'Content-Type': 'application/json','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'}, data=
json.dumps({
"touser": "需要发送的用户的openid",
"template_id": "消息模板id",
"url": "https://weixin.qq.com/download",

下面的参数一定要按照自己公众号申请的模板的字段名称填写比如:"thing2","thing4"。

"data": {
    "thing2": {
        "value": "xxxxx会议通知"
    },
    "thing4": {
        "value": "xxx地址"
    },
    "time6": {
        "value": "2024年9月22日"
    },
    "time11": {
        "value": "2014年8月22日"
    },
    "thing7": {
        "value": "张三"
    }
}

}))

前提在公众号后台添加ip白名单

posted @   龙岩龙  阅读(304)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示