如何让Node.js开发的Telegram自动转发机器人无引用转发消息?
解决Telegram机器人转发消息无引用标识的方案
问题出在你使用的forwardMessage方法本身会保留原消息的引用信息,要实现无引用的“直接发送”效果,需要重新构造消息内容并调用对应类型的发送接口,而非直接转发。
以下是修改后的完整代码,支持文本、图片、文档、视频等常见消息类型的无引用发送:
const TelegramBot = require('node-telegram-bot-api'); // 替换为你的实际ID和Token const sourceChannelId = YOUR_SOURCE_CHANNEL_ID; const targetGroupId = YOUR_TARGET_GROUP_ID; const botToken = 'YOUR_BOT_TOKEN'; const bot = new TelegramBot(botToken, { polling: true }); bot.on('channel_post', async (msg) => { if (msg.chat.id !== sourceChannelId) return; try { // 根据消息类型选择对应发送方法 if (msg.text) { // 处理文本消息 await bot.sendMessage(targetGroupId, msg.text, { parse_mode: msg.parse_mode, // 保留原消息格式(如Markdown/HTML) disable_web_page_preview: msg.disable_web_page_preview // 保留原消息链接预览设置 }); } else if (msg.photo) { // 处理图片消息(取分辨率最高的版本) const highestResPhoto = msg.photo[msg.photo.length - 1]; await bot.sendPhoto(targetGroupId, highestResPhoto.file_id, { caption: msg.caption, // 保留图片说明 parse_mode: msg.caption_parse_mode // 保留说明格式 }); } else if (msg.document) { // 处理文档消息 await bot.sendDocument(targetGroupId, msg.document.file_id, { caption: msg.caption, parse_mode: msg.caption_parse_mode }); } else if (msg.video) { // 处理视频消息 await bot.sendVideo(targetGroupId, msg.video.file_id, { caption: msg.caption, parse_mode: msg.caption_parse_mode }); } // 可按需扩展其他类型:音频、语音、贴纸等 } catch (error) { console.error('发送消息失败:', error); } });
关键说明:
- 放弃
forwardMessage,改用sendMessage/sendPhoto等原生发送方法,这类方法不会携带原消息的引用标识。 - 针对不同消息类型做分支处理,确保原消息的内容(文本、媒体、说明、格式)完整保留。
- 加入
async/await处理异步操作,同时捕获异常避免机器人崩溃。
注意事项:
- 确保机器人已加入源频道并拥有读取消息的权限。
- 确保机器人在目标群组中拥有发送消息、上传媒体的权限。
- 替换代码中的占位符为你的实际频道ID、群组ID和机器人Token。
内容的提问来源于stack exchange,提问作者Yusuf Mescioğlu
相关产品推荐
相关产品推荐

