Telegram Bot机器人通知 加入配置

This commit is contained in:
midoks
2023-03-06 19:32:36 +08:00
parent 51ccbefb04
commit 64d1387b03
5 changed files with 176 additions and 3 deletions
+58
View File
@@ -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
+42
View File
@@ -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():