6

Is there any way to make Hubot understand the context of conversation between messages? Such that he could ask me clarifying questions?

For example:

me: hey, create a branch plz
Hubot: How should I name it?
me: super-duper
Hubot: Branch 'super-duper' created

Should I use some kind of state machine? Any advices on that?

3 Answers 3

10

You can use robot's brain to persist state.

robot.respond /hey, create a branch plz/i, (res) ->
     res.reply "Ok, lets start"
     user = {stage: 1}
     name = res.message.user.name.toLowerCase()
     robot.brain.set name, user

robot.hear /(\w+)\s(\w+)/i, (msg) ->
     name = msg.message.user.name.toLowerCase()
     user = robot.brain.get(name) or null
     if user != null
      answer = msg.match[2]
      switch user.stage
        when 1 
          msg.reply "How should I name it?"
        when 2 
          user.name = answer
          msg.reply "Are you sure (y/n) ?"
        when 3
          user.confimation=answer

      user.stage += 1
      robot.brain.set name, user

      if user.stage > 3 #End of process
        if /y/i.test(user.confimation) 
           msg.reply "Branch #{user.name} created." 
        else
           msg.reply "Branch creation aborted"

        robot.brain.remove name
1
  • Not the most beautiful solution ever, but I like it. Thank you! Oct 13, 2015 at 15:52
0

You could assign something like a session to it.

We are doing this for logins. When I tell him login it is bound to the calling user. Advantage is you can store this in the brain. Disadvantage is that one user can only have one session. (you could overcome this through letting them specifying a id.)

0

This can also be done with the Hubot Conversation plugin. This adds a dialog object which you can interact with. The dialog is scripted, not "natural", but it can be used to create chatbot pathways to execute simple workflows.

Your example might work as follows:

var Conversation = require("hubot-conversation");
module.exports = function(robot) {
    var switchBoard = new Conversation(robot);

    robot.respond(/create a branch/, function(msg) {
        var dialog = switchBoard.startDialog(msg);

        msg.reply("How should I name it");
        dialog.addChoice(/[a-z]+/i, function(msg2) {
            msg2.reply("Branch #{msg2.match[1]} created");
        });
        dialog.addChoice(/bathroom/i, function(msg2) {
            msg.reply("Do I really have to?");
            dialog.addChoice(/yes/, function(msg3) {
                msg3.reply("Fine, Mom!");
            });
        });
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.