程序如下
import requests
import json
import os
def get_token(appID,appsecret):
url_token = 'https://api.weixin.qq.com/cgi-bin/token?'
res = requests.get(url=url_token,params={
"grant_type": 'client_credential',
'appid':appID,
'secret':appsecret,
}).json()
# print(res)
token = res.get('access_token')
# print(res)
return token
#读取配置文件
def rdconfig(filename):
if (os.path.exists(filename)):
with open(filename,"r") as f:
config = json.load(f)
# print("读取配置文件完成...")
return config
else:
print('初始化配置文件')
with open(filename, "w") as f:
appID = input('appID:')
appsecret = input('appsecret:')
userid = input('userid:')
template_id = input('template_id:')
token = get_token(appID, appsecret)
res = {"appID":appID,"appsecret":appsecret,"token":token,"userid":userid,"template_id":template_id}
res = json.dumps(res)
f.write(str(res))
f.close()
#写入配置文件
def wrconfig(new_dict,filename):
with open(filename, "w") as f:
json.dump(new_dict, f)
# print("写入配置文件完成...")
def sendtext(token,userID,text):
url_msg = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?'
body = {
"touser": "xxx", # 这里必须是关注公众号测试账号后的用户id
"msgtype": "text",
"text": {
"content": text
}
}
res = requests.post(url=url_msg, params={
'access_token': token # 这里是我们上面获取到的token
}, data=json.dumps(body, ensure_ascii=False).encode('utf-8'))
return res
def sendmb(token,template_id,userID,text,color):
url_msg = 'https://api.weixin.qq.com/cgi-bin/message/template/send?'
body = {
"touser": "xxx", # 这里必须是关注公众号测试账号后的用户id
"template_id": template_id,
"url": "http://weixin.qq.com/download",
"topcolor": color,
"data": {
"text":{
"value": text,
"color": color
}
}
}
res = requests.post(url=url_msg, params={
'access_token': token # 这里是我们上面获取到的token
}, data=json.dumps(body, ensure_ascii=False).encode('utf-8'))
return res
def send(text):
config = rdconfig('token.json')
print(config)
userID = config['userid']
template_id =config['template_id']
res = sendtext(config['token'],userID,text)
if(res.json()['errcode']==42001): # token两小时过期后重新获取
config['token']=get_token(config['appID'],config['appsecret'])
wrconfig(config,'token.json')
res = sendtext(config['token'],userID,text)
if(res.json()['errcode']==45047): # 客服消息如果长时间不回复将不能发,这边先换成模板消息
res = sendmb(config['token'],template_id,userID,text+'(请及时回复以免消息发送失败)','#FF0000')
return res.json()
# print(send('程序开始'))
上述代码运行后需要appID ,appsecret ,userid, template_id依次填入控制台,然后生成一个josn,之后就不用管了。
appID ,appsecret ,userid, template_id在:
申请好后会得到 applD 和 appsecret
userid 为关注公众号的用户id(扫码关注后随便发送一条消息)
template_id 为模板id(新增测试模板后会得到一个id)
模板如下所示
后续只需要在其他程序调用:
# -*- coding: utf-8 -*-
from test import send # 我上面的程序文件名叫test
try:
#里面写要监控的程序
send('111')
except Exception as e:
send('程序异常\n', + str(e))
标签:微信,程序,json,发送,token,template,res,config,id
From: https://www.cnblogs.com/nanguyhz/p/18245420