feat: notifications support Telegram topics in supergroups#493
feat: notifications support Telegram topics in supergroups#493jotka merged 1 commit intoFinsys:mainfrom
Conversation
|
I think this PR may not fully fix the original issue because it only supports the new Dockhand-specific syntax:
but the documented Apprise Telegram syntax is:
Notice the trailing slash. Apprise documents this as valid syntax, and also documents related forms like:
Apprise Telegram syntax: Telegram Bot API field: The current regex added by this PR is: /^tgram:\/\/([^/]+)\/([^:\/]+)(?::(\d+))?$/That matches: But does not match the Apprise-documented forms with a trailing slash: So if users copy the syntax from Apprise docs, the topic may still not be parsed/sent as intended. A possible minimal fix would be to accept the inline // tgram://bot_token/chat_id
// tgram://bot_token/chat_id/
// tgram://bot_token/chat_id:topic_id
// tgram://bot_token/chat_id:topic_id/
const match = appriseUrl.match(
/^tgram:\/\/([^/]+)\/([^:/?]+)(?::(\d+))?\/?$/
);
if (!match) {
return {
success: false,
error:
'Invalid Telegram URL format. Expected: tgram://bot_token/chat_id or tgram://bot_token/chat_id:topic_id/'
};
}
const [, botToken, chatId, topicIdStr] = match;
const topicId = topicIdStr ? Number.parseInt(topicIdStr, 10) : undefined;
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
const escapedTitle = escapeTelegramMarkdown(payload.title);
const escapedMessage = escapeTelegramMarkdown(payload.message);
const envTag = payload.environmentName
? ` \\[${escapeTelegramMarkdown(payload.environmentName)}\\]`
: '';
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
text: `*${escapedTitle}*${envTag}\n${escapedMessage}`,
...(Number.isFinite(topicId) ? { message_thread_id: topicId } : {}),
parse_mode: 'Markdown'
})
});If Dockhand wants to be a bit closer to Apprise compatibility, it could also support // tgram://bot_token/chat_id
// tgram://bot_token/chat_id/
// tgram://bot_token/chat_id:topic_id
// tgram://bot_token/chat_id:topic_id/
// tgram://bot_token/chat_id/?topic=topic_id
const match = appriseUrl.match(
/^tgram:\/\/([^/]+)\/([^:/?]+)(?::(\d+))?\/?(?:\?(.+))?$/
);
if (!match) {
return {
success: false,
error:
'Invalid Telegram URL format. Expected: tgram://bot_token/chat_id, tgram://bot_token/chat_id:topic_id/, or tgram://bot_token/chat_id/?topic=topic_id'
};
}
const [, botToken, chatId, inlineTopicId, queryString] = match;
const queryTopicId = queryString
? new URLSearchParams(queryString).get('topic')
: null;
const topicIdStr = inlineTopicId ?? queryTopicId ?? undefined;
const topicId = topicIdStr ? Number.parseInt(topicIdStr, 10) : undefined;
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
const escapedTitle = escapeTelegramMarkdown(payload.title);
const escapedMessage = escapeTelegramMarkdown(payload.message);
const envTag = payload.environmentName
? ` \\[${escapeTelegramMarkdown(payload.environmentName)}\\]`
: '';
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
text: `*${escapedTitle}*${envTag}\n${escapedMessage}`,
...(Number.isFinite(topicId) ? { message_thread_id: topicId } : {}),
parse_mode: 'Markdown'
})
});This still does not implement the full Apprise Telegram URL grammar, especially multiple chat IDs in one URL, but it should cover the exact syntax used in the original bug report and avoid silently excluding the documented trailing-slash form. /cc @jotka |
Proposed change
Add support for sending notifications to Topics in Telegram supergroups, as Apprise supports it
Type of change