#!/usr/bin/env python
#encoding=utf-8
import sys
import urllib2
import json
AppID = "xxxxxxxxxxxxxxxxxxxxxxx"
AppSecret = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
def get_token():
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (AppID, AppSecret)
result = urllib2.urlopen(url).read()
access_token = json.loads(result).get('access_token')
return access_token
if __name__ == "__main__":
#print get_token()
#exit(0)
#access_token = "EZqV_wXpDhMWZCMqahLU6yPJAHecVTZJkBkMpvmYc2A7OIUDSfIZeoLlCdC-RhDjwD5USBXdSpqTBl58ZH1LGhrxU3TlX3UpbTA396yV1xU8_N7Yjpv7NNIMm3VSCReCAGAiAHAPVP"
access_token = get_token()
url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token
template_id = "6lcl84tZxUFmBBbjcQzx42vg2U302paedAodBgUhWLc"
openids = ['oVWPisklk1ozqgs-P__7JIFHEXUE'] //直接指定,也可以从数据库读取
for o in openids:
message = {
"touser":o,
"template_id":template_id,
"url":"http://www.xxxxxxxxxx.com",
"data":{
"first": {
"value":"你好,即将有新课程",
"color":"#f52212"
},
"keyword1":{
"value":"《xxxxxxxxxxxxx》",
"color":"#173177"
},
"keyword2": {
"value":"xxxxxxxxxxxxxxxxxx",
"color":"#173177"
},
"keyword3":{
"value":"xxxxxxxxxxxxxxxxx",
"color":"#173177"
},
"keyword4":{
"value":"2016年6月22日 20:00",
"color":"#173177"
},
"remark": {
"value":"如有问题请直接在微信留言,我们将尽快给予您回复!",
"color":"#173177"
}
}
}
req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
req.add_header('encoding', 'utf-8')
response = urllib2.urlopen(req, json.dumps(message,ensure_ascii=False))
result = response.read()
print result