Skip to content

Commit c779d20

Browse files
committed
refactor bot setup
1 parent 99500eb commit c779d20

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

modules/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ class Setting:
5959
'defaultCustomOverlaysUrl': Setting(None, 'https://ton-blockchain.github.io/fallback_custom_overlays.json', 'Default custom overlays config url'),
6060
'debug': Setting(None, False, 'Debug mtc console mode. Prints Traceback on errors'),
6161
'subscribe_tg_channel': Setting('validator', False, 'Disables warning about subscribing to the `TON STATUS` channel'),
62-
'BotToken': Setting('alert-bot', None, 'Alerting Telegram bot token'),
63-
'ChatId': Setting('alert-bot', None, 'Alerting Telegram chat id'),
6462
'auto_backup': Setting('validator', None, 'Make validator backup every election'),
6563
'auto_backup_path': Setting('validator', '/tmp/mytoncore/auto_backups/', 'Path to store auto-backups'),
6664
'prometheus_url': Setting('prometheus', None, 'Prometheus pushgateway url'),

modules/alert_bot.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,22 @@ def test_alert(self, args):
256256
self.init()
257257
self.send_message('Test alert')
258258

259+
def setup_alert_bot(self, args):
260+
if len(args) != 2:
261+
raise Exception("Usage: setup_alert_bot <bot_token> <chat_id>")
262+
self.token = args[0]
263+
self.chat_id = args[1]
264+
init_alerts()
265+
try:
266+
self.send_welcome_message()
267+
self.ton.local.db['BotToken'] = args[0]
268+
self.ton.local.db['ChatId'] = args[1]
269+
color_print("setup_alert_bot - {green}OK{endc}")
270+
except Exception as e:
271+
self.local.add_log(f"Error while sending welcome message: {e}", "error")
272+
self.local.add_log(f"If you want the bot to write to a multi-person chat group, make sure the bot is added to that chat group. If it is not - do it and run the command `setup_alert_bot <bot_token> <chat_id>` again.", "info")
273+
color_print("setup_alert_bot - {red}Error{endc}")
274+
259275
def send_welcome_message(self):
260276
message = f"""
261277
This is alert bot. You have connected validator with ADNL <code>{self.ton.GetAdnlAddr()}</code>.
@@ -275,20 +291,6 @@ def send_welcome_message(self):
275291
"""
276292
self.send_message(text=message, disable_web_page_preview=True)
277293

278-
def on_set_chat_id(self, chat_id):
279-
self.token = self.ton.local.db.get("BotToken")
280-
if self.token is None:
281-
raise Exception("BotToken is not set")
282-
self.chat_id = chat_id
283-
init_alerts()
284-
try:
285-
self.send_welcome_message()
286-
return True
287-
except Exception as e:
288-
self.local.add_log(f"Error while sending welcome message: {e}", "error")
289-
self.local.add_log(f"If you want the bot to write to a multi-person chat group, make sure the bot is added to that chat group. If it is not - do it and run the command `set ChatId <ChatId>` again.", "info")
290-
return False
291-
292294
def check_db_usage(self):
293295
if time.time() - self.last_db_check < 600:
294296
return
@@ -452,3 +454,4 @@ def add_console_commands(self, console):
452454
console.AddItem("disable_alert", self.disable_alert, self.local.translate("disable_alert_cmd"))
453455
console.AddItem("list_alerts", self.print_alerts, self.local.translate("list_alerts_cmd"))
454456
console.AddItem("test_alert", self.test_alert, self.local.translate("test_alert_cmd"))
457+
console.AddItem("setup_alert_bot", self.setup_alert_bot, self.local.translate("setup_alert_bot_cmd"))

mytonctrl/mytonctrl.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,6 @@ def SetSettings(local, ton, args):
896896
color_print(f"{{red}} Error: set {name} ... is deprecated and does not work {{endc}}."
897897
f"\nInstead, use {{bold}}enable_mode {mode_name}{{endc}}")
898898
return
899-
if name == 'ChatId':
900-
from modules.alert_bot import AlertBotModule
901-
if not AlertBotModule(ton, local).on_set_chat_id(value):
902-
return
903899
force = False
904900
if len(args) > 2:
905901
if args[2] == "--force":

mytonctrl/resources/translate.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@
499499
"ru": "Отправить тестовое оповещение через Telegram Bot",
500500
"zh_TW": "通過 Telegram Bot 發送測試警報"
501501
},
502+
"setup_alert_bot_cmd": {
503+
"en": "Setup Telegram Bot for alerts",
504+
"ru": "Настроить Telegram Bot для оповещений",
505+
"zh_TW": "設置 Telegram Bot 以接收警報"
506+
},
502507
"benchmark_cmd": {
503508
"en": "Run benchmark",
504509
"ru": "Запустить бенчмарк",

0 commit comments

Comments
 (0)