python 實現公眾號模板訊息推送(親測有效)

龙岩龙發表於2024-05-22

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": "張三"
    }
}

}))

相關文章