Coverage for notifications/adapters/telegram.py: 100.00%
26 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 18:53 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 18:53 +0000
1"""Telegram, through the shared MT bot."""
3from __future__ import annotations
5import os
6from typing import ClassVar
8from telegram_notify import TelegramClient, escape
10from notifications.adapters.base import ChannelAdapter, DeliveryResult, NotificationConfigError
13class TelegramAdapter(ChannelAdapter):
14 id: ClassVar[str] = "telegram"
15 label: ClassVar[str] = "Telegram"
16 icon: ClassVar[str] = "✈️"
17 invite_pattern: ClassVar[str] = r"^https://t\.me/(\+|joinchat/)[A-Za-z0-9_-]{8,64}$"
18 invite_error: ClassVar[str] = "Must be a Telegram invite link (https://t.me/+...)"
19 destination_label: ClassVar[str] = "Chat ID"
20 placeholder: ClassVar[str] = "e.g. -1001234567890 or @channelname"
21 hint: ClassVar[str] = (
22 "Paste the chat_id for a group the Missing Table bot has been added to, or an @channel username."
23 )
24 setup_steps: ClassVar[tuple[str, ...]] = (
25 "Add @missingtable_bot to your Telegram group or channel. In a channel, make it an admin so it can post.",
26 "Find the chat ID: open the group at web.telegram.org and copy the number after # "
27 "in the address bar (e.g. -1001234567890).",
28 "A public group or channel can use its @username instead.",
29 "Paste it here and click Send test. If the test fails and the number does not start "
30 "with -100, add -100 in front and try again.",
31 )
32 destination_pattern: ClassVar[str] = r"^(@\w+|-?\d+)$"
33 destination_error: ClassVar[str] = (
34 "Telegram destination must be a numeric chat_id (e.g. -1001234567890) or @channel"
35 )
37 def check_config(self) -> None:
38 if not os.getenv("TELEGRAM_BOT_TOKEN"):
39 raise NotificationConfigError("TELEGRAM_BOT_TOKEN is not set — cannot send Telegram notifications.")
41 def _deliver(self, destination: str, content: str) -> DeliveryResult:
42 self.check_config()
43 client = TelegramClient(bot_token=os.environ["TELEGRAM_BOT_TOKEN"], chat_id=destination)
44 response = client.send(escape(content))
45 message_id = (response.get("result") or {}).get("message_id")
46 return DeliveryResult(message_ref=None if message_id is None else str(message_id))