Friday, 21 April 2017

Slackbot / Botkit: How to make the bot listen correctly?

I'm having issues with creating a way where the bot correctly listens within a slack channel without replying to everything being said in the channel.

So, here is the way I currently approached this:

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

This method works cause the bot is able to hear things like you're awesome and top 5. It does the action it is told to do so and then it waits to listen for another key word. Now, if a user says something within the slack channel that doesn't match any of the keywords like 'Where's Jim?' The bot will always reply I'm sorry I don't get that. I understand why it's doing that, but I'm having issues in finding a way around it.

I also tried to do it this way:

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

  });
});

The problem with this method is that the bot does the one action and doesn't do anything. So, if it hears Hi it will do the action. If it hears love it won't do anything. I put convo.next in the controller for each specific action, However, it doesn't work.

For some reason this way seems to work:

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) {
      convo.ask('did you mean to say you love me?'function(response,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();
    }
     })

  });
});

But, it makes no sense to have the bot ask the user for some input in order for it to keep the conversation going (meaning- be ready to hear the next keyword to be said).

Here is the link to the botkit documentation: Howdy BotKit for Slack

I read through it a couple times and I don't what to do.

Any feedback would be appreciated it, thanks!



via traveler316

No comments:

Post a Comment