一、
由于微信个人订阅号目前很多功能还没开通,所以采用微信公众平台测试帐号,
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
用微信扫码登录就可以获得一个自己的微信测试号:
扫码关注测试账号:
各种功能接口文档都有提供,可根据需求自行查看。
如果要在线调试接口,需要自己申请一个订阅号,登陆之后,点击开发者工具->在线调试工具
二、
上面的准备工作做完了,就可以开始调用接口实现功能了。
#coding:utf-8 import time import datetime import os import ConfigParser import requests import json class MsgToWeiXin(object): def __init__(self): self._config = ConfigParser.SafeConfigParser() # self._config.read('config.ini') batRunningPath = os.path.dirname(os.sys.executable) batPath = os.path.dirname(os.sys.path[0]) if os.path.exists(batPath + "MsgToWeiXinconfig.ini"): self._config.read(batPath + "MsgToWeiXinconfig.ini") else: self._config.read(batRunningPath + "config.ini") self._access_token = "" self._expires_in = 7200 self._token_getTime = 0 self._appid = self._config.get("weixin", "appid") self._secret = self._config.get("weixin", "secret") self._touser = self._config.get("weixin", "touser") self._togroup = self._config.get("weixin", "groupid") datenow = datetime.datetime.now() datenow = datenow.strftime("%Y%m%d") self._nToDay = datenow self._msg = "" def getMsgFromTxt(self): datenow = datetime.datetime.now() datenow = datenow.strftime("%Y%m%d") strFileName = str(datenow) + "_IndexServerInterface.txt" print strFileName try: for line in open(strFileName): if line != " ": self._msg += line return True except Exception, e: print str(e) return False def get_token(self): url = 'https://api.weixin.qq.com/cgi-bin/token' values = { 'grant_type':'client_credential', 'appid' : self._appid , 'secret':self._secret, } req = requests.get(url, params=values) data = req.json() self._token_getTime = time.time() self._access_token = data["access_token"] self._expires_in = data["expires_in"] def send_msg(self): url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + self._access_token content = "Date:{0}, {1} ".format(self._nToDay,self._msg) values = { "touser" : self._touser , "msgtype":"text", "text":{ "content": content } } #header={"content-Type":"application/json"} #req = requests.post(url, data=json.dumps(values),headers=header) req = requests.post(url, data=json.dumps(values, ensure_ascii=False)) result = req.json() return result['errcode'] def send_msg_group(self): url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=" + self._access_token content = "Date:{0}, {1} ".format(self._nToDay,self._msg) values = { "filter" : { "is_to_all":False, "group_id":self._togroup, }, "msgtype":"text", "text":{ "content": content } } #header={"content-Type":"application/json"} #req = requests.post(url, data=json.dumps(values),headers=header) req = requests.post(url, data=json.dumps(values, ensure_ascii=False)) result = req.json() return result['errcode'] if __name__ == '__main__': msgToWX = MsgToWeiXin() if True == msgToWX.getMsgFromTxt(): if msgToWX._msg != "": print msgToWX._msg else: msgToWX._msg += "指标数据正常" msgToWX.get_token() if 0 == msgToWX.send_msg(): print "send success" else: print "send failed" else: print "getMsgFromTxt error"
Linux下报错:ImportError: No module named 'requests.packages.urllib3'