0

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 Answers2

0

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

Rahul Rana
  • 455
  • 2
  • 7
  • Thank you but actually, users can send more than one photo. the real problem is that if the user sends, for example, two photos, my bot responses twice. And I don't know how to check if the photo is the first or the second. – Annalisa Bovone Jul 02 '19 at 08:55
0

// 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);
        }
 }
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30