我正在用nodejs开发一个Telegram机器人。我创建了一个 node-telegram-bot-api 实例,并使用 on('photo') 方法来管理用户是否向我的机器人发送照片。
问题是,当用户通过从图库中多次选择照片来一起发送多张照片时,因为我的机器人响应的次数与发送的照片的次数一样多。我认为发生这种情况是因为机器人执行 on('photo') 方法的次数与发送照片的次数一样多。
bot.on('photo', function (msg) {
var fileId = msg.photo[2].file_id;
bot.downloadFile(fileId, folder);
bot.sendMessage(chatId, "I got the photo", keyboard);
bot.sendMessage(chatId, "Do you want to go to the next step of the procedure?", keyboard);
//I would like that the bot sends the last message only once
我希望机器人只响应一次。
你有什么建议吗?
最佳答案
// Init value to check is sent
var responded = False;
bot.on('photo', function (msg) {
var fileId = msg.photo[2].file_id;
bot.downloadFile(fileId, folder);
// If still false, send it!
if (!responded) {
bot.sendMessage(chatId, "I got the photo", keyboard);
// Message is sent, put responded on true
responded = True
}
else {
bot.sendMessage(chatId, "Do you want to go to the next step of the procedure?", keyboard);
}
}
关于javascript - 如果用户同时发送多张照片,如何让机器人响应一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56848591/