Files
office_translator/utils/telegram.py
sepehr 29753881a6
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2m34s
feat: add Telegram notifications for user signup and Stripe events
2026-06-07 11:37:01 +02:00

38 lines
1.3 KiB
Python

import os
import logging
import httpx
logger = logging.getLogger(__name__)
async def send_telegram_notification(message: str) -> bool:
"""
Envoie un message formaté en Markdown à l'administrateur via Telegram.
Utilise les variables d'environnement TELEGRAM_BOT_TOKEN et TELEGRAM_CHAT_ID.
"""
bot_token = os.getenv("TELEGRAM_BOT_TOKEN")
chat_id = os.getenv("TELEGRAM_CHAT_ID")
if not bot_token or not chat_id:
logger.warning("Notification Telegram ignorée : TELEGRAM_BOT_TOKEN ou TELEGRAM_CHAT_ID non configuré.")
return False
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {
"chat_id": chat_id,
"text": message,
"parse_mode": "Markdown"
}
try:
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.post(url, json=payload)
if resp.status_code == 200:
logger.info("Notification Telegram envoyée avec succès.")
return True
else:
logger.error(f"Échec de l'envoi Telegram (HTTP {resp.status_code}): {resp.text}")
return False
except Exception as e:
logger.error(f"Erreur lors de l'envoi de la notification Telegram : {e}")
return False