javascriptnode.jskik

Kik Bot keyboard items not changing


So I have a Kik bot that I am currently working on that uses the keyboards to suggest things that the user might want to say to the bot as most Kik bots do. For different users I want to have different options pop up. I created a function to check if the current user is once of those special users and if so to display another option for them. I have conferment from many tests that the function is returning true, how ever the keyboard options refuse to change from what a normal user would see. Here is my code

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;
                  }

Solution

  • Node Js likes to continue programs when functions start running so that it can take more requests. The function userIsAdmin() makes a web request to firebase so while taking only a fraction of a second to download the data, its long enough for it to return false before it was done. What I had to do was edit the function userIsAdmin() so that it took a callback as a parameter and then call it. Here is my new code:

    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)
                   }
                   });
    

    And here is my adminCheck function:

    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)
                       });