mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-10 01:29:26 +08:00
Telegram Bot机器人通知 加入配置
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"tgbot": {"app_token": "5928900966:AAEqs-pIDfai-7rS_cr1R4igsInaXd1OANs", "chat_id": "5568699210", "enable": true}}
|
||||
@@ -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: "<div class='bt-form pd20'>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>APP_TOKEN</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='text' name='app_token' value='"+app_token+"' style='width:100%'/></div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>CHAT_ID</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='text' name='chat_id' value='"+chat_id+"' style='width:100%' /></div>\
|
||||
</div>\
|
||||
</div>",
|
||||
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']});
|
||||
|
||||
@@ -155,9 +155,9 @@
|
||||
|
||||
<p class="mtb15">
|
||||
<span class="set-tit text-right" title="TelegramBot" style="float: left;">TG机器人通知</span>
|
||||
<input id="cfg_ssl" name="bind_ssl" class="btswitch btswitch-ios" type="checkbox" {{data['ssl']}}>
|
||||
<label class="btswitch-btn ml5" for="cfg_ssl" style="float: left;margin-top:4px;"></label>
|
||||
<button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getPanelSSL();">TelegramBot配置</button>
|
||||
<input id="cfg_tgbot" name="cfg_tgbot" onclick="setNotifyApi('tgbot',this)" class="btswitch btswitch-ios" type="checkbox" {{data['notify_tgbot_enable']}}>
|
||||
<label class="btswitch-btn ml5" for="cfg_tgbot" style="float: left;margin-top:4px;"></label>
|
||||
<button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getTgbot();">TelegramBot配置</button>
|
||||
<span class="set-info c7">Telegram Bot机器人通知</span>
|
||||
</p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user