Files
site_aegisone/max_bot/app/handlers/consent.py
T

111 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import datetime
import logging
from sqlalchemy import select
from app.database import async_session
from app.models import BotUser, BotConversation, BotMessage
from app.max_api import max_api
from app.config_reader import config_reader
from app.keyboards import contact_keyboard, consent_keyboard, return_to_consent_keyboard
logger = logging.getLogger(__name__)
async def handle_consent_callback(user_id: int, conv_id: int, callback_id: str) -> None:
if callback_id == "consent_yes":
await handle_consent_yes(user_id, conv_id)
elif callback_id == "consent_no":
await handle_consent_no(user_id, conv_id)
async def handle_consent_response(user_id: int, conv_id: int, text: str) -> None:
text_lower = text.strip().lower()
consent_words = ["да", "даю", "согласен", "согласна", "yes", "ok", "хорошо", "даю согласие"]
refuse_words = ["нет", "не даю", "отказ", "no", "не согласен", "не согласна"]
if any(w in text_lower for w in consent_words):
await handle_consent_yes(user_id, conv_id)
elif any(w in text_lower for w in refuse_words):
await handle_consent_no(user_id, conv_id)
else:
await max_api.send_message(
user_id,
"Пожалуйста, дайте чёткий ответ — даёте ли вы согласие на обработку "
"персональных данных? Это необходимо для обработки вашего запроса.",
attachments=consent_keyboard(),
)
async def handle_consent_yes(user_id: int, conv_id: int) -> None:
async with async_session() as db:
result = await db.execute(select(BotUser).where(BotUser.id == user_id))
user = result.scalar_one_or_none()
if user:
user.consent_given = True
user.consent_date = datetime.datetime.utcnow()
result = await db.execute(
select(BotConversation).where(BotConversation.id == conv_id)
)
conv = result.scalar_one_or_none()
if conv:
conv.state = "awaiting_contact"
await db.commit()
try:
await max_api.send_message(
user_id,
"Спасибо! Поделитесь, пожалуйста, вашим контактом, чтобы мы могли "
"связаться с вами.\n\n"
"Нажмите кнопку «Поделиться контактом» или введите номер телефона / email вручную.",
attachments=contact_keyboard(),
)
except Exception as e:
logger.error(f"Failed to send consent_yes message: {e}")
async def handle_consent_no(user_id: int, conv_id: int) -> None:
async with async_session() as db:
result = await db.execute(
select(BotConversation).where(BotConversation.id == conv_id)
)
conv = result.scalar_one_or_none()
if conv:
conv.state = "consent_refused"
await db.commit()
phones = config_reader.get_phones()
email = config_reader.get_site_mail()
phone_text = "\n".join(
f"📞 {phone} — [позвонить](tel:{link})"
for phone, link in phones
)
try:
await max_api.send_message(
user_id,
f"Без вашего согласия мы не можем обработать обращение.\n\n"
f"Вы можете позвонить нам:\n{phone_text}\n\n"
f"📧 [Написать на почту](mailto:{email})\n\n"
f"Если передумаете и захотите дать согласие — просто напишите об этом.",
)
except Exception as e:
logger.error(f"Failed to send consent_no message: {e}")
async def handle_refused_again(user_id: int, conv_id: int, text: str) -> None:
text_lower = text.strip().lower()
consent_words = ["да", "даю", "согласен", "согласна", "yes", "ok", "хорошо", "даю согласие", "передумал", "согласен дать"]
if any(w in text_lower for w in consent_words):
await handle_consent_yes(user_id, conv_id)
else:
async with async_session() as db:
conv = await db.get(BotConversation, conv_id)
if conv:
conv.state = "completed"
await db.commit()
from app.handlers.greeting import analyze_and_respond
await analyze_and_respond(user_id, conv_id, text)