溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python如何實(shí)現(xiàn)微信推送模板消息功能

發(fā)布時(shí)間:2022-02-23 10:24:05 來(lái)源:億速云 閱讀:398 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python如何實(shí)現(xiàn)微信推送模板消息功能的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Python如何實(shí)現(xiàn)微信推送模板消息功能文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

具體代碼如下:

#!/usr/bin/env python

#-*- coding: utf-8 -*-

import httplib

import json

import MySQLdb

#從數(shù)據(jù)庫(kù)中獲取access_token

access_token=""

try:

conn=MySQLdb.connect(host='192.168.1.1',user='root',passwd='root',db='db_weixin',port=3306)

cur=conn.cursor()

cur.execute('select access_token from weixin_public')

result=cur.fetchone()

#print result

#print result[0]

access_token=result[0]

cur.close()

conn.close()

except MySQLdb.Error,e:

print "Mysql Error %d: %s" % (e.args[0], e.args[1])

#根據(jù)接口推送消息

if not access_token is None:

conn = httplib.HTTPConnection("api.weixin.qq.com:80")#微信接口鏈接

headers = {"Content-type":"application/json"} #application/x-www-form-urlencoded

params = ({'touser' : "oEGZ4johnKOtayJbnEVeuaZr6zQ0",#用戶openid

'template_id' : 'AtFuydv8k_15UGZuFntaBzJRCsHCkjNm1dcWD3A-11Y',#模板消息ID

'url' : 'https://www.jb51.net',#跳轉(zhuǎn)鏈接

"topcolor" : "#667F00",#顏色

"data" : {#模板內(nèi)容

"first" : {"value" : "尊敬的710.so : 您的網(wǎng)站https://www.jb51.net (192.168.1.1) 有異常訪問(wèn)", "color" : "#173177"},

"keyword1" : {"value" : "訪問(wèn)時(shí)間 2015-04-05 15:30:59 訪問(wèn)IP 192.168.1.2", "color" : "#173177"},

"keyword2" : {"value" : "訪問(wèn)鏈接 https://www.jb51.net", "color" : "#173177"},

"remark" : {"value" : "訪問(wèn)頻率 10/s", "color" : "#173177"}

}

}

)

conn.request("POST", "/cgi-bin/message/template/send?access_token="+access_token, json.JSONEncoder().encode(params), headers)#推送消息請(qǐng)求

response = conn.getresponse()

data = response.read()#推送返回?cái)?shù)據(jù)

if response.status == 200:

print 'success'

print data

else:

print 'fail'

conn.close()

關(guān)于“Python如何實(shí)現(xiàn)微信推送模板消息功能”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Python如何實(shí)現(xiàn)微信推送模板消息功能”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI