law-exam-bot/worker.py
Egor Matveev 8761d8ff65 fix
2025-03-29 00:20:38 +03:00

63 lines
1.6 KiB
Python

import datetime
import os
from random import randrange, choice
import base
import queues
from telebot.types import Message
import json
from sprint_platform import PlatformClient
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
stage = os.getenv("STAGE", 'local')
client = PlatformClient(
security_token,
'law-exam-bot',
stage,
['constants', 'answers', 'replies'],
[],
need_poll=True,
)
store = {}
class Daemon(base.Daemon, queues.TasksHandlerMixin):
@property
def queue_name(self):
return 'law_exam_bot_worker'
def execute(self):
self.poll()
def send(self, text: str, chat_id: int):
queues.set_task(
'botalka_mailbox',
{
'project': 'law-exam-bot',
'name': 'telegram-bot',
'body': {
'text': text,
'chat_id': chat_id,
}
},
1,
)
def process(self, payload):
global store
message: Message = Message.de_json(json.dumps(payload))
questions = client.get_config('questions')
current_question = store.get(message.chat.id)
if current_question is not None:
self.send("Вот ответ на вопрос", message.chat.id)
self.send(current_question['answer'], message.chat.id)
selected = randrange(len(questions))
store[message.chat.id] = selected
self.send("Следующий вопрос: " + questions[selected]['question'], message.chat.id)
Daemon().execute()