NodeJS开发Telegram Bot无法接收其他Bot消息问题求助
解决Telegram Bot无法接收其他Bot消息的问题
问题根源
Telegram Bot API默认不会将其他Bot发送的消息推送给你的Bot,这是官方为避免Bot间循环互动设计的限制。此外,若你的Bot在目标群组中没有足够权限(如未开启"读取所有消息"的管理员权限),也会导致无法接收消息。
解决方案
配置Bot群组权限
- 将你的Bot添加为目标群组的管理员
- 开启管理员权限中的读取所有消息选项
修改Bot初始化代码
在创建TelegramBot实例时,添加allowed_updates参数,明确指定接收message类型的更新,这样Telegram会推送所有消息(包括其他Bot发送的)。
修改后的完整代码
const express = require("express"); const app = express(); const cors = require('cors'); const TelegramBot = require('node-telegram-bot-api'); const {log} = require("util"); app.use(cors()); const port = 2023; const token = '******************'; const chatId = '-100*********'; const sendChatId ='-100*************'; // 关键修改:添加allowed_updates参数 const bot = new TelegramBot(token, { polling: true, allowed_updates: ['message'] }); bot.on('message', (msg) => { console.log("Start"); console.log(msg); if (msg.chat.id.toString() === chatId) { if(msg.entities && msg.entities.length > 4){ bot.sendPhoto(sendChatId, msg.entities[1].url, { caption: msg.text }) .then((sentMessage) => { console.log('Resimli mesaj gönderildi:', sentMessage.caption); }) .catch((error) => { console.error('Resimli mesaj gönderme hatası:', error); }); }else if(msg.photo && msg.photo.length >= 4){ bot.sendPhoto(sendChatId, msg.photo[3].file_id, { caption: msg.caption }) .then((sentMessage) => { console.log('Resimli mesaj gönderildi:', sentMessage.caption); }) .catch((error) => { console.error('Resimli mesaj gönderme hatası:', error); }); } else{ bot.sendMessage(sendChatId, (msg.text && msg.text.length) > 0 ? msg.text : msg.caption) .then((sentMessage) => { console.log('Metin mesajı gönderildi:', sentMessage.text); }) .catch((error) => { console.error('Metin mesajı gönderme hatası:', error); }); } } console.log("End"); }); app.use(express.json()); app.use( express.urlencoded({ extended: true, }) ); app.get("/", (req, res) => { res.json({ message: "Success" }); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
内容的提问来源于stack exchange,提问作者Ayyub Hajiyev
相关产品推荐
相关产品推荐

