Skip to content

Commit

Permalink
Merge pull request #35 from Rxyalxrd/master
Browse files Browse the repository at this point in the history
Обновил логику таймера, если не можем подключиться к redis, то берем стандартное значение
  • Loading branch information
GreenVibesOnly authored Oct 18, 2024
2 parents a7e70ab + a3e392f commit 5c4f75e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ cython_debug/

# PyCharm
.idea/

#redis
.redisdata/
42 changes: 21 additions & 21 deletions app/bot/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from crud.portfolio_projects_crud import portfolio_crud
import bot.bot_const as bc
from loggers.log import setup_logging
from redis_db.connect import get_redis_connection


router = Router()
Expand All @@ -49,7 +48,6 @@ async def show_projects(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

await callback.message.edit_text(
bc.MESSAGE_FOR_SHOW_PROJECTS,
Expand All @@ -58,7 +56,7 @@ async def show_projects(

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

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -71,15 +69,14 @@ async def previous_choice(callback: CallbackQuery) -> None:
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

await callback.message.edit_text(
bc.MESSAGE_FOR_PREVIOUS_CHOICE, reply_markup=main_keyboard
)

logger.info(f"Пользователь {user_id} вернулся в основное меню")

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(log_error_text="Ошибка при получении вопросов.")
Expand All @@ -92,7 +89,6 @@ async def get_questions(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

question_type = (
"GENERAL_QUESTIONS"
Expand All @@ -111,7 +107,7 @@ async def get_questions(
f"Пользователь {user_id} " f"запросил {question_type.lower()}."
)

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -126,7 +122,6 @@ async def get_faq_answer(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

question = await get_question_by_id(callback.data.split(":")[1], session)

Expand All @@ -145,7 +140,7 @@ async def get_faq_answer(
f"ответ на вопрос {callback.data.split(':')[1]} "
)

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -160,7 +155,6 @@ async def back_to_products(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

await callback.message.edit_text(
text=bc.MESSAGE_FOR_BACK_TO_PRODUCTS,
Expand All @@ -169,7 +163,7 @@ async def back_to_products(

logger.info(f"Пользователь {user_id} вернулся к выбору продуктов.")

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -184,7 +178,6 @@ async def get_response_by_title(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

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

Expand All @@ -197,7 +190,7 @@ async def get_response_by_title(

logger.info(f"Пользователь {user_id} выбрал категорию {category_id}.")

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(log_error_text="Ошибка при показе портфолио.")
Expand All @@ -210,7 +203,6 @@ async def view_portfolio(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

url = await portfolio_crud.get_portfolio(session)
await callback.message.edit_text(
Expand All @@ -220,7 +212,7 @@ async def view_portfolio(

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

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -233,7 +225,6 @@ async def company_info(callback: CallbackQuery, session: AsyncSession) -> None:
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

await callback.message.edit_text(
bc.MESSAGE_FOR_COMPANY_INFO,
Expand All @@ -242,7 +233,7 @@ async def company_info(callback: CallbackQuery, session: AsyncSession) -> None:

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

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(log_error_text="Ошибка при запросе техподдержки.")
Expand All @@ -253,15 +244,14 @@ async def get_support(callback: CallbackQuery) -> None:
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

await callback.message.edit_text(
bc.MESSAGE_FOR_GET_SUPPORT, reply_markup=support_keyboard
)

logger.info(f"Пользователь {user_id} запросил техподдержку.")

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -276,7 +266,6 @@ async def products_services(
await callback.answer()

user_id = get_user_id(callback)
redis_client = await get_redis_connection()

await callback.message.edit_text(
bc.MESSAGE_FOR_PRODUCTS_SERVICES,
Expand All @@ -288,7 +277,7 @@ async def products_services(
f"информацию о продуктах и услугах. "
)

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -311,6 +300,8 @@ async def process_category_callback(callback: CallbackQuery, session):

await callback.answer()

user_id = get_user_id(callback)

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

category = await get_category_by_id(category_id, session)
Expand All @@ -330,16 +321,25 @@ async def process_category_callback(callback: CallbackQuery, session):
).as_markup()
)

await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
log_error_text='Ошибка при возврате назад.'
)
@router.callback_query(F.data.startswith("back"))
async def process_back_callback(callback: CallbackQuery):

await callback.answer()

user_id = get_user_id(callback)

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

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

await callback.message.delete()

await start_inactivity_timer(callback.message, user_id, bot)
17 changes: 6 additions & 11 deletions app/bot/fsm_contexts/feedback_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 @@ -25,14 +24,14 @@
@message_exception_handler(log_error_text="Ошибка при составлении фидбека.")
@router.callback_query(F.data == "get_feedback_yes")
async def get_feedback_yes(
callback: CallbackQuery, state: FSMContext, session: AsyncSession
callback: CallbackQuery,
state: FSMContext,
session: AsyncSession
) -> None:
"""Начало сбора обратной связи."""

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 @@ -42,7 +41,7 @@ async def get_feedback_yes(

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

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(log_error_text="Ошибка при обработке оценки.")
Expand All @@ -54,8 +53,6 @@ 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 @@ -68,7 +65,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, redis_client)
await start_inactivity_timer(message, user_id, bot)


@message_exception_handler(log_error_text="Ошибка при обработке текста.")
Expand All @@ -82,8 +79,6 @@ 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 @@ -97,6 +92,6 @@ async def process_description(
f"Ваш комментарий: {feedback_data['feedback_text']}"
)

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

await state.clear()
18 changes: 7 additions & 11 deletions app/bot/fsm_contexts/manager_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
from bot.keyborads import back_to_main_menu
from bot.smtp import send_mail
from bot.validators import (
is_valid_name, is_valid_phone_number, format_phone_number
is_valid_name,
is_valid_phone_number,
format_phone_number
)
from crud.request_to_manager import create_request_to_manager
from helpers import ask_next_question, get_user_id, start_inactivity_timer
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,8 +39,6 @@ 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 @@ -49,7 +49,7 @@ async def contact_with_manager(

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

await start_inactivity_timer(user_id, bot, redis_client)
await start_inactivity_timer(callback.message, user_id, bot)


@message_exception_handler(
Expand All @@ -61,8 +61,6 @@ 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 @@ -75,7 +73,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, redis_client)
await start_inactivity_timer(message, user_id, bot)


@message_exception_handler(
Expand All @@ -89,8 +87,6 @@ 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 @@ -128,6 +124,6 @@ async def process_phone_number(
).as_markup(),
)

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

await state.clear()
Loading

0 comments on commit 5c4f75e

Please sign in to comment.