From db25637dfc24b75d4fbbef83cf3039880c9d0bf6 Mon Sep 17 00:00:00 2001 From: Prudhvi Suraaj Date: Mon, 4 Aug 2025 08:01:51 +0530 Subject: [PATCH 1/3] update readme with new topicId messaging info --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index cf89885..3f72b37 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Send a message to a Telegram chat or channel. - `chatId`: Chat ID or username (e.g., @channelname or -1001234567890) - `text`: Message text +- `topicId`: Optional topic ID for forum channels - `parseMode`: Optional formatting (HTML, Markdown, MarkdownV2) - `disableWebPagePreview`: Optional boolean - `disableNotification`: Optional boolean for silent messages @@ -311,6 +312,8 @@ The system uses customizable templates for different message types: - **Contact**: Contact information - **Poll**: Poll questions and options +All message templates include topic ID information for forum channels, allowing the AI to understand the context of which topic thread the message belongs to. + ### Customization To customize the sampling behavior: @@ -367,6 +370,19 @@ Send a message to a Telegram channel: } ``` +Send a message to a specific topic in a forum channel: + +```json +{ + "tool_name": "SEND_MESSAGE", + "arguments": { + "chatId": "@mychannel", + "text": "Hello from the Telegram MCP Server!", + "topicId": 123 + } +} +``` + ### GET_CHANNEL_INFO Get information about a Telegram channel: From 3d74bd9ce5a324aab8c6448e18696449b27b97be Mon Sep 17 00:00:00 2001 From: Prudhvi Suraaj Date: Mon, 4 Aug 2025 08:02:35 +0530 Subject: [PATCH 2/3] adds support for messaging on forum groups via topic ids --- src/config.ts | 10 ++++++++++ src/sampling/handler.ts | 8 +++++++- src/sampling/message-handlers.ts | 9 +++++++++ src/sampling/types.ts | 1 + src/services/telegram-service.ts | 5 ++++- src/tools/send-message.ts | 5 +++++ 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index fd67a02..4b7e0aa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -157,6 +157,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.TEXT} content: {content}`, @@ -165,6 +166,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.PHOTO} caption: {caption} photo_info: {photoInfo}`, @@ -174,6 +176,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.DOCUMENT} filename: {fileName} mime_type: {mimeType} @@ -184,6 +187,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.VOICE} duration: {duration}s`, @@ -192,6 +196,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.VIDEO} caption: {caption} duration: {duration}s`, @@ -201,6 +206,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.STICKER} emoji: {stickerEmoji} set_name: {stickerSetName}`, @@ -210,6 +216,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.LOCATION} latitude: {latitude} longitude: {longitude}`, @@ -219,6 +226,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.CONTACT} contact_name: {contactName} phone_number: {phoneNumber}`, @@ -228,6 +236,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: ${MessageType.POLL} question: {pollQuestion} options: {pollOptions}`, @@ -237,6 +246,7 @@ user_id: {userId} chat_id: {chatId} isDM: {isDM} message_id: {messageId} +topic_id: {topicId} message_type: {messageType} content: {content}`, }, diff --git a/src/sampling/handler.ts b/src/sampling/handler.ts index 31a848b..aab7ae3 100644 --- a/src/sampling/handler.ts +++ b/src/sampling/handler.ts @@ -171,6 +171,7 @@ export class SamplingHandler { await this.telegramService.sendMessage( request.chatId, "Sorry, the AI service is not available right now.", + request.templateData.topicId, ); } return; @@ -212,7 +213,11 @@ export class SamplingHandler { // Send response back to Telegram (unless in silent mode) if (!samplingConfig.silentMode) { - await this.telegramService.sendMessage(request.chatId, responseText); + await this.telegramService.sendMessage( + request.chatId, + responseText, + request.templateData.topicId, + ); } console.log( @@ -225,6 +230,7 @@ export class SamplingHandler { await this.telegramService.sendMessage( request.chatId, "Sorry, I encountered an error while processing your request. Please try again.", + request.templateData.topicId, ); } } diff --git a/src/sampling/message-handlers.ts b/src/sampling/message-handlers.ts index cf3830f..ffb7369 100644 --- a/src/sampling/message-handlers.ts +++ b/src/sampling/message-handlers.ts @@ -12,6 +12,7 @@ export function handleTextMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.TEXT, }; } @@ -30,6 +31,7 @@ export function handlePhotoMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.PHOTO, }; } @@ -51,6 +53,7 @@ export function handleDocumentMessage( chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.DOCUMENT, }; } @@ -68,6 +71,7 @@ export function handleVoiceMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.VOICE, }; } @@ -86,6 +90,7 @@ export function handleVideoMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.VIDEO, }; } @@ -104,6 +109,7 @@ export function handleStickerMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.STICKER, }; } @@ -124,6 +130,7 @@ export function handleLocationMessage( chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.LOCATION, }; } @@ -142,6 +149,7 @@ export function handleContactMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.CONTACT, }; } @@ -160,6 +168,7 @@ export function handlePollMessage(ctx: Context): MessageTemplateData | null { chatId: ctx.chat.id, isDM: ctx.chat.id === msg.from.id, messageId: msg.message_id, + topicId: msg.message_thread_id, messageType: MessageType.POLL, }; } diff --git a/src/sampling/types.ts b/src/sampling/types.ts index 6f01b53..492ad52 100644 --- a/src/sampling/types.ts +++ b/src/sampling/types.ts @@ -52,6 +52,7 @@ export interface MessageTemplateData { chatId: number; isDM: boolean; messageId: number; + topicId?: number; messageType?: string; caption?: string; photoInfo?: string; diff --git a/src/services/telegram-service.ts b/src/services/telegram-service.ts index dab109d..9f4e28f 100644 --- a/src/services/telegram-service.ts +++ b/src/services/telegram-service.ts @@ -30,9 +30,12 @@ export class TelegramService { async sendMessage( chatId: string | number, text: string, + topicId?: number, ): Promise { try { - const message = await this.bot.telegram.sendMessage(chatId, text); + const message = await this.bot.telegram.sendMessage(chatId, text, { + message_thread_id: topicId, + }); return { messageId: message.message_id, diff --git a/src/tools/send-message.ts b/src/tools/send-message.ts index 02395e9..fb9cc12 100644 --- a/src/tools/send-message.ts +++ b/src/tools/send-message.ts @@ -9,6 +9,10 @@ const sendMessageParams = z.object({ "The chat ID or channel username (e.g., @channelname or -1001234567890)", ), text: z.string().min(1).describe("The message text to send"), + topicId: z + .number() + .optional() + .describe("The topic ID for forum channels (optional)"), }); type SendMessageParams = z.infer; @@ -24,6 +28,7 @@ export const sendMessageTool = { const messageInfo = await telegramService.sendMessage( params.chatId, params.text, + params.topicId, ); return dedent` From 90dc661146b92823f9cb49d37f64be124f88bf69 Mon Sep 17 00:00:00 2001 From: Prudhvi Suraaj Date: Mon, 4 Aug 2025 08:10:57 +0530 Subject: [PATCH 3/3] create --- .changeset/cyan-masks-fail.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cyan-masks-fail.md diff --git a/.changeset/cyan-masks-fail.md b/.changeset/cyan-masks-fail.md new file mode 100644 index 0000000..43cc49e --- /dev/null +++ b/.changeset/cyan-masks-fail.md @@ -0,0 +1,5 @@ +--- +"@iqai/mcp-telegram": patch +--- + +Added support for messaging on topic channels of forum type groups