javascript - Slack/Botkit : How to make bot in slack listen better?

标签 javascript node.js bots slack botkit

我在创建一种让机器人在 Slack channel 中正确监听而不回复 channel 中所说的所有内容的方法时遇到问题。

所以,这是我目前处理此问题的方法:

controller.hears(['You\'re awesome','Help',"What would Jeremy say?", "Hello","Top 5", "Hi", "I love you", /^.{0,}jirabot.{0,}$/], ['direct_message','direct_mention','mention','ambient'],function(bot,message) {

   console.log(message);

 // This will show the last 5 created tickets in Zendesk
   if(message.text === "Top 5"){
     try{
         Do this thing
     });
   }catch(err){
     bot.reply(message, 'I\'m sorry I did not get that. Please try again.');
   }

 }

   else if (message.text === "You're Awesome"){
       bot.reply(message, 'Nah, You\'re Awesome <@'+message.user+'>');
     }
   else {
        bot.reply(message, 'I\'m sorry I don\'t understand');
        }

这种方式可行,但如果有人说别的东西,它会一直说“对不起,我不明白”。如何让机器人每次都这样说?

我也尝试过这种方式,但是机器人在完成其中一个操作后终止并且不会执行任何其他操作:

  var hi = 'HI';
    var love = 'I LOVE YOU';

controller.hears([hi, /^.{0,}jirabot.{0,}$/],['direct_message','direct_mention','mention','ambient'],function(bot,message) {

  // start a conversation to handle this response.
  bot.startConversation(message,function(err,convo) {

      if (message.text.toLowerCase() === hi.toLowerCase()){
        bot.reply(message, 'What can I do for you? <@'+message.user+'>');
      convo.next();
    }else{
      bot.reply(message, 'Sorry, I don\'t understand');
      convo.next();
    }

  });
});
controller.hears([love, /^.{0,}jirabot.{0,}$/],['direct_message','direct_mention','mention','ambient'],function(bot,message) {

  // start a conversation to handle this response.
  bot.startConversation(message,function(err,convo) {

      if (message.text.toLowerCase() === love.toLowerCase()){
        bot.reply(message, 'No, I love you more <@'+message.user+'>');
      convo.next();
    }else{
      bot.reply(message, 'Sorry, I don\'t understand');
      convo.next();
    }

  });
});

任何想法或反馈都很棒!

最佳答案

当前,您正在匹配用户输入中任何位置出现的这些字符串。因此,您将触发此用户输入:hi,还会触发此输入:hildjf我喜欢香菇!

我按原样运行了您的代码,它不会在所有用户输入到 channel 时触发,只有在hears中列出的字符串才会触发。

使用一些正则表达式来锁定匹配,使用 ^ 表示字符串的开头,使用 $ 表示结尾。因此,要匹配仅说出字符串 hi 的用户,请使用模式 '^hi$'。您将不再匹配部分字符串,例如 shiitake

关于javascript - Slack/Botkit : How to make bot in slack listen better?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574517/

相关文章:

javascript - socket io js延迟断开连接发射

security - 当机器人攻击!

bots - Facebook 信使平台 : generic template with quick replies

javascript - 将字符串传递给函数不会设置变量名称

javascript - 无法在 IE8 中将 0.px 设置为 div 的最高值

javascript - 在 React 中显示/隐藏组件

python - 解析时发生意外的EOF错误?

Javascript:当我调整窗口大小时,如何知道 DOM 上正在运行什么函数?

node.js - MongoDb 按日期分组并分页

node.js - 如何使用 node.js 获取 nightwatch.js 中元素的位置