refactor: use AlbumMiddleware instead of MediaGroupCollector, send photos with caption, add photo support for room hours

This commit is contained in:
2026-07-15 00:55:40 +03:00
parent 05b4a79b40
commit 6d5608343a
7 changed files with 98 additions and 84 deletions
@@ -1,3 +1,5 @@
import json
from aiogram import Bot
from aiogram.enums import ContentType
from aiogram.exceptions import TelegramBadRequest, TelegramForbiddenError, TelegramRetryAfter
@@ -314,11 +316,24 @@ async def on_room_remark_input(
widget: MessageInput,
dialog_manager: DialogManager,
):
if message.text and message.text.strip():
dialog_manager.dialog_data["remark"] = message.text.strip()
else:
dialog_manager.dialog_data["remark"] = None
album = dialog_manager.middleware_data.get("album")
photo_file_ids = []
remark = None
if album:
for msg in album:
if msg.photo:
photo_file_ids.append(msg.photo[-1].file_id)
if msg.caption and msg.caption.strip():
remark = msg.caption.strip()
elif message.photo:
photo_file_ids = [message.photo[-1].file_id]
remark = message.caption.strip() if message.caption and message.caption.strip() else None
elif message.text and message.text.strip():
remark = message.text.strip()
dialog_manager.dialog_data["remark"] = remark
dialog_manager.dialog_data["photo_file_ids"] = photo_file_ids
await dialog_manager.switch_to(AdminMenuSG.room_add_hours_confirm)
@@ -328,6 +343,7 @@ async def on_room_skip_remark(
dialog_manager: DialogManager,
):
dialog_manager.dialog_data["remark"] = None
dialog_manager.dialog_data["photo_file_ids"] = []
await dialog_manager.switch_to(AdminMenuSG.room_add_hours_confirm)
@@ -338,10 +354,13 @@ async def get_room_hours_confirm_data(
hours = dialog_manager.dialog_data.get("selected_hours", 0)
remark = dialog_manager.dialog_data.get("remark", "")
remark_text = f"\n\n<b>Примечание:</b> {remark}" if remark else ""
photo_file_ids = dialog_manager.dialog_data.get("photo_file_ids", [])
has_photo = bool(photo_file_ids)
return {
"hours": hours,
"remark_text": remark_text,
"has_photo": f"✅ Да ({len(photo_file_ids)} шт.)" if has_photo else "❌ Нет",
}
@@ -363,12 +382,15 @@ async def on_room_add_hours_confirm(
admin_id = callback.from_user.id
if room_id and hours:
photo_file_ids = dialog_manager.dialog_data.get("photo_file_ids", [])
photo_file_id = json.dumps(photo_file_ids) if photo_file_ids else None
results = await transactions_repository.add_hours_to_room(
room_id=room_id,
amount=hours,
admin_id=admin_id,
is_active=True,
remark=remark,
photo_file_id=photo_file_id,
)
for transaction, resident in results:
@@ -377,11 +399,15 @@ async def on_room_add_hours_confirm(
if user:
try:
remark_text = f"\n💬 <i>{remark}</i>" if remark else ""
await bot.send_message(
user.id,
f"<blockquote> <b>Уведомление</b></blockq uote>\n\n"
f"Вашей комнате начислено <b>+{hours}</b> ч{remark_text}"
)
notification_text = f"<blockquote> <b>Уведомление</b></blockquote>\n\nВашей комнате начислено <b>+{hours}</b> ч{remark_text}"
if photo_file_ids:
if len(photo_file_ids) == 1:
await bot.send_photo(user.id, photo_file_ids[0], caption=notification_text)
else:
media = [InputMediaPhoto(media=fid, caption=notification_text if i == 0 else None) for i, fid in enumerate(photo_file_ids)]
await bot.send_media_group(user.id, media=media)
else:
await bot.send_message(user.id, notification_text)
except (TelegramBadRequest, TelegramForbiddenError, TelegramRetryAfter):
pass
@@ -703,7 +729,7 @@ room_add_hours_custom_window = Window(
)
room_add_hours_remark_window = Window(
Const("<blockquote>💬 <b>Примечание</b></blockquote>\n\nВведите примечание к операции (или пропустите):"),
Const("<blockquote>💬 <b>Примечание</b></blockquote>\n\nВведите примечание к операции или отправьте фото/медиагруппу с подписью (или пропустите):"),
MessageInput(on_room_remark_input),
Button(
Const("⏭ Пропустить"),
@@ -719,7 +745,7 @@ room_add_hours_remark_window = Window(
)
room_add_hours_confirm_window = Window(
Format("<blockquote> <b>Подтверждение</b></blockquote>\n\nВы уверены, что хотите добавить <code>{hours}</code> часов?{remark_text}"),
Format("<blockquote> <b>Подтверждение</b></blockquote>\n\nВы уверены, что хотите добавить <code>{hours}</code> часов?{remark_text}\n\n📷 <b>Фото:</b> {has_photo}"),
Row(
Button(
Const("✅ Да"),