from app.settings_cache import SettingsCache from app.conversation import ( get_or_create_user, get_open_conversation, create_conversation, add_message, auto_categorize_text, get_template_rendered ) from app.integrations.yandex_gpt import categorize_with_gpt from app.keyboards.inline import build_category_buttons from app.fsm import BotState, FSM async def handle_inquiry(bot, user_data: dict, db, max_api): max_user_id = user_data["user_id"] text = user_data.get("text", "").strip() user = await get_or_create_user(db, max_user_id) if not text: await max_api.send_message(max_user_id, "Пожалуйста, опишите ваш вопрос.") return conv = await get_open_conversation(db, user.id) if not conv: conv = await create_conversation(db, user.id) conv.inquiry_text = text has_attachment = user_data.get("has_attachment", False) attachment_type = user_data.get("attachment_type", "") attachment_path_str = user_data.get("attachment_path", "") if has_attachment: conv.has_attachment = True conv.attachment_type = attachment_type conv.attachment_path = attachment_path_str await add_message(db, conv.id, "in", text, has_attachment, attachment_type, attachment_path_str) if has_attachment and attachment_path_str and max_api: try: file_url = await max_api.get_file_url(attachment_path_str) if file_url: import os import aiohttp from app.config import get_settings from app.models.models import FileSyncQueue settings = get_settings() local_dir = settings.UPLOADS_DIR os.makedirs(local_dir, exist_ok=True) local_filename = f"conv_{conv.id}_{int(datetime.now().timestamp())}_{attachment_type}" local_path = os.path.join(local_dir, local_filename) async with aiohttp.ClientSession() as session: async with session.get(file_url) as resp: if resp.status == 200: with open(local_path, "wb") as f: f.write(await resp.read()) sync_entry = FileSyncQueue( local_path=local_path, remote_path=f"/AegisOne_Service/bot/{local_filename}", entity_type="conversation", entity_id=conv.id, status="pending", ) db.add(sync_entry) except Exception: pass gpt_key = SettingsCache.get("yandex_gpt_key") if gpt_key: cat_name = await categorize_with_gpt(text) if cat_name: from sqlalchemy import select from app.models.models import BotCategory result = await db.execute( select(BotCategory).where( BotCategory.name.ilike(f"%{cat_name}%"), BotCategory.active == True ).limit(1) ) cat = result.scalar_one_or_none() if cat: conv.inquiry_type = cat.id await db.flush() confirm_text = await get_template_rendered(db, "auto_categorize_confirm", category=cat.name) from app.keyboards.inline import build_category_buttons buttons = build_category_buttons() await max_api.send_message_with_keyboard(max_user_id, confirm_text, buttons) FSM.set_state(max_user_id, BotState.CATEGORY_SELECT) return else: cat = await auto_categorize_text(db, text) if cat: conv.inquiry_type = cat.id await db.flush() await db.flush() ooh = not SettingsCache.is_work_hours() if ooh: start = SettingsCache.get_int("work_hours_start", 9) end = SettingsCache.get_int("work_hours_end", 18) ooh_text = await get_template_rendered(db, "out_of_hours", work_hours_start=start, work_hours_end=end) await max_api.send_message(max_user_id, ooh_text) await _confirm_inquiry(bot, user_data, db, max_api, conv, ooh) async def _confirm_inquiry(bot, user_data, db, max_api, conv, is_ooh: bool): max_user_id = user_data["user_id"] user = await get_or_create_user(db, max_user_id) if is_ooh: text = await get_template_rendered(db, "confirmation_ooh", first_name=user.first_name, conv_id=conv.id) else: text = await get_template_rendered(db, "confirmation", first_name=user.first_name, conv_id=conv.id) await max_api.send_message(max_user_id, text, format="markdown") from app.integrations import _1c_unf as ic_integration await ic_integration.send_to_1c(db, conv) FSM.set_state(max_user_id, BotState.CONFIRMATION)