102 lines
4.1 KiB
Python
102 lines
4.1 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
|
|
|
|
_STEPS = ["service_type", "scope", "address", "comment"]
|
|
_STEP_LABELS = {
|
|
"service_type": "Тип услуги (Видеонаблюдение, СКУД, Пожарная сигнализация, IT-инфраструктура, Обслуживание, Другое)",
|
|
"scope": "Объём работ (краткое описание)",
|
|
"address": "Адрес объекта",
|
|
"comment": "Дополнительные пожелания (или отправьте «-» для пропуска)",
|
|
}
|
|
|
|
_COLLECT_KEY = "commercial_data"
|
|
_STEP_KEY = "commercial_step"
|
|
|
|
|
|
async def handle_commercial(bot, user_data: dict, db, max_api):
|
|
max_user_id = user_data["user_id"]
|
|
text = user_data.get("text", "").strip()
|
|
state = FSM.get_state(max_user_id)
|
|
|
|
if state != BotState.FEATURE_COMMERCIAL:
|
|
FSM.set_state(max_user_id, BotState.FEATURE_COMMERCIAL)
|
|
FSM.set_temp(max_user_id, {_STEP_KEY: 0, _COLLECT_KEY: {}})
|
|
await max_api.send_message(
|
|
max_user_id,
|
|
f"📄 **Коммерческое предложение**\n\nШаг 1/{len(_STEPS)}:\n{_STEP_LABELS['service_type']}"
|
|
)
|
|
return
|
|
|
|
tmp = FSM.get_temp(max_user_id)
|
|
step = tmp.get(_STEP_KEY, 0)
|
|
data = tmp.get(_COLLECT_KEY, {})
|
|
|
|
if step >= len(_STEPS):
|
|
await _finish_commercial(bot, user_data, db, max_api, data)
|
|
return
|
|
|
|
step_key = _STEPS[step]
|
|
if not text and step_key != "comment":
|
|
await max_api.send_message(max_user_id, f"Пожалуйста, заполните поле:\n{_STEP_LABELS[step_key]}")
|
|
return
|
|
|
|
data[step_key] = text if text else ""
|
|
step += 1
|
|
|
|
if step < len(_STEPS):
|
|
FSM.set_temp(max_user_id, {_STEP_KEY: step, _COLLECT_KEY: data})
|
|
next_key = _STEPS[step]
|
|
await max_api.send_message(
|
|
max_user_id,
|
|
f"📄 Шаг {step + 1}/{len(_STEPS)}:\n{_STEP_LABELS[next_key]}"
|
|
)
|
|
else:
|
|
await _finish_commercial(bot, user_data, db, max_api, data)
|
|
|
|
|
|
async def _finish_commercial(bot, user_data, db, max_api, data):
|
|
max_user_id = user_data["user_id"]
|
|
user = await get_or_create_user(db, max_user_id)
|
|
summary = (
|
|
f"📄 **Коммерческое предложение**\n\n"
|
|
f"Услуга: {data.get('service_type', '—')}\n"
|
|
f"Объём: {data.get('scope', '—')}\n"
|
|
f"Адрес: {data.get('address', '—')}\n"
|
|
f"Комментарий: {data.get('comment', '—')}\n\n"
|
|
f"Спасибо, {user.first_name}! Мы подготовим КП и свяжемся с вами."
|
|
)
|
|
await max_api.send_message(max_user_id, summary, format="markdown")
|
|
|
|
conv_text = (
|
|
f"Коммерческое предложение: услуга={data.get('service_type', '')}, "
|
|
f"объём={data.get('scope', '')}, адрес={data.get('address', '')}, "
|
|
f"комментарий={data.get('comment', '')}"
|
|
)
|
|
conv = await _create_commercial_conv(db, user.id, conv_text)
|
|
from app.integrations import _1c_unf as ic_integration
|
|
await ic_integration.send_to_1c(db, conv)
|
|
|
|
FSM.clear_temp(max_user_id)
|
|
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)
|
|
|
|
|
|
async def _create_commercial_conv(db, user_id, details):
|
|
from app.conversation import create_conversation, add_message
|
|
from sqlalchemy import select
|
|
from app.models.models import BotCategory
|
|
result = await db.execute(
|
|
select(BotCategory).where(BotCategory.name == "Консультация").limit(1)
|
|
)
|
|
cat = result.scalar_one_or_none()
|
|
conv = await create_conversation(db, user_id)
|
|
conv.inquiry_text = details
|
|
conv.inquiry_type = cat.id if cat else None
|
|
await db.flush()
|
|
await add_message(db, conv.id, "in", details)
|
|
return conv
|