对于如何使用 telegram bot api 在群聊中接收消息,我找到了 2 个解决方案:
不幸的是,如果我执行其中任何一项,Telegram 机器人将收到发送到该群组的所有消息(根据我的测试)。
但是,根据我读到的内容,只有当我提到机器人时才应该向机器人发送消息,例如@my-bot-name-bot
如何限制机器人仅在我提及时才接收消息?
如您所知,如果您的机器人是聊天管理员,则所有消息都会接收到机器人。没有办法改变这种行为。
仅收听通过用户名提及机器人的消息的最佳方法是,您可以编辑代码并明确查看消息是否包含机器人的用户名。
这是一个简单的 JavaScript 示例:
// (Change the bot username)
const username = "@my_cool_bot";
bot.on("message", async (ctx) => {
// Ignore the messages that doesn't mention the bot.
if (!ctx.message.text?.includes(username)) return;
// Process the message here...
})
希望这有帮助:)