【问题标题】:Kik Bot keyboard items not changingKik Bot 键盘项目不变
【发布时间】:2016-11-14 04:13:15
【问题描述】:

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

【问题讨论】:

  • 您的消息是否真的被发送(= 您是否在 Kik 应用上收到消息?)。
  • @RémiVansteelandt 是的,除了建议的键盘没有改变之外,一切正常。
  • 为了测试目的,我会让消息具有不同的字符串。我听起来你只是总是发送第二条消息。如果您更改消息正文,您将能够看到

标签: javascript node.js kik


【解决方案1】:

Node Js 喜欢在函数开始运行时继续程序,以便它可以接受更多请求。函数userIsAdmin() 向firebase 发出网络请求,因此虽然下载数据只需要几分之一秒,但它的时间足以让它在完成之前返回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)
                   });

【讨论】:

  • 只是为了帮助你以后遇到类似的问题,这与执行时间无关。 Node.js 大部分时间都是异步工作的,为了不让主线程“挂起”。这样您就可以并行处理多个请求。我建议查看 Promise,这是一种比简单回调更简洁的处理此类事情的方式。
猜你喜欢
  • 2018-01-05
  • 1970-01-01
  • 2021-06-19
  • 2016-08-11
  • 2017-07-22
  • 2016-12-24
  • 1970-01-01
  • 2016-08-09
  • 2019-04-23
相关资源
最近更新 更多