Search code examples
javascriptnode.jsnpmtelegram-botnode-telegram-bot-api

How to make a bot responding once if an user sends more than one photo at the same time?


I'm developing a Telegram bot in nodejs. I create an instance of node-telegram-bot-api and I'm using the method on('photo') to manage if a user sends a photo to my bot.

The problem is when a user sends more than one photo together by multi selecting photos from the gallery because my bot responses as many times as the photo sent. I think that this happens because the bot executes the on('photo') method as many times as the photo sent.

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 

I would like that the bot responses just one time.

Do you have any suggestions?


Solution

  • // 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);
            }
     }