72b6879f4b
- 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
98 lines
2.7 KiB
Python
98 lines
2.7 KiB
Python
import httpx
|
|
from typing import Optional
|
|
from app.config import settings
|
|
|
|
|
|
class MaxAPI:
|
|
def __init__(self):
|
|
self.base_url = settings.max_api_base
|
|
self.token = settings.max_token
|
|
self.client = httpx.AsyncClient(timeout=30.0)
|
|
|
|
def _headers(self) -> dict:
|
|
return {
|
|
"Authorization": self.token,
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
async def get_me(self) -> dict:
|
|
r = await self.client.get(f"{self.base_url}/me", headers=self._headers())
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
async def subscribe_webhook(self) -> dict:
|
|
payload = {
|
|
"url": settings.webhook_url,
|
|
"update_types": settings.update_types,
|
|
"secret": settings.webhook_secret,
|
|
}
|
|
r = await self.client.post(
|
|
f"{self.base_url}/subscriptions",
|
|
headers=self._headers(),
|
|
json=payload,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
async def unsubscribe_webhook(self) -> dict:
|
|
r = await self.client.delete(
|
|
f"{self.base_url}/subscriptions",
|
|
headers=self._headers(),
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
async def get_subscriptions(self) -> dict:
|
|
r = await self.client.get(
|
|
f"{self.base_url}/subscriptions",
|
|
headers=self._headers(),
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
async def send_message(
|
|
self,
|
|
user_id: int,
|
|
text: str,
|
|
attachments: Optional[list] = None,
|
|
format: Optional[str] = None,
|
|
notify: bool = True,
|
|
) -> dict:
|
|
payload = {"text": text, "notify": notify}
|
|
if attachments:
|
|
payload["attachments"] = attachments
|
|
if format:
|
|
payload["format"] = format
|
|
r = await self.client.post(
|
|
f"{self.base_url}/messages?user_id={user_id}",
|
|
headers=self._headers(),
|
|
json=payload,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
async def answer_callback(
|
|
self,
|
|
callback_id: str,
|
|
message: Optional[dict] = None,
|
|
notification: Optional[str] = None,
|
|
) -> dict:
|
|
payload = {}
|
|
if message is not None:
|
|
payload["message"] = message
|
|
if notification is not None:
|
|
payload["notification"] = notification
|
|
r = await self.client.post(
|
|
f"{self.base_url}/answers?callback_id={callback_id}",
|
|
headers=self._headers(),
|
|
json=payload,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
async def close(self):
|
|
await self.client.aclose()
|
|
|
|
|
|
max_api = MaxAPI()
|