law-exam-bot/worker.py
Egor Matveev b5536a985a
All checks were successful
Deploy Dev / Build (pull_request) Successful in 1m10s
Deploy Dev / Push (pull_request) Successful in 12s
Deploy Dev / Deploy dev (pull_request) Successful in 6s
initial
2025-03-29 00:07:55 +03:00

64 lines
1.7 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):
message: Message = Message.de_json(json.dumps(payload))
questions = client.get_config('questions')
current_question = store.get(message.chat.id)
if current_question:
self.send("Вот ответ на вопрос", message.chat.id)
text_split = [current_question['answer'][i:i+1000] for i in range(0, len(current_question['answer']), 1000)]
for elem in text_split:
self.send(elem, message.chat.id)
selected = randrange(len(questions))
store[message.chat.id] = selected
self.send("Следующий вопрос: " + questions[selected]['question'], message.chat.id)
Daemon().execute()