200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 使用python+微信发送消息提醒 实现程序监控

使用python+微信发送消息提醒 实现程序监控

时间:2020-08-08 04:49:19

相关推荐

使用python+微信发送消息提醒 实现程序监控

使用python+微信发送消息提醒,实现程序监控

使用python+微信可以非常方便的提醒自己运行的程序是否报错,监控程序运行状态

1.申请微信测试号

https://mp./debug/cgi-bin/sandbox?t=sandbox/login

申请好后会得到appIDappsecret,之后会用到

userid为关注公众号的用户id (扫码关注后随便发送一条消息)

template_id为模板id(新增测试模板后会得到一个id)

模板如下所示,准备好上面的appIDappsecretuseridtemplate_id就可以开始运行程序了

2.python程序

你可以不用复制直接从github上下载: /yyh-001/sendwx

把要下载好的程序(sendwx.py)和要运行的python程序放在一个同一个文件夹内,直接开始第三步

你也可以新建一个python文件,将代码复制到文件,我这边保存为sendwx.py

import requestsimport jsonimport osdef get_token(appID,appsecret):url_token = 'https://api./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 configelse: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}res1 = json.dumps(res)f.write(str(res1))f.close()return res#写入配置文件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./cgi-bin/message/custom/send?'body = {"touser": userID, # 这里必须是关注公众号测试账号后的用户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 resdef sendmb(token,template_id,userID,text,color):url_msg = 'https://api./cgi-bin/message/template/send?'body = {"touser": userID, # 这里必须是关注公众号测试账号后的用户id"template_id": template_id,"url": "/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 resdef send(text):config = rdconfig('token.json')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('程序开始'))# 发送模板消息# sendmb(token,template_id,userID,'程序错误,请及时回复','#FF0000')

3.实现程序监控,发送消息提醒

运行程序,程序第一次执行会初始化配置文件,将之前获得的appIDappsecretuseridtemplate_id依次填入控制台,配置文件会保存在本地的【token.json】中,第一次配置之后就不用管了,程序会自动更新token,只需要在别的程序导入这个包既可

from sendwx import send #我之前保存的是sendwx.pysend('这里填入你要发送的消息即可')

【示例】程序报错监控

from sendwx import send #我之前保存的是sendwx.pytry:#里面写你要监控的程序except Exception as e:send('程序异常\n' + str(e))

如果微信接收不到可能是折叠在公众号内部,只需要打开微信-设置-新消息提醒-新消息系统通知-将其他通知的允许通知打开即可

最近在学django,可以的话弄一个部署到服务器会更加方便

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。