GPQBot

Форк
0
/
fidback.py 
108 строк · 3.9 Кб
1
from pybotx import (
2
    Bot,
3
    HandlerCollector,
4
    IncomingMessage,
5
)
6

7
from enum import Enum, auto
8
from pathlib import Path
9
from pybotx_fsm import FSMCollector
10
from app.bot.middlewares.cancel_creation import cancel_creation_middleware
11
from app.bot.middlewares.db_sessions import db_session_middleware
12
from app.interactors.create import CreateFidbackInteractor
13
from app.services.file_storage import FileStorage
14
from app.bot.commands.answer.cancel import get_cancel_keyboard_button
15
from app.bot.constants import constants
16
from schemas.shemas import FidbackCreate
17
from resources import strings
18

19
collector = HandlerCollector()
20

21
class CreateFidback(Enum):
22
    WAITING_FIDBACK_SUBDIVISION = auto()
23
    WAITING_FIDBACK_DIVISION = auto()
24
    WAITING_FIDBACK_DISCRIPTION = auto()
25
    WAITING_FIDBACK_APPROVE = auto()
26

27
collector = HandlerCollector()
28
file_storage = FileStorage(Path(constants.FILE_STORAGE_PATH))
29
fsm = FSMCollector(CreateFidback)
30

31
@collector.command("/создать", description="Оставить обратную связь")
32
async def create_fidback(message: IncomingMessage, bot: Bot) -> None:
33
    await message.state.fsm.change_state(
34
        CreateFidback.WAITING_FIDBACK_SUBDIVISION,
35
        task=FidbackCreate(subdivision=message.body)
36
    )
37
    await bot.answer_message(body=strings.FIDBACK_SUB, keyboard=get_cancel_keyboard_button())
38

39
@fsm.on(CreateFidback.WAITING_FIDBACK_SUBDIVISION, middlewares=[cancel_creation_middleware])
40
async def waiting_fidback_subdivision(message: IncomingMessage, bot: Bot) -> None:
41
    if len(message.body) > 2:
42
        await bot.answer_message(
43
            body=strings.FIDBACK_SUB, keyboard=get_cancel_keyboard_button()
44
                                 )
45
        return 
46

47
    task = message.state.fsm_storage.task
48
    task.subdivision = message.body
49

50
    await message.state.fsm.change_state(
51
        CreateFidback.WAITING_FIDBACK_DIVISION,
52
        task=task,
53
    )
54
    await bot.answer_message(
55
        body=strings.FIDBACK_SUB, keyboard=get_cancel_keyboard_button()
56
    )
57
                                         
58

59
       
60
@fsm.on(CreateFidback.WAITING_FIDBACK_DIVISION, middlewares=[cancel_creation_middleware])
61
async def waiting_fidback_division(message: IncomingMessage, bot: Bot) -> None:
62
    if len(message.body) > 3:
63
        await bot.answer_message(
64
           body=strings.FIDBACK_DIV, keyboard=get_cancel_keyboard_button()
65
        )
66

67
        return 
68
    task = message.state.fsm_storage.task
69
    task.division = message.body
70

71
    await message.state.fsm.change_state(
72
        CreateFidback.WAITING_FIDBACK_DISCRIPTION, task=task
73
    )
74

75
@fsm.on(CreateFidback.WAITING_FIDBACK_DISCRIPTION, middlewares=[db_session_middleware,cancel_creation_middleware])
76
async def waiting_fidback_discription(message: IncomingMessage, bot: Bot) -> None:
77
    if len(message.body) > 5:
78
        await bot.answer_message(
79
           body=strings.FIDBACK_TEXT, keyboard=get_cancel_keyboard_button()
80
           )                               
81
        return
82
    task = message.state.fsm_storage.task
83
    task.description = message.body
84

85
    await message.state.fsm.change_state(
86
        CreateFidback.WAITING_FIDBACK_APPROVE, task=task
87
    )
88

89
@fsm.on(CreateFidback.WAITING_FIDBACK_APPROVE, middlewares=[db_session_middleware,cancel_creation_middleware])
90
async def waiting_fidback_approve(message: IncomingMessage, bot: Bot) -> None:
91
    task = message.state.fsm_storage.task
92
    
93
    db_session = message.state.db_session
94

95
    if message.body == strings.CONFIRM_TASK_COMMAND:
96
        interactor = CreateFidbackInteractor(db_session)
97
        task = await interactor.execute(task)
98

99
        await message.state.fsm.drop_state()
100
        await bot.send(message=strings.SUCCESS_FIDBACK
101
                       )
102
    elif message.body == strings.CANCEL_TASK_COMMAND:
103
        await message.state.fsm.drop_state(
104
            CreateFidback.WAITING_FIDBACK_SUBDIVISION,
105
            task=FidbackCreate(subdivision=message.body)
106
        )
107
    else:
108
        pass

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.