Add a Telegram bot client with setup tooling for admin alerts
Bot API client (apps/integration/telegram.py):
- sendMessage, getMe, getUpdates over requests, no extra dependencies
- Telegram API errors are surfaced with the provider description
("chat not found", "bot was blocked") instead of a bare HTTP status
- messages longer than the Bot API limit are truncated with a marker
Notifications:
- notifications.py now goes through the client; failures are logged and
never break the exchange
Setup helpers:
- telegram_check: verifies the token, prints the bot name, sends a test
message, and suggests chats when chat_id is missing
- telegram_chats: lists chats that wrote to the bot, --apply saves the
chat_id when exactly one chat is known
- admin action "Detect chat_id from bot messages" does the same in the UI,
with explicit messages for no chats, many chats and API errors
- admin help text explains the BotFather -> /start -> test flow
Docs: Telegram setup section, command reference and limitations
(4096-char limit, negative chat_id for groups/channels, no internet =
no Telegram).
Tests: 12 new cases for the client, both commands and the admin action;
the suite is 45 passing.
This commit is contained in:
1 parent
e1cc6dbaad
commit
b18e3ea703
7 files changed
+581
-21
No files matched your search
@@ -8,17 +8,15 @@ exchange itself, so every transport swallows its own errors and only logs them.
|
||||
|
||||
import logging
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django.core.mail import send_mail
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import NotificationSettings
|
||||
from .telegram import TelegramError, send_message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TELEGRAM_API_URL = "https://api.telegram.org/bot{token}/sendMessage"
|
||||
TELEGRAM_TIMEOUT = 10
|
||||
ADMIN_SYNC_URL = "/admin/integration/syncrun/{pk}/change/"
|
||||
|
||||
|
||||
@@ -43,21 +41,18 @@ def _send_email(config: NotificationSettings, subject: str, message: str) -> boo
|
||||
def _send_telegram(config: NotificationSettings, subject: str, message: str) -> bool:
|
||||
if not config.telegram_configured:
|
||||
return False
|
||||
url = TELEGRAM_API_URL.format(token=config.telegram_bot_token)
|
||||
try:
|
||||
response = requests.post(
|
||||
url,
|
||||
json={
|
||||
"chat_id": config.telegram_chat_id,
|
||||
"text": f"{subject}\n\n{message}",
|
||||
"disable_web_page_preview": True,
|
||||
},
|
||||
timeout=TELEGRAM_TIMEOUT,
|
||||
send_message(
|
||||
config.telegram_bot_token,
|
||||
config.telegram_chat_id,
|
||||
f"{subject}\n\n{message}",
|
||||
)
|
||||
response.raise_for_status()
|
||||
except Exception as exc: # noqa: BLE001 - alerting must never break the workflow
|
||||
except TelegramError as exc: # noqa: BLE001 - alerting must never break the workflow
|
||||
logger.warning("Не удалось отправить оповещение в Telegram: %s", exc)
|
||||
return False
|
||||
except Exception: # noqa: BLE001 - safety net for unexpected transport errors
|
||||
logger.exception("Непредвиденная ошибка при отправке в Telegram")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user