Update notification service

This commit is contained in:
MacRimi
2026-03-22 22:24:17 +01:00
parent 147ca0a41a
commit 4b99de8841
2 changed files with 35 additions and 8 deletions

View File

@@ -24,6 +24,7 @@ interface ChannelConfig {
rich_format?: boolean
bot_token?: string
chat_id?: string
topic_id?: string // Telegram topic ID for supergroups with topics
url?: string
token?: string
webhook_url?: string
@@ -1126,6 +1127,16 @@ export function NotificationSettings() {
onChange={e => updateChannel("telegram", "chat_id", e.target.value)}
/>
</div>
<div className="space-y-1.5">
<Label className="text-[11px] text-muted-foreground">Topic ID <span className="text-muted-foreground/60">(optional)</span></Label>
<Input
className="h-7 text-xs font-mono"
placeholder="123456"
value={config.channels.telegram?.topic_id || ""}
onChange={e => updateChannel("telegram", "topic_id", e.target.value)}
/>
<p className="text-[10px] text-muted-foreground">For supergroups with topics enabled. Leave empty for regular chats.</p>
</div>
{/* Message format */}
<div className="flex items-center justify-between py-1">
<div>

View File

@@ -135,7 +135,7 @@ class TelegramChannel(NotificationChannel):
'UNKNOWN': '\u26AA', # white circle
}
def __init__(self, bot_token: str, chat_id: str):
def __init__(self, bot_token: str, chat_id: str, topic_id: str = ''):
super().__init__()
token = bot_token.strip()
# Strip 'bot' prefix if user included it (API_BASE already adds it)
@@ -143,6 +143,8 @@ class TelegramChannel(NotificationChannel):
token = token[3:]
self.bot_token = token
self.chat_id = chat_id.strip()
# Topic ID for supergroups with topics enabled (message_thread_id)
self.topic_id = topic_id.strip() if topic_id else ''
def validate_config(self) -> Tuple[bool, str]:
if not self.bot_token:
@@ -177,6 +179,12 @@ class TelegramChannel(NotificationChannel):
'chat_id': self.chat_id,
'photo': photo_url,
}
# Add topic ID for supergroups with topics enabled
if self.topic_id:
try:
payload['message_thread_id'] = int(self.topic_id)
except ValueError:
pass
if caption:
payload['caption'] = caption[:1024] # Telegram caption limit
payload['parse_mode'] = 'HTML'
@@ -204,13 +212,20 @@ class TelegramChannel(NotificationChannel):
def _post_message(self, text: str) -> Tuple[int, str]:
url = self.API_BASE.format(token=self.bot_token)
payload = json.dumps({
payload_dict = {
'chat_id': self.chat_id,
'text': text,
'parse_mode': 'HTML',
'disable_web_page_preview': True,
}).encode('utf-8')
}
# Add topic ID for supergroups with topics enabled
if self.topic_id:
try:
payload_dict['message_thread_id'] = int(self.topic_id)
except ValueError:
pass # Invalid topic_id, skip
payload = json.dumps(payload_dict).encode('utf-8')
return self._http_request(url, payload, {'Content-Type': 'application/json'})
def _split_message(self, text: str) -> list:
@@ -892,11 +907,12 @@ def create_channel(channel_type: str, config: Dict[str, str]) -> Optional[Notifi
Channel instance or None if creation fails
"""
try:
if channel_type == 'telegram':
return TelegramChannel(
bot_token=config.get('bot_token', ''),
chat_id=config.get('chat_id', '')
)
if channel_type == 'telegram':
return TelegramChannel(
bot_token=config.get('bot_token', ''),
chat_id=config.get('chat_id', ''),
topic_id=config.get('topic_id', '')
)
elif channel_type == 'gotify':
return GotifyChannel(
server_url=config.get('url', ''),