因此,我目前正在研究一个Kik机器人,该机器人使用键盘来建议用户可能要对机器人说的话,就像大多数Kik机器人一样。对于不同的用户,我希望弹出不同的选项。我创建了一个函数来检查当前用户是否曾经是那些特殊用户,如果是,则为他们显示另一个选项。我从许多测试中获悉,该函数返回的是true,无论键盘选项如何拒绝改变普通用户的外观。这是我的代码

message.stopTyping();
                  if (userIsAdmin(message.from)) //This function returns the boolean true
                  {
                  message.reply(Bot.Message.text("I don't understand what you are trying to ask me. Please reply with something I can work with.").addResponseKeyboard(["Homework", "Admin Options"]))
                  }
                  else
                  {
                  message.reply(Bot.Message.text("I don't understand what you are trying to ask me. Please reply with something I can work with.").addResponseKeyboard(["Homework"])) //The bot always displays this as the keyboard, no matter if the user is an admin or not
                  }
                  break;
                  }

最佳答案

当函数开始运行时,Node Js喜欢继续执行程序,以便可以接收更多请求。函数userIsAdmin()向Firebase发出Web请求,因此虽然只花了不到一秒钟的时间来下载数据,但它的时间足以使其在完成之前返回false。我要做的是编辑函数userIsAdmin(),以便它将回调作为参数,然后调用它。这是我的新代码:

let sendingMessage = Bot.Message.text("I don't understand what you are trying to ask me. Please reply with something I can work with.")

    adminCheck(user, function(isAdmin)
               {
               if (isAdmin)
               {
               bot.send(sendingMessage.addResponseKeyboard(adminSuggestedResponces), user)
               }
               else
               {
               bot.send(sendingMessage.addResponseKeyboard(userSuggestedResponces), user)
               }
               });


这是我的adminCheck函数:

var isAdmin = false
    adminsRef.on("child_added", function(snapshot)
                 {
                 if(user == snapshot.key && snapshot.val() == true)
                 {
                 isAdmin = true
                 }
                 });

    adminsRef.once("value", function(snapshot)
                   {
                   callback(isAdmin)
                   });

10-07 12:16