First commit
This commit is contained in:
16
bot/handlers/form_utils/__init__.py
Normal file
16
bot/handlers/form_utils/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# bot/handlers/__init__.py
|
||||
from aiogram import Router
|
||||
from .form_answer import router as form_answer_router
|
||||
from .topic_replies import router as topic_replies_router
|
||||
from .form_callback import router as form_callback_router
|
||||
|
||||
# Настройка экспорта и роутера
|
||||
__all__ = ('router',)
|
||||
router: Router = Router(name="handlers_router")
|
||||
|
||||
# Подготовка мастер-роутера
|
||||
router.include_routers(
|
||||
form_answer_router,
|
||||
topic_replies_router,
|
||||
form_callback_router,
|
||||
)
|
||||
45
bot/handlers/form_utils/form_answer.py
Normal file
45
bot/handlers/form_utils/form_answer.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from aiogram import Router
|
||||
from aiogram.types import Message
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from bot.data.topic_map import user_topic_map
|
||||
from bot.keyboards import decision_keyboard
|
||||
from bot.states.anketa_states import StartForm
|
||||
from configs import ImportantID
|
||||
|
||||
router = Router(name="form_handlers")
|
||||
|
||||
TOPIC_TYPE = "anketa"
|
||||
|
||||
@router.message(StartForm.waiting_for_application)
|
||||
async def handle_application(message: Message, state: FSMContext):
|
||||
await state.clear()
|
||||
await message.answer("Спасибо! Ваша анкета отправлена на рассмотрение!")
|
||||
|
||||
user = message.from_user
|
||||
user_id = user.id
|
||||
if user.username:
|
||||
users = f' от @{user.username}'
|
||||
else:
|
||||
users = ''
|
||||
text = f'<b><a href="tg://user?id={user_id}">Анкета{users}</a></b>\n\n{message.html_text}'
|
||||
|
||||
key = (user_id, TOPIC_TYPE) # Ключ с типом топика
|
||||
|
||||
if key in user_topic_map:
|
||||
thread_id = user_topic_map[key]
|
||||
else:
|
||||
topic = await message.bot.create_forum_topic(
|
||||
chat_id=ImportantID.SUPPORT_CHAT_ID,
|
||||
name=f"Анкета от {user.full_name}"
|
||||
)
|
||||
thread_id = topic.message_thread_id
|
||||
user_topic_map[key] = thread_id
|
||||
|
||||
await message.bot.send_message(
|
||||
chat_id=ImportantID.SUPPORT_CHAT_ID,
|
||||
message_thread_id=thread_id,
|
||||
text=text,
|
||||
reply_markup=decision_keyboard(thread_id, TOPIC_TYPE) # <--- Передаём оба аргумента
|
||||
)
|
||||
|
||||
|
||||
44
bot/handlers/form_utils/form_callback.py
Normal file
44
bot/handlers/form_utils/form_callback.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from aiogram import Router, F
|
||||
from aiogram.types import CallbackQuery
|
||||
from bot.data.topic_map import user_topic_map
|
||||
|
||||
router = Router(name="form_callbacks")
|
||||
|
||||
TEXTS = {
|
||||
"anketa": {
|
||||
"accept": "<b>🎉 Ваша анкета принята!</b>\n\nДобро пожаловать в проект!",
|
||||
"reject": "<b>❌ Ваша анкета отклонена.</b>\n\nВы можете попробовать позже."
|
||||
},
|
||||
"application": {
|
||||
"accept": "<b>🎉 Ваша анкета принята!</b>\n\nДобро пожаловать в проект!",
|
||||
"reject": "<b>❌ Ваша анкета отклонена.</b>\n\nВы можете попробовать позже."
|
||||
},
|
||||
"union": {
|
||||
"accept": "<b>🤝 Союз одобрен!</b>\n\nТеперь вы в союзе.",
|
||||
"reject": "<b>💔 Союз отклонён.</b>\n\nВы можете обсудить детали с администрацией."
|
||||
}
|
||||
}
|
||||
|
||||
@router.callback_query(F.data.regexp(r"^([a-z_]+):(accept|reject):(\d+)$"))
|
||||
async def process_decision_callback(callback: CallbackQuery):
|
||||
kind, action, thread_id_str = callback.data.split(":")
|
||||
thread_id = int(thread_id_str)
|
||||
|
||||
user_id = None
|
||||
for (uid, k), tid in user_topic_map.items():
|
||||
if k == kind and tid == thread_id:
|
||||
user_id = uid
|
||||
break
|
||||
|
||||
if not user_id:
|
||||
await callback.answer("Пользователь не найден.", show_alert=True)
|
||||
return
|
||||
|
||||
text_to_send = TEXTS.get(kind, {}).get(action)
|
||||
if not text_to_send:
|
||||
await callback.answer("Некорректные данные.", show_alert=True)
|
||||
return
|
||||
|
||||
await callback.message.bot.send_message(chat_id=user_id, text=text_to_send, parse_mode="HTML")
|
||||
await callback.message.edit_reply_markup(reply_markup=None)
|
||||
await callback.answer("Ответ отправлен пользователю.")
|
||||
29
bot/handlers/form_utils/topic_replies.py
Normal file
29
bot/handlers/form_utils/topic_replies.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from aiogram import Router, F
|
||||
from aiogram.types import Message
|
||||
from bot.data.topic_map import user_topic_map
|
||||
|
||||
router: Router = Router(name="topic_replies")
|
||||
|
||||
@router.message(F.is_topic_message, F.reply_to_message, ~F.from_user.is_bot)
|
||||
async def forward_reply_to_user(message: Message):
|
||||
thread_id = message.message_thread_id
|
||||
if thread_id is None:
|
||||
return # нет thread_id, выходим
|
||||
|
||||
# Найдем user_id по thread_id
|
||||
user_id = None
|
||||
for (uid, kind), tid in user_topic_map.items():
|
||||
if tid == thread_id:
|
||||
user_id = uid
|
||||
break
|
||||
|
||||
if user_id is None:
|
||||
return # Топик не зарегистрирован
|
||||
|
||||
reply_text = f"<b>Ответ администратора:</b>\n{message.html_text}"
|
||||
|
||||
try:
|
||||
await message.bot.send_message(chat_id=user_id, text=reply_text, parse_mode="HTML")
|
||||
except Exception as e:
|
||||
await message.reply(f"⚠️ Не удалось отправить сообщение пользователю: {e}")
|
||||
|
||||
Reference in New Issue
Block a user