diff --git a/class/core/config_api.py b/class/core/config_api.py index 185bc7ba1..a09f865ec 100755 --- a/class/core/config_api.py +++ b/class/core/config_api.py @@ -661,6 +661,54 @@ class config_api: mw.writeLog('面板设置', '将未授权响应状态码设置为:{}'.format(status_code)) return mw.returnJson(True, '设置成功!') + def getNotifyApi(self): + # 获取 + data = mw.getNotifyData() + return mw.returnData(True, 'ok', data) + + def setNotifyApi(self): + tag = request.form.get('tag', '').strip() + tag_data = request.form.get('data', '').strip() + + data = mw.getNotifyData() + data[tag] = json.loads(tag_data) + + mw.writeNotify(data) + return mw.returnData(True, '设置成功') + + def setNotifyTestApi(self): + # 异步通知验证 + tag = request.form.get('tag', '').strip() + tag_data = request.form.get('data', '').strip() + + if tag == 'tgbot': + t = json.loads(tag_data) + test_bool = mw.tgbotNotifyTest(t['app_token'], t['chat_id']) + if test_bool: + return mw.returnData(True, '验证成功') + return mw.returnData(False, '验证失败') + + return mw.returnData(False, '暂时未支持该验证') + + def setNotifyEnableApi(self): + # 异步通知验证 + tag = request.form.get('tag', '').strip() + tag_enable = request.form.get('enable', '').strip() + + data = mw.getNotifyData() + op_enable = True + op_action = '开启' + if tag_enable != 'true': + op_enable = False + op_action = '关闭' + + if tag in data: + data[tag]['enable'] = op_enable + + mw.writeNotify(data) + + return mw.returnData(True, op_action + '成功') + def getPanelTokenApi(self): api_file = self.__api_addr tmp = mw.readFile(api_file) @@ -853,4 +901,14 @@ class config_api: else: data['hook_global_static'] = [] + # notiy config + notify_data = mw.getNotifyData() + notify_tag_list = ['tgbot', 'email'] + for tag in notify_tag_list: + new_tag = 'notify_' + tag + '_enable' + data[new_tag] = '' + if tag in notify_data and 'enable' in notify_data[tag]: + if notify_data[tag]['enable']: + data[new_tag] = 'checked' + return data diff --git a/class/core/mw.py b/class/core/mw.py index 8e4bb0e99..1deae6be0 100755 --- a/class/core/mw.py +++ b/class/core/mw.py @@ -1662,6 +1662,48 @@ def getMyORMDb(): o = ormDb.ORM() return o +##################### notify start ######################################### + + +def initNotifyConfig(): + p = getNotifyPath() + if not os.path.exists(p): + writeFile(p, '{}') + return True + + +def getNotifyPath(): + path = 'data/notify.json' + return path + + +def getNotifyData(): + initNotifyConfig() + p = getNotifyPath() + t = readFile(p) + return json.loads(t) + + +def writeNotify(data): + p = getNotifyPath() + return writeFile(p, json.dumps(data)) + + +def tgbotNotifyTest(app_token, chat_id): + import telebot + bot = telebot.TeleBot(app_token) + + try: + data = bot.send_message(chat_id, "MW-通知验证测试OK") + # print(data) + return True + except Exception as e: + pass + return False + + +##################### notify end ######################################### + ##################### ssh start ######################################### def getSshDir(): diff --git a/data/notify.json b/data/notify.json new file mode 100644 index 000000000..8de1fa909 --- /dev/null +++ b/data/notify.json @@ -0,0 +1 @@ +{"tgbot": {"app_token": "5928900966:AAEqs-pIDfai-7rS_cr1R4igsInaXd1OANs", "chat_id": "5568699210", "enable": true}} \ No newline at end of file diff --git a/route/static/app/config.js b/route/static/app/config.js index 83a8a8c67..b4b9c899c 100755 --- a/route/static/app/config.js +++ b/route/static/app/config.js @@ -354,6 +354,78 @@ function setPanelSSL(){ }); } +function setNotifyApi(tag, obj){ + var enable = $(obj).prop("checked"); + console.log(tag,obj,enable); + $.post('/config/set_notify_enable', {'tag':tag, 'enable':enable},function(rdata){ + showMsg(rdata.msg, function(){ + if (rdata.status){} + } ,{icon:rdata.status?1:2}, 1000); + },'json'); +} + +function getTgbot(){ + var loadT = layer.msg('正在获取TgBot信息...',{icon:16,time:0,shade: [0.3, '#000']}); + $.post('/config/get_notify',{},function(data){ + layer.close(loadT); + + var app_token = ''; + var chat_id = ''; + + if (data.status){ + if (typeof(data['data']['tgbot']) !='undefined'){ + app_token = data['data']['tgbot']['app_token']; + chat_id = data['data']['tgbot']['chat_id']; + } + } + + layer.open({ + type: 1, + area: "500px", + title: '配置TgBot配置', + closeBtn: 1, + shift: 5, + btn:["确定","关闭","验证"], + shadeClose: false, + content: "
\ +
\ + APP_TOKEN\ +
\ +
\ +
\ + CHAT_ID\ +
\ +
\ +
", + yes:function(index){ + var pdata = {}; + pdata['app_token'] = $('input[name="app_token"]').val(); + pdata['chat_id'] = $('input[name="chat_id"]').val(); + $.post('/config/set_notify',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){ + showMsg(rdata.msg, function(){ + if (rdata.status){ + layer.close(index); + } + },{icon:rdata.status?1:2},2000); + }); + }, + + btn3:function(index){ + var pdata = {}; + pdata['app_token'] = $('input[name="app_token"]').val(); + pdata['chat_id'] = $('input[name="chat_id"]').val(); + $.post('/config/set_notify_test',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){ + showMsg(rdata.msg, function(){ + if (rdata.status){ + layer.close(index); + } + },{icon:rdata.status?1:2},2000); + }); + return false; + } + }); + }); +} function getPanelSSL(){ var loadT = layer.msg('正在获取证书信息...',{icon:16,time:0,shade: [0.3, '#000']}); diff --git a/route/templates/default/config.html b/route/templates/default/config.html index a6c5f2f38..34d10050f 100755 --- a/route/templates/default/config.html +++ b/route/templates/default/config.html @@ -155,9 +155,9 @@

TG机器人通知 - - - + + + Telegram Bot机器人通知