From 123993c3cc0ea3d9ce0bcd99ef51fc082fbcba75 Mon Sep 17 00:00:00 2001 From: urinx <1336006643@qq.com> Date: Thu, 25 May 2017 16:21:48 +0800 Subject: [PATCH] + when bot fail then send mail --- wxbot_project_py2.7/config/requirements.txt | 1 + wxbot_project_py2.7/config/wechat.conf.bak | 5 ++++ wxbot_project_py2.7/wechat/utils.py | 6 ++++ wxbot_project_py2.7/wechat/wechat.py | 1 + wxbot_project_py2.7/wechat/wechat_apis.py | 7 +++-- wxbot_project_py2.7/weixin_bot.py | 13 +++++++++ wxbot_project_py2.7/wx_handler/__init__.py | 3 +- .../wx_handler/sendgrid_mail.py | 20 +++++++++++++ .../wx_handler/wechat_msg_processor.py | 28 +++++++++++-------- 9 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 wxbot_project_py2.7/wx_handler/sendgrid_mail.py diff --git a/wxbot_project_py2.7/config/requirements.txt b/wxbot_project_py2.7/config/requirements.txt index b0cf8fe..a0597d7 100644 --- a/wxbot_project_py2.7/config/requirements.txt +++ b/wxbot_project_py2.7/config/requirements.txt @@ -3,3 +3,4 @@ flask requests requests_toolbelt pymysql +sendgrid diff --git a/wxbot_project_py2.7/config/wechat.conf.bak b/wxbot_project_py2.7/config/wechat.conf.bak index 2dba3e0..31ac260 100644 --- a/wxbot_project_py2.7/config/wechat.conf.bak +++ b/wxbot_project_py2.7/config/wechat.conf.bak @@ -35,6 +35,11 @@ user = root passwd = root database = wechat +[sendgrid] +api_key = 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 +from_email = wxbot@wechat.com +to_email = xxx@example.com + [loggers] keys = root,WeChat diff --git a/wxbot_project_py2.7/wechat/utils.py b/wxbot_project_py2.7/wechat/utils.py index 06b8d48..63c86bc 100644 --- a/wxbot_project_py2.7/wechat/utils.py +++ b/wxbot_project_py2.7/wechat/utils.py @@ -231,6 +231,12 @@ def save_json(filename, data, dirName, mode='w+'): return fn +def load_json(filepath): + Log.debug('load json: ' + filepath) + with open(filepath, 'r') as f: + return _decode_data(json.loads(f.read())) + + def pickle_save(data, file): """ @brief Use pickle to save python object into file diff --git a/wxbot_project_py2.7/wechat/wechat.py b/wxbot_project_py2.7/wechat/wechat.py index b68ab7f..81b2001 100644 --- a/wxbot_project_py2.7/wechat/wechat.py +++ b/wxbot_project_py2.7/wechat/wechat.py @@ -34,6 +34,7 @@ class WeChat(WXAPI): "[#] Skey: " + self.skey + "\n" + \ "[#] DeviceId: " + self.device_id + "\n" + \ "[#] PassTicket: " + self.pass_ticket + "\n" + \ + "[#] Run Time: " + self.get_run_time() + '\n' + \ "=========================" return description diff --git a/wxbot_project_py2.7/wechat/wechat_apis.py b/wxbot_project_py2.7/wechat/wechat_apis.py index 84b84e7..fa75db5 100644 --- a/wxbot_project_py2.7/wechat/wechat_apis.py +++ b/wxbot_project_py2.7/wechat/wechat_apis.py @@ -767,8 +767,11 @@ class WXAPI(object): @param text String @return Bool: whether operation succeed """ - dic = self.webwxsendmsg(text, user_id) - return dic['BaseResponse']['Ret'] == 0 + try: + dic = self.webwxsendmsg(text, user_id) + return dic['BaseResponse']['Ret'] == 0 + except: + return False def send_img(self, user_id, file_path): """ diff --git a/wxbot_project_py2.7/weixin_bot.py b/wxbot_project_py2.7/weixin_bot.py index 88a489b..ec7a53b 100755 --- a/wxbot_project_py2.7/weixin_bot.py +++ b/wxbot_project_py2.7/weixin_bot.py @@ -6,6 +6,7 @@ from wechat import WeChat from wechat.utils import * from wx_handler import WeChatMsgProcessor from wx_handler import Bot +from wx_handler import SGMail from db import SqliteDB from db import MysqlDB from config import ConfigManager @@ -44,6 +45,11 @@ flask_log_handler.setFormatter(formatter) logger.addHandler(flask_log_handler) app.logger.addHandler(flask_log_handler) +# sendgrid mail +sg_apikey = cm.get('sendgrid', 'api_key') +from_email = cm.get('sendgrid', 'from_email') +to_email = cm.get('sendgrid', 'to_email') +sg = SGMail(sg_apikey, from_email, to_email) @app.route('/') def index(): @@ -260,6 +266,13 @@ while True: Log.error(traceback.format_exc()) finally: wechat.stop() + + # send a mail to tell the wxbot is failing + subject = 'wxbot stop message' + log_file = open(eval(cm.get('handler_fileHandler', 'args'))[0], 'r') + mail_content = '
' + str(wechat) + '\n\n-----\nLogs:\n-----\n\n' + ''.join(log_file.readlines()[-100:]) + '' + sg.send_mail(subject, mail_content, 'text/html') + log_file.close() if wechat.exit_code == 0: echo(Constant.MAIN_RESTART) diff --git a/wxbot_project_py2.7/wx_handler/__init__.py b/wxbot_project_py2.7/wx_handler/__init__.py index 19c28f2..c13710e 100644 --- a/wxbot_project_py2.7/wx_handler/__init__.py +++ b/wxbot_project_py2.7/wx_handler/__init__.py @@ -2,4 +2,5 @@ # coding: utf-8 from wechat_msg_processor import WeChatMsgProcessor -from bot import Bot \ No newline at end of file +from bot import Bot +from sendgrid_mail import SGMail \ No newline at end of file diff --git a/wxbot_project_py2.7/wx_handler/sendgrid_mail.py b/wxbot_project_py2.7/wx_handler/sendgrid_mail.py new file mode 100644 index 0000000..58d4b39 --- /dev/null +++ b/wxbot_project_py2.7/wx_handler/sendgrid_mail.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# coding: utf-8 + +#=================================================== +import sendgrid +from sendgrid.helpers.mail import * +#=================================================== + +class SGMail(object): + + def __init__(self, apikey, from_email, to_email): + self.sg = sendgrid.SendGridAPIClient(apikey=apikey) + self.from_email = Email(from_email) + self.to_email = Email(to_email) + + def send_mail(self, subject, plain_content, type='text/plain'): + content = Content(type, plain_content) + mail = Mail(self.from_email, subject, self.to_email, content) + response = self.sg.client.mail.send.post(request_body=mail.get()) + return response.status_code == 202 diff --git a/wxbot_project_py2.7/wx_handler/wechat_msg_processor.py b/wxbot_project_py2.7/wx_handler/wechat_msg_processor.py index d70f54b..4faccb3 100644 --- a/wxbot_project_py2.7/wx_handler/wechat_msg_processor.py +++ b/wxbot_project_py2.7/wx_handler/wechat_msg_processor.py @@ -195,6 +195,8 @@ class WeChatMsgProcessor(object): if text == 'test_revoke': # 撤回消息测试 dic = wechat.webwxsendmsg('这条消息将被撤回', uid) wechat.revoke_msg(dic['MsgID'], uid, dic['LocalID']) + elif text == 'reply': + wechat.send_text(uid, '自动回复') def handle_command(self, cmd, msg): @@ -210,20 +212,22 @@ class WeChatMsgProcessor(object): g_id = g['UserName'] cmd = cmd.strip() - if cmd == 'test': - # wechat.send_file(g_id, 'test/Data/upload/shake.wav') + if cmd == 'runtime': + wechat.send_text(g_id, wechat.get_run_time()) + elif cmd == 'test_sendimg': wechat.send_img(g_id, 'test/emotion/7.gif') - elif cmd == 'runtime': - wechat.webwxsendmsg(wechat.get_run_time(), g_id) - else: + elif cmd == 'test_sendfile': + wechat.send_file(g_id, 'test/Data/upload/shake.wav') + elif cmd == 'test_bot': # reply bot # --------- - # if wechat.bot: - # r = wechat.bot.reply(cmd) - # if r: - # wechat.webwxsendmsg(r, g_id) - # else: - # pass + if wechat.bot: + r = wechat.bot.reply(cmd) + if r: + wechat.send_text(g_id, r) + else: + pass + elif cmd == 'test_emot': img_name = [ '0.jpg', '1.jpeg', '2.gif', '3.jpg', '4.jpeg', '5.gif', '6.gif', '7.gif', '8.jpg', '9.jpg' @@ -231,6 +235,8 @@ class WeChatMsgProcessor(object): name = img_name[int(time.time()) % 10] emot_path = os.path.join('test/emotion/', name) wechat.send_emot(g_id, emot_path) + else: + pass def check_schedule_task(self): # update group member list at 00:00 am every morning