javascript - 如果用户同时发送多张照片,如何让机器人响应一次?

标签 javascript node.js npm telegram-bot node-telegram-bot-api

我正在用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/

相关文章:

node.js - node.js require.paths.unshift() 的问题

javascript - dynamodb nodejs/lambda date.now()

node.js - 更新 npm 到 @angular 2 不可用

javascript - 什么时候使用 Yarn 而不是 NPM?有什么区别?

javascript - 在nodejs中运行cron作业

javascript - 很难将我的 Jquery 分离到单独的 js 文件中

javascript - Node Nodemon 错误 : Cannot find module 'C:\Program Files\Git\node_modules\nodemon\bin\nodemon.js'

javascript - 同时动画和添加类

node.js - 如何修复错误无法在 Angular 2 cli ckeditor 中找到名称 'object'

javascript - 带有 MarkerClusterer 的 Google map 商店定位器库