Files
site_aegisone/max_bot/app/handlers/handoff.py
T
angel 72b6879f4b v1.7.0: refactor max_bot to flat structure, add VCF+UserModel+NLP history context, portal pages and proxy fixes
- Refactored max_bot from nested packages to flat module structure
- Q2: Extended BotUser model (patronymic, email, org, address, vcf_raw, contact_hash, phone_verified, email_verified, last_interaction, total_conversations, total_tickets)
- Q2: VCF parser (FN, N, TEL, EMAIL, ORG, ADR), upsert on re-contact, NLP history context (_get_user_history_context -> YandexGPT)
- Q1: Broadcast preview modal with 10s confirmation timer
- Q3: CSS var(--white)->var(--bg-card), var(--text)->var(--text-primary)
- Q4: bot_settings showNotification(), editable max_bot_id
- Q5: Webhook secret passthrough via X-Max-Bot-Api-Secret
- Masking sensitive keys, dialog_cleared handler, migrate via _add_column_if_not_exists()
- Rate limit (asyncio.sleep 0.5 per 10), dead code removed, conv.intent context in contact.py
- Portal pages: bot_consent, bot_kb (edit), bot_settings, bot_test, bot_tickets, portal_settings
- Tests: 21/21 passing, added test_yandex_gpt.py, test_email_sender.py
- Deploy: deploy_full.sh, schema.sql, seed_knowledge_base.sql
2026-05-29 02:30:30 +03:00

93 lines
3.2 KiB
Python

import datetime
import logging
from sqlalchemy import select
from app.database import async_session
from app.models import BotConversation, BotMessage
from app.max_api import max_api
from app.keyboards import consent_keyboard
logger = logging.getLogger(__name__)
async def handle_callback(user_id: int, callback_id: str, callback_data: dict) -> None:
async with async_session() as db:
result = await db.execute(
select(BotConversation)
.where(BotConversation.user_id == user_id)
.order_by(BotConversation.id.desc())
.limit(1)
)
conv = result.scalar_one_or_none()
conv_id = conv.id if conv else 0
if callback_id.startswith("consent_"):
from app.handlers.consent import handle_consent_callback
await handle_consent_callback(user_id, conv_id, callback_id)
else:
await max_api.send_message(
user_id,
"Извините, я не распознала действие. Пожалуйста, воспользуйтесь кнопками.",
)
async def handle_contact_share(
user_id: int,
conv_id: int,
phone: str,
vcf_info: str = "",
vcf_data: dict = None,
contact_hash: str = "",
) -> None:
if vcf_data is None:
vcf_data = {}
async with async_session() as db:
from app.models import BotUser
user_result = await db.execute(select(BotUser).where(BotUser.id == user_id))
user = user_result.scalar_one_or_none()
if user:
user.phone = phone or user.phone
if vcf_data.get("first_name"):
user.first_name = vcf_data["first_name"]
if vcf_data.get("last_name"):
user.last_name = vcf_data["last_name"]
if vcf_data.get("patronymic"):
user.patronymic = vcf_data["patronymic"]
if vcf_data.get("email"):
user.email = vcf_data["email"]
if vcf_data.get("organization"):
user.organization = vcf_data["organization"]
if vcf_data.get("address"):
user.address = vcf_data["address"]
if vcf_info:
user.vcf_raw = vcf_info
if contact_hash:
user.contact_hash = contact_hash
user.phone_verified = True
if phone:
user.phone_verified = True
msg = BotMessage(
conversation_id=conv_id,
direction="incoming",
text=f"[Контакт поделен] {phone or vcf_data.get('phone', '')}",
created_at=datetime.datetime.utcnow(),
)
db.add(msg)
conv_result = await db.execute(
select(BotConversation).where(BotConversation.id == conv_id)
)
conv = conv_result.scalar_one_or_none()
if conv:
conv.state = "awaiting_inquiry"
await db.commit()
await max_api.send_message(
user_id,
"Спасибо! Ваш контакт получен.\n\n"
"Опишите, пожалуйста, суть вашего обращения — "
"расскажите подробнее, чем мы можем вам помочь.",
)