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
This commit is contained in:
@@ -1,32 +1,92 @@
|
||||
from app.settings_cache import SettingsCache
|
||||
from app.conversation import get_or_create_user, get_open_conversation, get_template_rendered
|
||||
from app.fsm import BotState, FSM
|
||||
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_handoff_request(bot, user_data: dict, db, max_api):
|
||||
max_user_id = user_data["user_id"]
|
||||
user = await get_or_create_user(db, max_user_id)
|
||||
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()
|
||||
|
||||
if not SettingsCache.is_work_hours():
|
||||
from app.handlers.out_of_hours import handle_out_of_hours
|
||||
await handle_out_of_hours(bot, user_data, db, max_api)
|
||||
return
|
||||
conv_id = conv.id if conv else 0
|
||||
|
||||
text = await get_template_rendered(db, "handoff")
|
||||
await max_api.send_message(max_user_id, text)
|
||||
|
||||
conv = await get_open_conversation(db, user.id)
|
||||
if conv:
|
||||
conv.status = "handoff"
|
||||
conv.priority = "high"
|
||||
await db.flush()
|
||||
|
||||
from app.integrations import _1c_unf as ic_integration
|
||||
await ic_integration.send_to_1c(db, conv)
|
||||
|
||||
FSM.set_state(max_user_id, BotState.HANDOFF_REQUEST)
|
||||
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_inquiry_fallback(bot, user_data, db, max_api):
|
||||
from app.handlers.inquiry import handle_inquiry
|
||||
await handle_inquiry(bot, user_data, db, max_api)
|
||||
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"
|
||||
"Опишите, пожалуйста, суть вашего обращения — "
|
||||
"расскажите подробнее, чем мы можем вам помочь.",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user