Skip to content

Commit

Permalink
Now users can leave feedback right in bot
Browse files Browse the repository at this point in the history
  • Loading branch information
nyudenkov committed Aug 19, 2022
1 parent 4e74655 commit e0c30eb
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 55 deletions.
4 changes: 4 additions & 0 deletions app/bot/dialogs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from aiogram_dialog import DialogRegistry

from .feedback import FeedbackDialogSG
from .feedback import feedback_dialog
from .hour import HourDialogSG
from .hour import hour_dialog
from .language import LanguageDialogSG
Expand All @@ -11,5 +13,7 @@
async def register_dialogs(registry: DialogRegistry):
registry.register(language_dialog)

registry.register(feedback_dialog)

registry.register(hour_dialog)
registry.register(timezone_dialog)
76 changes: 76 additions & 0 deletions app/bot/dialogs/feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from aiogram.dispatcher.filters.state import State
from aiogram.dispatcher.filters.state import StatesGroup
from aiogram.types import CallbackQuery
from aiogram.types import Message
from aiogram_dialog import Dialog
from aiogram_dialog import DialogManager
from aiogram_dialog import Window
from aiogram_dialog.widgets.input import MessageInput
from aiogram_dialog.widgets.kbd import Back
from aiogram_dialog.widgets.kbd import Button
from aiogram_dialog.widgets.kbd import Cancel
from aiogram_dialog.widgets.kbd import Group
from aiogram_dialog.widgets.kbd import SwitchTo

from app.bot.dialogs.widgets.text import IConst
from app.bot.dialogs.widgets.text import IFormat
from app.bot.middlewares import i18n
from app.database.models import FeedbackReport

_ = i18n.gettext


class FeedbackDialogSG(StatesGroup):
main = State()
receive_report = State()


async def feedback_type_chosen(callback: CallbackQuery, button: Button, manager: DialogManager):
ctx = manager.current_context()
type_id = button.widget_id
ctx.dialog_data['report_type'] = type_id
if type_id == "bug":
ctx.dialog_data['message_state_text'] = _("найденного бага. Расскажи, как его повторить 🪳")
elif type_id == "feature":
ctx.dialog_data['message_state_text'] = _("своего предложения")
else:
await callback.message.answer(_("Возникла ошибка, сообщи @nyudenkov, пожалуйста"))


async def report_input(message: Message, dialog: Dialog, manager: DialogManager):
ctx = manager.current_context()
await FeedbackReport.create(
type=ctx.dialog_data['report_type'], text=message.text
)
await message.answer(_("Спасибо!"))
await manager.done()


feedback_dialog = Dialog(
Window(
IConst(_("Выбери чем хочешь поделиться")),
Group(
SwitchTo(
IConst(_("Ошибкой")),
id="bug",
on_click=feedback_type_chosen,
state=FeedbackDialogSG.receive_report,
),
SwitchTo(
IConst(_("Предложением")),
id="feature",
on_click=feedback_type_chosen,
state=FeedbackDialogSG.receive_report,
),
width=2,
),
Cancel(IConst(_("Отменить действие"))),
state=FeedbackDialogSG.main,
),
Window(
IFormat(_("Поделись, пожалуйста, деталями {message_state_text}")),
Back(IConst(_("Вернуться назад"))),
MessageInput(report_input),
state=FeedbackDialogSG.receive_report,
)
)
7 changes: 5 additions & 2 deletions app/bot/handlers/feedback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from aiogram import types
from aiogram_dialog import DialogManager
from aiogram_dialog import StartMode

from app import enums
from app.bot.dialogs.feedback import FeedbackDialogSG
from app.bot.middlewares import i18n
from app.bot.utils.errors import catch_error
from app.bot.utils.statistics import catch_intent
Expand All @@ -12,6 +15,6 @@

@catch_intent(intent=enums.Intent.FEEDBACK)
@catch_error
async def feedback_handler(message: types.Message):
async def feedback_handler(message: types.Message, dialog_manager: DialogManager):
await User.get_from_message(message)
await message.reply(_(Message.FEEDBACK))
await dialog_manager.start(FeedbackDialogSG.main, mode=StartMode.RESET_STACK)
5 changes: 4 additions & 1 deletion app/bot/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from aiogram import types

from app.bot import bot
from app.bot.middlewares import i18n
from app.misc.sentry import capture_exception

_ = i18n.gettext


def catch_error(func):
@functools.wraps(func)
Expand All @@ -18,6 +21,6 @@ async def wrapper(*args, **kwargs):
chat_id = msg.message.chat.id
elif type(msg) == types.Message:
chat_id = msg.chat.id
await bot.send_message(chat_id, "Возникла ошибка, сообщите @nyudenkov, пожалуйста")
await bot.send_message(chat_id, _("Возникла ошибка, сообщи @nyudenkov, пожалуйста"))

return wrapper
7 changes: 6 additions & 1 deletion app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app import enums
from app.database import mixins

__all__ = ["User", "Link", "StatisticsRecord"]
__all__ = ["User", "Link", "StatisticsRecord", "FeedbackReport"]


class User(mixins.ModelMixin):
Expand Down Expand Up @@ -58,3 +58,8 @@ async def get_unread_links_by_owner(cls, owner: User) -> "QuerySet":

class StatisticsRecord(mixins.CreatedMixin):
intent = fields.CharEnumField(enums.Intent)


class FeedbackReport(mixins.CreatedMixin):
type = fields.CharEnumField(enums.ReportType)
text = fields.TextField()
6 changes: 6 additions & 0 deletions app/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class Intent(str, Enum):
STATISTICS = "statistics"
FEEDBACK = "feedback"
HOUR = "hour"
TIMEZONE = "timezone"
LINKS = "links"
LANGUAGE = "language"
MAILING = "mailing"


class ReportType(str, Enum):
BUG = "bug"
FEATURE = "feature"
Loading

0 comments on commit e0c30eb

Please sign in to comment.