我正在用nodejs开发一个Telegram机器人。我创建了node-telegram- bot -api的一个实例,并使用(‘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 我希望机器人只响应一次。
你有什么意见建议?
发布于 2019-07-02 16:53:28
// 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-02 16:48:30
您可以查看第二次回复是否为照片。您可以询问用户是否要使用最新的照片或旧的照片
https://stackoverflow.com/questions/56848591
复制相似问题