Files
customerTicket/apps/integrations/bale/providers.py
T
2026-09-10 11:34:19 +03:30

31 lines
1.4 KiB
Python

from dataclasses import dataclass
import requests
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
@dataclass
class NotificationResult:
success: bool
provider_id: str = ""
error: str = ""
class ConsoleBaleProvider:
def send_message(self, chat_id, text):
if settings.DEBUG: print(f"[DEV BALE] {chat_id or 'dashboard'}: {text}")
return NotificationResult(True, "console")
class BaleBotProvider:
def send_message(self, chat_id, text):
if not chat_id: return NotificationResult(False, error="Bale chat id is not configured")
response = requests.post(f"{settings.BALE_API_BASE.rstrip('/')}/bot{settings.BALE_BOT_TOKEN}/sendMessage", json={"chat_id": chat_id, "text": text, "disable_web_page_preview": True}, timeout=settings.INTEGRATION_HTTP_TIMEOUT_SECONDS)
response.raise_for_status(); data = response.json()
if not data.get("ok", True): return NotificationResult(False, error=str(data.get("description", "Bale rejected message")))
return NotificationResult(True, str((data.get("result") or {}).get("message_id", "bale")))
def get_bale_provider():
if settings.BALE_BOT_TOKEN:
return BaleBotProvider()
if settings.DEBUG:
return ConsoleBaleProvider()
raise ImproperlyConfigured("BALE_BOT_TOKEN is required to deliver Bale notifications in production.")