add mailing function
This commit is contained in:
43
app/bot/dialogs/flows/mailing/dialogs.py
Normal file
43
app/bot/dialogs/flows/mailing/dialogs.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from aiogram_dialog import Dialog, Window
|
||||
from aiogram_dialog.widgets.input import MessageInput
|
||||
from aiogram_dialog.widgets.kbd import Back, Button, Cancel
|
||||
from aiogram_dialog.widgets.text import Const
|
||||
|
||||
# from .getters import event_getter, events_list_getter, registration_getter
|
||||
from .handlers import choose_recipients, confirm_mailing, message_data
|
||||
from .states import MailingSG
|
||||
|
||||
mailing_dialog = Dialog(
|
||||
Window(
|
||||
Const("Пришлите сообщение которое хотите разослать"),
|
||||
MessageInput(message_data),
|
||||
Cancel(Const("Отмена")),
|
||||
state=MailingSG.message_data,
|
||||
),
|
||||
Window(
|
||||
Const("Кому отправить сообщение?"),
|
||||
Button(
|
||||
Const("Всем"),
|
||||
id="send_all",
|
||||
on_click=choose_recipients,
|
||||
),
|
||||
Button(
|
||||
Const("Не админам"),
|
||||
id="send_users",
|
||||
on_click=choose_recipients,
|
||||
),
|
||||
Back(Const("Назад")),
|
||||
state=MailingSG.recipients,
|
||||
),
|
||||
Window(
|
||||
Const("Начать рассылку?"),
|
||||
Button(
|
||||
Const("Разослать"),
|
||||
id="start_mailing",
|
||||
on_click=confirm_mailing,
|
||||
),
|
||||
Back(Const("Назад")),
|
||||
Cancel(Const("Отмена")),
|
||||
state=MailingSG.confirm,
|
||||
),
|
||||
)
|
||||
54
app/bot/dialogs/flows/mailing/handlers.py
Normal file
54
app/bot/dialogs/flows/mailing/handlers.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import asyncio
|
||||
|
||||
from aiogram.types import CallbackQuery, Message
|
||||
from aiogram_dialog import DialogManager
|
||||
from aiogram_dialog.widgets.input import MessageInput
|
||||
from aiogram_dialog.widgets.kbd import Button
|
||||
|
||||
from app.infrastructure.database.crud import get_users
|
||||
|
||||
|
||||
async def message_data(
|
||||
message: Message,
|
||||
widget: MessageInput,
|
||||
manager: DialogManager,
|
||||
):
|
||||
manager.dialog_data["message"] = message
|
||||
await manager.next()
|
||||
|
||||
|
||||
async def choose_recipients(
|
||||
callback: CallbackQuery,
|
||||
button: Button,
|
||||
manager: DialogManager,
|
||||
):
|
||||
manager.dialog_data["recipients"] = button.widget_id
|
||||
await manager.next()
|
||||
|
||||
|
||||
async def confirm_mailing(
|
||||
callback: CallbackQuery,
|
||||
button: Button,
|
||||
manager: DialogManager,
|
||||
):
|
||||
print(manager.dialog_data["recipients"])
|
||||
users = list(await get_users(
|
||||
manager.middleware_data["session"],
|
||||
exclude_admins=manager.dialog_data["recipients"] == "send_users",
|
||||
))
|
||||
|
||||
await asyncio.gather(
|
||||
*[
|
||||
callback.bot.copy_message(
|
||||
chat_id=user.tg_id,
|
||||
from_chat_id=manager.dialog_data["message"].chat.id,
|
||||
message_id=manager.dialog_data["message"].message_id,
|
||||
)
|
||||
for user in users
|
||||
]
|
||||
)
|
||||
await callback.bot.send_message(
|
||||
chat_id=callback.from_user.id,
|
||||
text=f"Разослано сообщений: {len(users)}",
|
||||
)
|
||||
await manager.done()
|
||||
8
app/bot/dialogs/flows/mailing/states.py
Normal file
8
app/bot/dialogs/flows/mailing/states.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from aiogram.fsm.state import State, StatesGroup
|
||||
|
||||
|
||||
class MailingSG(StatesGroup):
|
||||
message_data = State()
|
||||
recipients = State()
|
||||
# mailing_date = State()
|
||||
confirm = State()
|
||||
Reference in New Issue
Block a user