51 lines
2.4 KiB
Python
51 lines
2.4 KiB
Python
import telebot
|
|
import threading
|
|
import time
|
|
|
|
from daemons import base
|
|
from utils import platform
|
|
from utils import queues
|
|
|
|
|
|
class Daemon(base.Daemon):
|
|
def __init__(self):
|
|
self.telegram_bots: dict[str, dict[str, tuple[telebot.TeleBot, threading.Thread]|None]] = {}
|
|
|
|
def execute(self):
|
|
while True:
|
|
bots = platform.platform_client.get_config('bots')
|
|
for project_name, project in bots.items():
|
|
if project_name not in self.telegram_bots:
|
|
self.telegram_bots[project_name] = {}
|
|
for bot_name, bot_info in project.items():
|
|
if bot_name not in self.telegram_bots[project_name]:
|
|
self.telegram_bots[project_name][bot_name] = None
|
|
internal_bot_info = self.telegram_bots[project_name][bot_name]
|
|
if bot_info.get('poll_enabled'):
|
|
if internal_bot_info is not None:
|
|
bot, thread = internal_bot_info
|
|
if thread.is_alive:
|
|
print(f'process for {project_name} {bot_name} is alive')
|
|
continue
|
|
bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
|
|
thread = threading.Thread(target=self.start_polling, args=[bot, bot_info['queue']])
|
|
print(f'starting process for {project_name} {bot_name}')
|
|
thread.start()
|
|
self.telegram_bots[project_name][bot_name] = (bot, thread)
|
|
print(f'started process for {project_name} {bot_name}')
|
|
else:
|
|
if internal_bot_info is None or not internal_bot_info[1].is_alive:
|
|
print(f'process for {project_name} {bot_name} is not alive')
|
|
continue
|
|
print(f'terminating process for {project_name} {bot_name}')
|
|
internal_bot_info[0].stop_bot()
|
|
self.telegram_bots[project_name][bot_name] = None
|
|
print(f'terminated process for {project_name} {bot_name}')
|
|
time.sleep(10)
|
|
|
|
def start_polling(self, bot, queue):
|
|
@bot.message_handler()
|
|
def do_action(message):
|
|
queues.set_task(queue, message.json, 1)
|
|
bot.polling()
|