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?

有帮助吗?

解决方案

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

其他提示

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.)

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!");
            });
        });
    });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top