python 实现微信模板消息发送
发布时间:2020-05-25 00:59:35 所属栏目:Python 来源:互联网
导读:python 实现微信模板消息发送
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #WechatPush.py
# encoding: utf-8
import urllib2,json
class WechatPush(object):
def __init__(self,appid,secrect):
self.appid = appid
self.secrect = secrect
#获取accessToken
def getToken(self):
#判断缓存
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+self.appid + "&secret="+self.secrect
f = urllib2.urlopen(url)
s = f.read()
#读取json数据
j = json.loads(s)
j.keys()
token = j['access_token']
return token
#开始推送
def do_push(self,touser,template_id,url,data,topcolor):
if topcolor.strip()=='':
topcolor = "#7B68EE"
dict_arr = {'touser': touser,'template_id':template_id,'url':url,'topcolor':topcolor,'data':data}
json_template = json.dumps(dict_arr)
token = self.getToken()
requst_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token
content = self.post_data(requst_url,json_template)
#读取json数据
j = json.loads(content)
j.keys()
errcode = j['errcode']
errmsg = j['errmsg']
return errmsg
#模拟post请求
def post_data(self,para_dct):
para_data = para_dct
f = urllib2.urlopen(url,para_data)
content = f.read()
return content
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
