Compare commits
2 Commits
1be3401d09
...
5cf9455f76
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cf9455f76 | |||
| da3af83b3a |
@@ -4,9 +4,11 @@ from aiogram.types import CallbackQuery, Message
|
|||||||
from aiogram_dialog import DialogManager
|
from aiogram_dialog import DialogManager
|
||||||
from aiogram_dialog.widgets.input import MessageInput
|
from aiogram_dialog.widgets.input import MessageInput
|
||||||
from aiogram_dialog.widgets.kbd import Button
|
from aiogram_dialog.widgets.kbd import Button
|
||||||
|
from structlog import get_logger
|
||||||
|
|
||||||
from app.infrastructure.database.crud import get_users
|
from app.infrastructure.database.crud import get_users
|
||||||
|
|
||||||
|
logger = get_logger()
|
||||||
|
|
||||||
async def message_data(
|
async def message_data(
|
||||||
message: Message,
|
message: Message,
|
||||||
@@ -26,29 +28,50 @@ async def choose_recipients(
|
|||||||
await manager.next()
|
await manager.next()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def confirm_mailing(
|
async def confirm_mailing(
|
||||||
callback: CallbackQuery,
|
callback: CallbackQuery,
|
||||||
button: Button,
|
button: Button,
|
||||||
manager: DialogManager,
|
manager: DialogManager,
|
||||||
):
|
):
|
||||||
print(manager.dialog_data["recipients"])
|
print(manager.dialog_data["recipients"])
|
||||||
|
|
||||||
users = list(await get_users(
|
users = list(await get_users(
|
||||||
manager.middleware_data["session"],
|
manager.middleware_data["session"],
|
||||||
exclude_admins=manager.dialog_data["recipients"] == "send_users",
|
exclude_admins=manager.dialog_data["recipients"] == "send_users",
|
||||||
))
|
))
|
||||||
|
|
||||||
await asyncio.gather(
|
message = manager.dialog_data["message"]
|
||||||
*[
|
source_chat_id = message.chat.id
|
||||||
callback.bot.copy_message(
|
source_message_id = message.message_id
|
||||||
|
|
||||||
|
success_count = 0
|
||||||
|
|
||||||
|
# Создаём задачу с обработкой ошибок для каждого пользователя
|
||||||
|
async def send_to_user(user):
|
||||||
|
nonlocal success_count
|
||||||
|
try:
|
||||||
|
await callback.bot.copy_message(
|
||||||
chat_id=user.tg_id,
|
chat_id=user.tg_id,
|
||||||
from_chat_id=manager.dialog_data["message"].chat.id,
|
from_chat_id=source_chat_id,
|
||||||
message_id=manager.dialog_data["message"].message_id,
|
message_id=source_message_id,
|
||||||
)
|
)
|
||||||
for user in users
|
success_count += 1
|
||||||
]
|
except Exception as e:
|
||||||
|
logger.warning(
|
||||||
|
"не удалось отправить сообщение",
|
||||||
|
user_id=user.tg_id,
|
||||||
|
error=str(e),
|
||||||
|
exc_info=True, # добавит traceback
|
||||||
)
|
)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
await asyncio.gather(*[send_to_user(user) for user in users])
|
||||||
|
|
||||||
await callback.bot.send_message(
|
await callback.bot.send_message(
|
||||||
chat_id=callback.from_user.id,
|
chat_id=callback.from_user.id,
|
||||||
text=f"Разослано сообщений: {len(users)}",
|
text=f"Рассылка завершена.\nУспешно отправлено: {success_count} из {len(users)}",
|
||||||
)
|
)
|
||||||
await manager.done()
|
await manager.done()
|
||||||
Reference in New Issue
Block a user