botalka/daemons/poll.py
emmatveev ba6b903c57
All checks were successful
Deploy Dev / Build (pull_request) Successful in 5s
Deploy Dev / Push (pull_request) Successful in 7s
Deploy Dev / Deploy dev (pull_request) Successful in 8s
fix
2024-11-27 04:10:50 +03:00

44 lines
1.8 KiB
Python

import telebot
import multiprocessing
import time
from daemons import base
from utils import platform
from utils import queues
class Daemon(base.Daemon):
def __init__(self):
self.telegram_pollers: dict[str, dict[str, multiprocessing.Process|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_pollers:
self.telegram_pollers[project_name] = {}
for bot_name, bot_info in project.items():
if bot_name not in self.telegram_pollers[project_name]:
self.telegram_pollers[project_name][bot_name] = None
process = self.telegram_pollers[project_name][bot_name]
if bot_info.get('poll_enabled'):
if process is not None and process.is_alive:
continue
new_process = multiprocessing.Process(target=self.start_polling, args=[bot_info['secrets']['telegram_token'], bot_info['queue']])
new_process.start()
self.telegram_pollers[project_name][bot_name] = new_process
else:
if process is None:
continue
if process.is_alive:
process.terminate()
self.telegram_pollers[project_name][bot_name] = None
time.sleep(10)
def start_polling(telegram_token, queue):
bot = telebot.TeleBot(telegram_token)
@bot.message_handler()
def do_action(message):
queues.set_task(queue, message.json, 1)
bot.polling()