69 lines
3.0 KiB
Python
69 lines
3.0 KiB
Python
from app.settings_cache import SettingsCache
|
|
from app.conversation import get_or_create_user, get_template_rendered
|
|
from app.fsm import BotState, FSM
|
|
from app.keyboards.inline import build_main_menu_buttons
|
|
|
|
|
|
async def handle_notifications(bot, user_data: dict, db, max_api):
|
|
max_user_id = user_data["user_id"]
|
|
payload = user_data.get("payload", "")
|
|
|
|
if FSM.get_state(max_user_id) != BotState.FEATURE_NOTIFICATIONS:
|
|
FSM.set_state(max_user_id, BotState.FEATURE_NOTIFICATIONS)
|
|
user = await get_or_create_user(db, max_user_id)
|
|
from sqlalchemy import select
|
|
from app.models.models import BotNotificationSubscription
|
|
result = await db.execute(
|
|
select(BotNotificationSubscription).where(
|
|
BotNotificationSubscription.user_id == user.id
|
|
).limit(1)
|
|
)
|
|
sub = result.scalar_one_or_none()
|
|
status_change = "✅" if sub and sub.notify_on_status_change else "⬜"
|
|
reply = "✅" if sub and sub.notify_on_reply else "⬜"
|
|
broadcast = "✅" if sub and sub.notify_on_broadcast else "⬜"
|
|
text = (
|
|
"🔔 **Уведомления**\n\n"
|
|
"Выберите, о чём уведомлять:\n\n"
|
|
f"{status_change} /status — Смена статуса обращения\n"
|
|
f"{reply} /reply — Ответ оператора\n"
|
|
f"{broadcast} /broadcast — Новости и рассылки\n\n"
|
|
"Нажмите на пункт, чтобы переключить."
|
|
)
|
|
await max_api.send_message(max_user_id, text, format="markdown")
|
|
return
|
|
|
|
user = await get_or_create_user(db, max_user_id)
|
|
from sqlalchemy import select
|
|
from app.models.models import BotNotificationSubscription
|
|
result = await db.execute(
|
|
select(BotNotificationSubscription).where(
|
|
BotNotificationSubscription.user_id == user.id
|
|
).limit(1)
|
|
)
|
|
sub = result.scalar_one_or_none()
|
|
if not sub:
|
|
sub = BotNotificationSubscription(user_id=user.id)
|
|
db.add(sub)
|
|
await db.flush()
|
|
|
|
toggle_map = {
|
|
"/status": "notify_on_status_change",
|
|
"/reply": "notify_on_reply",
|
|
"/broadcast": "notify_on_broadcast",
|
|
}
|
|
key = payload if payload.startswith("/") else (payload if payload in toggle_map else user_data.get("text", "").strip())
|
|
if key in toggle_map:
|
|
col = toggle_map[key]
|
|
current = getattr(sub, col)
|
|
setattr(sub, col, not current)
|
|
await db.flush()
|
|
await max_api.send_message(max_user_id, f"Настройка «{key}» изменена.")
|
|
else:
|
|
await max_api.send_message(max_user_id, "Нажмите /status, /reply или /broadcast для переключения.")
|
|
|
|
FSM.transition(max_user_id, "done")
|
|
buttons = build_main_menu_buttons()
|
|
menu_text = await get_template_rendered(db, "main_menu")
|
|
await max_api.send_message_with_keyboard(max_user_id, menu_text, buttons)
|