skip to Main Content

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?

2

Answers


  1. You can check if the response is photo second time.
    you can ask user if wants to use latest photo or the old one

    Login or Signup to reply.
  2. // 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);
            }
     }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search