init
This commit is contained in:
48
backend/app/services/telegram_notify.py
Normal file
48
backend/app/services/telegram_notify.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
|
||||
logger = logging.getLogger("app.services.telegram")
|
||||
|
||||
|
||||
async def notify_new_order(
|
||||
order_id: str,
|
||||
client_name: str,
|
||||
client_phone: str,
|
||||
material_name: str,
|
||||
total_rub: float,
|
||||
comment: str | None = None,
|
||||
) -> None:
|
||||
"""Send a notification about a new order to Telegram."""
|
||||
logger.info("=== Telegram notification ===")
|
||||
logger.info("Order: %s, client: %s, phone: %s, material: %s, total: %.2f RUB",
|
||||
order_id, client_name, client_phone, material_name, total_rub)
|
||||
|
||||
if not settings.TELEGRAM_BOT_TOKEN or not settings.TELEGRAM_CHAT_ID:
|
||||
logger.warning("Telegram credentials not configured (token=%s, chat_id=%s), skipping",
|
||||
"set" if settings.TELEGRAM_BOT_TOKEN else "empty",
|
||||
"set" if settings.TELEGRAM_CHAT_ID else "empty")
|
||||
return
|
||||
|
||||
text = (
|
||||
f"\U0001f195 Новый заказ #{order_id}\n"
|
||||
f"Клиент: {client_name}\n"
|
||||
f"Телефон: {client_phone}\n"
|
||||
f"Материал: {material_name}\n"
|
||||
f"Сумма: {total_rub} \u20bd\n"
|
||||
f"Комментарий: {comment or '\u2014'}"
|
||||
)
|
||||
|
||||
url = f"https://api.telegram.org/bot{settings.TELEGRAM_BOT_TOKEN}/sendMessage"
|
||||
logger.debug("Sending to Telegram: chat_id=%s, text_length=%d", settings.TELEGRAM_CHAT_ID, len(text))
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
resp = await client.post(url, json={"chat_id": settings.TELEGRAM_CHAT_ID, "text": text})
|
||||
logger.info("Telegram response: status=%d, body=%s", resp.status_code, resp.text[:200])
|
||||
if resp.status_code != 200:
|
||||
logger.error("Telegram API error: %s", resp.text)
|
||||
except Exception:
|
||||
logger.exception("Failed to send Telegram notification")
|
||||
Reference in New Issue
Block a user