0

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

我希望机器人只响应一次。

你有什么建议吗?

4

2 回答 2

0

您可以第二次检查响应是否是照片。您可以询问用户是否要使用最新照片或旧照片

于 2019-07-02T08:48:29.640 回答
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);
        }
 }

于 2019-07-02T08:53:28.470 回答