0

所以我有一个我目前正在研究的 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;
                  }
4

1 回答 1

1

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)
                   });
于 2016-07-13T16:05:20.427 回答