Skip to content

Commit

Permalink
Merge pull request #32 from Rxyalxrd/master
Browse files Browse the repository at this point in the history
add media to categorytype answer, update fsms
  • Loading branch information
GreenVibesOnly authored Oct 18, 2024
2 parents 7a68e30 + f75110e commit 8acafea
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 204 deletions.
58 changes: 52 additions & 6 deletions app/bot/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging

from aiogram.types import InlineKeyboardButton
from aiogram import F, Router
from aiogram.types import CallbackQuery
from aiogram.utils.keyboard import InlineKeyboardBuilder
from sqlalchemy.ext.asyncio import AsyncSession
from helpers import get_user_id, start_inactivity_timer

from helpers import get_user_id, start_inactivity_timer
from core.bot_setup import bot
from bot.exceptions import message_exception_handler
from bot.keyborads import (
Expand All @@ -18,9 +19,14 @@
get_company_information_keyboard,
support_keyboard,
back_to_main_menu,

)
from crud.questions import get_question_by_id
from crud.projects import get_title_by_id, response_text_by_id
from crud.projects import (
get_category_by_id,
get_title_by_id,
response_text_by_id
)
from crud.portfolio_projects_crud import portfolio_crud
import bot.bot_const as bc
from loggers.log import setup_logging
Expand Down Expand Up @@ -59,9 +65,7 @@ async def show_projects(
log_error_text="Ошибка при возврате в основное меню."
)
@router.callback_query(F.data == "back_to_main_menu")
async def previous_choice(
callback: CallbackQuery, session: AsyncSession
) -> None:
async def previous_choice(callback: CallbackQuery) -> None:
"""Возвращает в основное меню."""

await callback.answer()
Expand Down Expand Up @@ -243,7 +247,7 @@ async def company_info(callback: CallbackQuery, session: AsyncSession) -> None:

@message_exception_handler(log_error_text="Ошибка при запросе техподдержки.")
@router.callback_query(F.data == "tech_support")
async def get_support(callback: CallbackQuery, session: AsyncSession) -> None:
async def get_support(callback: CallbackQuery) -> None:
"""Выводит виды тех. поддержки."""

await callback.answer()
Expand Down Expand Up @@ -297,3 +301,45 @@ async def get_feedback_no(callback: CallbackQuery) -> None:
await callback.answer()

await callback.message.answer(bc.MESSAGE_FOR_GET_FEEDBACK_NO)


@message_exception_handler(
log_error_text='Ошибка при ответе на выбранный тип категории.'
)
@router.callback_query(F.data.startswith("show_category"))
async def process_category_callback(callback: CallbackQuery, session):

await callback.answer()

category_id = callback.data.split(":")[1]

category = await get_category_by_id(category_id, session)

photo_message = await callback.message.answer_photo(
photo=category.media
)

await callback.message.answer(
text=(
f"{category.name}\nОписание: "
f"{category.description}\nСсылка: {category.url}"),
reply_markup=InlineKeyboardBuilder().add(
InlineKeyboardButton(
text="Назад", callback_data=f"back:{photo_message.message_id}"
)
).as_markup()
)


@router.callback_query(F.data.startswith("back"))
async def process_back_callback(callback: CallbackQuery):

await callback.answer()

photo_message_id = callback.data.split(":")[1]

await callback.message.bot.delete_message(
callback.message.chat.id, photo_message_id
)

await callback.message.delete()
13 changes: 10 additions & 3 deletions app/bot/fsm_contexts/feedback_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from crud.users import get_id_by_tg_id
from core.bot_setup import bot
from crud.feedback_crud import feedback_crud
from redis_db.connect import get_redis_connection


router = Router()
Expand All @@ -30,6 +31,8 @@ async def get_feedback_yes(

user_id = get_user_id(callback)

redis_client = await get_redis_connection()

await state.update_data(user=await get_id_by_tg_id(
callback.from_user.id, session))

Expand All @@ -39,7 +42,7 @@ async def get_feedback_yes(

logger.info(f"Пользователь {callback.from_user.id} начал процесс.")

await start_inactivity_timer(user_id, bot, session)
await start_inactivity_timer(user_id, bot, redis_client)


@message_exception_handler(log_error_text="Ошибка при обработке оценки.")
Expand All @@ -51,6 +54,8 @@ async def process_rating(message: Message, state: FSMContext, session:AsyncSessi

user_id = get_user_id(message)

redis_client = await get_redis_connection()

if not is_valid_rating(rating):
await message.answer("Пожалуйста, введите число от 1 до 10.")
return
Expand All @@ -63,7 +68,7 @@ async def process_rating(message: Message, state: FSMContext, session:AsyncSessi

logger.info(f"Пользователь {message.from_user.id} ввел оценку.")

await start_inactivity_timer(user_id, bot, session)
await start_inactivity_timer(user_id, bot, redis_client)


@message_exception_handler(log_error_text="Ошибка при обработке текста.")
Expand All @@ -77,6 +82,8 @@ async def process_description(

user_id = get_user_id(message)

redis_client = await get_redis_connection()

logger.info(f"Пользователь {message.from_user.id} ввел текст.")

feedback_data = await state.get_data()
Expand All @@ -90,6 +97,6 @@ async def process_description(
f"Ваш комментарий: {feedback_data['feedback_text']}"
)

await start_inactivity_timer(user_id, bot, session)
await start_inactivity_timer(user_id, bot, redis_client)

await state.clear()
14 changes: 10 additions & 4 deletions app/bot/fsm_contexts/manager_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from loggers.log import setup_logging
from core.bot_setup import bot
from core.settings import settings

from redis_db.connect import get_redis_connection

router = Router()

Expand All @@ -37,6 +37,8 @@ async def contact_with_manager(

user_id = get_user_id(callback)

redis_client = await get_redis_connection()

await state.update_data(request_type=callback.data)

await callback.message.edit_text(bc.START_INPUT_USER_DATA)
Expand All @@ -47,7 +49,7 @@ async def contact_with_manager(

logger.info(f"Пользователь {user_id} начал процесс.")

await start_inactivity_timer(user_id, bot, session)
await start_inactivity_timer(user_id, bot, redis_client)


@message_exception_handler(
Expand All @@ -59,6 +61,8 @@ async def process_first_name(message: Message, state: FSMContext) -> None:

user_id = get_user_id(message)

redis_client = await get_redis_connection()

if not is_valid_name(message.text):
await message.answer(bc.INPUT_NAME)
return
Expand All @@ -71,7 +75,7 @@ async def process_first_name(message: Message, state: FSMContext) -> None:

await ask_next_question(message, state, bc.Form.phone_number, bc.QUESTIONS)

await start_inactivity_timer(user_id, bot)
await start_inactivity_timer(user_id, bot, redis_client)


@message_exception_handler(
Expand All @@ -85,6 +89,8 @@ async def process_phone_number(

user_id = get_user_id(message)

redis_client = await get_redis_connection()

if not is_valid_phone_number(message.text):
await message.answer(bc.INPUT_NUMBER_PHONE)
return
Expand Down Expand Up @@ -122,6 +128,6 @@ async def process_phone_number(
).as_markup(),
)

await start_inactivity_timer(user_id, bot, session)
await start_inactivity_timer(user_id, bot, redis_client)

await state.clear()
Loading

0 comments on commit 8acafea

Please sign in to comment.