Passed
Push — master ( 8b1b4c...c30160 )
by torrua
01:18
created

app.bot.telegram.handlers.messages   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A bot_text_messages_handler() 0 20 4
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 Session
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
    with Session() as session:
23
        words = WordSelector().by_name(user_request).with_relationships().all(session)
24
25
    if words:
26
        for word in words:
27
            await bot.send_message(
28
                chat_id=message.chat.id,
29
                text=export_as_str(word),
30
                reply_markup=WordKeyboard(word).keyboard_cpx(),
31
            )
32
    else:
33
        await send_message_by_key(user_request, message.chat.id)
34