Skip to content

Commit b47ab23

Browse files
committed
add en(dis)bling alerts
1 parent ffd8c41 commit b47ab23

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

modules/alert_bot.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import requests
44

55
from modules.module import MtcModule
6-
from mypylib.mypylib import get_timestamp
6+
from mypylib.mypylib import get_timestamp, print_table, color_print
77
from mytoncore import get_hostname
88
from mytonctrl.utils import timestamp2utcdatetime
99

@@ -99,6 +99,8 @@ def send_message(self, text: str):
9999
raise Exception(f"send_message error: {response}")
100100

101101
def send_alert(self, alert_name: str, *args, **kwargs):
102+
if not self.alert_is_enabled(alert_name):
103+
return
102104
last_sent = self.get_alert_sent(alert_name)
103105
time_ = timestamp2utcdatetime(int(time.time()))
104106
alert = ALERTS.get(alert_name)
@@ -136,15 +138,50 @@ def init(self):
136138
self.set_global_vars()
137139
self.inited = True
138140

139-
def set_alert_sent(self, alert_name: str):
141+
def get_alert_from_db(self, alert_name: str):
140142
if 'alerts' not in self.ton.local.db:
141143
self.ton.local.db['alerts'] = {}
142-
self.ton.local.db['alerts'][alert_name] = int(time.time())
144+
if alert_name not in self.ton.local.db['alerts']:
145+
self.ton.local.db['alerts'][alert_name] = {'sent': 0, 'enabled': True}
146+
return self.ton.local.db['alerts'][alert_name]
147+
148+
def set_alert_sent(self, alert_name: str):
149+
alert = self.get_alert_from_db(alert_name)
150+
alert['sent'] = int(time.time())
143151

144152
def get_alert_sent(self, alert_name: str):
145-
if 'alerts' not in self.ton.local.db:
146-
return 0
147-
return self.ton.local.db['alerts'].get(alert_name, 0)
153+
alert = self.get_alert_from_db(alert_name)
154+
return alert.get('sent', 0)
155+
156+
def alert_is_enabled(self, alert_name: str):
157+
alert = self.get_alert_from_db(alert_name)
158+
return alert.get('enabled', True) # default is True
159+
160+
def set_alert_enabled(self, alert_name: str, enabled: bool):
161+
alert = self.get_alert_from_db(alert_name)
162+
alert['enabled'] = enabled
163+
self.ton.local.save()
164+
165+
def enable_alert(self, args):
166+
if len(args) != 1:
167+
raise Exception("Usage: enable_alert <alert_name>")
168+
alert_name = args[0]
169+
self.set_alert_enabled(alert_name, True)
170+
color_print("enable_alert - {green}OK{endc}")
171+
172+
def disable_alert(self, args):
173+
if len(args) != 1:
174+
raise Exception("Usage: disable_alert <alert_name>")
175+
alert_name = args[0]
176+
self.set_alert_enabled(alert_name, False)
177+
color_print("disable_alert - {green}OK{endc}")
178+
179+
def print_alerts(self, args):
180+
table = [['Name', 'Enabled', 'Last sent']]
181+
for alert_name in ALERTS:
182+
alert = self.get_alert_from_db(alert_name)
183+
table.append([alert_name, alert['enabled'], alert['sent']])
184+
print_table(table)
148185

149186
def check_db_usage(self):
150187
usage = self.ton.GetDbUsage()
@@ -226,4 +263,6 @@ def check_status(self):
226263
self.local.try_function(self.check_adnl_connection_failed)
227264

228265
def add_console_commands(self, console):
229-
...
266+
console.AddItem("enable_alert", self.enable_alert, self.local.translate("enable_alert_cmd"))
267+
console.AddItem("disable_alert", self.disable_alert, self.local.translate("disable_alert_cmd"))
268+
console.AddItem("list_alerts", self.print_alerts, self.local.translate("list_alerts_cmd"))

mytoncore/mytoncore.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,6 +3089,9 @@ def using_validator(self):
30893089
def using_liteserver(self):
30903090
return self.get_mode_value('liteserver')
30913091

3092+
def using_alert_bot(self):
3093+
return self.get_mode_value('alert-bot')
3094+
30923095
def Tlb2Json(self, text):
30933096
# Заменить скобки
30943097
start = 0

mytonctrl/mytonctrl.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ def inject_globals(func):
135135
module = ControllerModule(ton, local)
136136
module.add_console_commands(console)
137137

138+
if ton.using_alert_bot():
139+
from modules.alert_bot import AlertBotModule
140+
module = AlertBotModule(ton, local)
141+
module.add_console_commands(console)
142+
138143
console.AddItem("cleanup", inject_globals(cleanup_validator_db), local.translate("cleanup_cmd"))
139144
console.AddItem("benchmark", inject_globals(run_benchmark), local.translate("benchmark_cmd"))
140145
# console.AddItem("activate_ton_storage_provider", inject_globals(activate_ton_storage_provider), local.translate("activate_ton_storage_provider_cmd"))

mytonctrl/resources/translate.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,21 @@
464464
"ru": "Удалить пользовательский оверлей",
465465
"zh_TW": "刪除自定義覆蓋"
466466
},
467+
"enable_alert_cmd": {
468+
"en": "Enable specific Telegram Bot alert",
469+
"ru": "Включить определенное оповещение через Telegram Bot",
470+
"zh_TW": "啟用特定的 Telegram Bot 警報"
471+
},
472+
"disable_alert_cmd": {
473+
"en": "Disable specific Telegram Bot alert",
474+
"ru": "Отключить определенное оповещение через Telegram Bot",
475+
"zh_TW": "禁用特定的 Telegram Bot 警報"
476+
},
477+
"list_alerts_cmd": {
478+
"en": "List all available Telegram Bot alerts",
479+
"ru": "Список всех доступных оповещений через Telegram Bot",
480+
"zh_TW": "列出所有可用的 Telegram Bot 警報"
481+
},
467482
"cleanup_cmd": {
468483
"en": "Clean node old logs and temp files",
469484
"ru": "Очистить старые логи и временные файлы ноды",

0 commit comments

Comments
 (0)