v1.8.1: fix consent flow — split-based word matching, refuse words before quick_intent, operator handler, plain text phones, clarify_intent post-processing

This commit is contained in:
2026-06-01 19:54:11 +03:00
parent cc87bcf72c
commit a64a274829
5 changed files with 142 additions and 25 deletions
+44 -15
View File
@@ -1,4 +1,5 @@
import datetime
import re
import logging
from sqlalchemy import select
from app.database import async_session
@@ -10,6 +11,16 @@ from app.keyboards import contact_keyboard, consent_keyboard, return_to_consent_
logger = logging.getLogger(__name__)
def _matches_any(text: str, words: list) -> bool:
text_words = set(text.split())
for w in words:
if w in text_words:
return True
if len(w) > 3 and w in text:
return True
return False
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)
@@ -22,9 +33,9 @@ async def handle_consent_response(user_id: int, conv_id: int, text: str) -> None
consent_words = ["да", "даю", "согласен", "согласна", "yes", "ok", "хорошо", "даю согласие"]
refuse_words = ["нет", "не даю", "отказ", "no", "не согласен", "не согласна"]
if any(w in text_lower for w in consent_words):
if _matches_any(text_lower, consent_words):
await handle_consent_yes(user_id, conv_id)
elif any(w in text_lower for w in refuse_words):
elif _matches_any(text_lower, refuse_words):
await handle_consent_no(user_id, conv_id)
else:
await max_api.send_message(
@@ -77,17 +88,14 @@ async def handle_consent_no(user_id: int, conv_id: int) -> None:
phones = config_reader.get_phones()
email = config_reader.get_site_mail()
phone_text = "\n".join(
f"📞 {phone} — [позвонить](tel:{link})"
for phone, link in phones
)
phone_text = "\n".join(f"📞 {phone}" for phone, _ 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"📧 {email}\n\n"
f"Если передумаете и захотите дать согласие — просто напишите об этом.",
)
except Exception as e:
@@ -97,14 +105,35 @@ async def handle_consent_no(user_id: int, conv_id: int) -> None:
async def handle_refused_again(user_id: int, conv_id: int, text: str) -> None:
text_lower = text.strip().lower()
consent_words = ["да", "даю", "согласен", "согласна", "yes", "ok", "хорошо", "даю согласие", "передумал", "согласен дать"]
refuse_words = ["нет", "не даю", "не хочу", "отказ", "отмена", "no", "cancel", "я отказался"]
if any(w in text_lower for w in consent_words):
if _matches_any(text_lower, consent_words):
await handle_consent_yes(user_id, conv_id)
elif _matches_any(text_lower, refuse_words):
phones = config_reader.get_phones()
email = config_reader.get_site_mail()
phone_text = "\n".join(f"📞 {phone}" for phone, _ in phones)
try:
await max_api.send_message(
user_id,
f"Хорошо, без согласия мы не можем обработать обращение.\n\n"
f"Вы можете позвонить нам:\n{phone_text}\n\n"
f"📧 {email}\n\n"
f"Если передумаете — напишите «Даю согласие».",
)
except Exception as e:
logger.error(f"Failed to send refused_again message: {e}")
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)
phones = config_reader.get_phones()
email = config_reader.get_site_mail()
phone_text = "\n".join(f"📞 {phone}" for phone, _ in phones)
try:
await max_api.send_message(
user_id,
f"Хорошо, без согласия мы не можем обработать обращение.\n\n"
f"Вы можете позвонить нам:\n{phone_text}\n\n"
f"📧 {email}\n\n"
f"Если передумаете — напишите «Даю согласие».",
)
except Exception as e:
logger.error(f"Failed to send refused_again message: {e}")