Merge pull request 'master' (#16) from master into prod
Reviewed-on: #16
This commit is contained in:
commit
d3d92f56ee
2
.gitignore
vendored
2
.gitignore
vendored
@ -117,3 +117,5 @@ GitHub.sublime-settings
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
.history
|
||||
|
||||
local_platform.json
|
||||
|
@ -4,4 +4,5 @@ WORKDIR /usr/src/app
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . .
|
||||
ENTRYPOINT ["python", "entrypoint.py"]
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
ENTRYPOINT ["python", "main.py"]
|
||||
|
@ -1,3 +1,3 @@
|
||||
class Base:
|
||||
class Daemon:
|
||||
def execute(self):
|
||||
raise NotImplemented
|
||||
|
@ -20,6 +20,8 @@ class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
||||
if bot['type'] == 'telegram':
|
||||
token = bot['secrets']['telegram_token']
|
||||
self.process_telegram(token, payload['body'])
|
||||
else:
|
||||
print('Unknown bot type:', bot['type'])
|
||||
|
||||
def process_telegram(self, token, payload):
|
||||
try:
|
||||
|
@ -1,5 +1,5 @@
|
||||
import telebot
|
||||
import multiprocessing
|
||||
import threading
|
||||
import time
|
||||
|
||||
from daemons import base
|
||||
@ -9,34 +9,39 @@ from utils import queues
|
||||
|
||||
class Daemon(base.Daemon):
|
||||
def __init__(self):
|
||||
self.telegram_pollers: dict[str, dict[str, multiprocessing.Process|None]] = {}
|
||||
self.telegram_bots: dict[str, dict[str, telebot.TeleBot|None]] = {}
|
||||
|
||||
def execute(self):
|
||||
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)
|
||||
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
|
||||
bot = self.telegram_bots[project_name][bot_name]
|
||||
if bot_info.get('poll_enabled'):
|
||||
if bot is not None:
|
||||
print(f'process for {project_name} {bot_name} is alive')
|
||||
continue
|
||||
print(f'starting process for {project_name} {bot_name}')
|
||||
bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
|
||||
self.start_polling(bot, bot_info['queue'])
|
||||
self.telegram_bots[project_name][bot_name] = bot
|
||||
print(f'started process for {project_name} {bot_name}')
|
||||
else:
|
||||
if bot is None:
|
||||
print(f'process for {project_name} {bot_name} is not alive')
|
||||
continue
|
||||
print(f'terminating process for {project_name} {bot_name}')
|
||||
bot.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(telegram_token, queue):
|
||||
bot = telebot.TeleBot(telegram_token)
|
||||
def start_polling(self, bot: telebot.TeleBot, queue: str):
|
||||
@bot.message_handler()
|
||||
def do_action(message):
|
||||
def do_action(message: telebot.types.Message):
|
||||
queues.set_task(queue, message.json, 1)
|
||||
bot.polling()
|
||||
threading.Thread(target=bot.polling).start()
|
||||
|
20
main.py
20
main.py
@ -2,14 +2,16 @@ import sys
|
||||
|
||||
|
||||
arg = sys.argv[-1]
|
||||
# arg = 'poll'
|
||||
|
||||
if arg == "poll":
|
||||
print("poll is starting")
|
||||
from daemons.poll import Daemon
|
||||
elif arg == 'mailbox':
|
||||
print("mailbox is starting")
|
||||
from daemons.mailbox import Daemon
|
||||
else:
|
||||
raise ValueError(f"Unknown param {arg}")
|
||||
if __name__ == '__main__':
|
||||
if arg == "poll":
|
||||
print("poll is starting")
|
||||
from daemons.poll import Daemon
|
||||
elif arg == 'mailbox':
|
||||
print("mailbox is starting")
|
||||
from daemons.mailbox import Daemon
|
||||
else:
|
||||
raise ValueError(f"Unknown param {arg}")
|
||||
|
||||
Daemon().execute()
|
||||
Daemon().execute()
|
||||
|
6
requirements.txt
Normal file
6
requirements.txt
Normal file
@ -0,0 +1,6 @@
|
||||
certifi==2024.8.30
|
||||
charset-normalizer==3.4.0
|
||||
idna==3.10
|
||||
pyTelegramBotAPI==4.1.1
|
||||
requests==2.32.3
|
||||
urllib3==2.2.3
|
@ -88,6 +88,6 @@ class PlatformClient:
|
||||
|
||||
platform_client = PlatformClient(
|
||||
'Botalka',
|
||||
os.getenv('STAGE'),
|
||||
os.getenv('STAGE', 'local'),
|
||||
need_poll=True,
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user