Total Complexity | 4 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # -*- coding:utf-8 -*- |
||
2 | """ |
||
3 | Telegram bot messages functions |
||
4 | """ |
||
5 | from loglan_core import WordSelector |
||
6 | |||
7 | from app.bot.telegram import bot, msg |
||
8 | from app.bot.telegram.handlers.commands import send_message_by_key |
||
9 | from app.bot.telegram.keyboards import WordKeyboard |
||
10 | from app.bot.telegram.models import export_as_str |
||
11 | from app.engine import async_session_maker |
||
12 | |||
13 | |||
14 | async def bot_text_messages_handler(message: msg) -> None: |
||
15 | """ |
||
16 | Handle user's text messages |
||
17 | :param message: |
||
18 | :return: None |
||
19 | """ |
||
20 | |||
21 | user_request = message.text.removeprefix("/") |
||
22 | async with async_session_maker() as session: |
||
23 | |||
24 | words = await ( |
||
25 | WordSelector() |
||
26 | .by_name(user_request) |
||
27 | .with_relationships() |
||
28 | .all_async(session, unique=True) |
||
29 | ) |
||
30 | |||
31 | if words: |
||
32 | for word in words: |
||
33 | await bot.send_message( |
||
34 | chat_id=message.chat.id, |
||
35 | text=export_as_str(word), |
||
36 | reply_markup=WordKeyboard(word).keyboard_cpx(), |
||
37 | ) |
||
38 | else: |
||
39 | await send_message_by_key(user_request, message.chat.id) |
||
40 |