如何使Hubot理解聊天环境?

问题描述 投票:6回答:3

是否有任何方法可以使Hubot理解消息之间的对话上下文?这样他可以问我澄清的问题?

例如:

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

我应该使用某种状态机吗?有什么建议吗?

state-machine hubot
3个回答
10
投票

您可以使用机器人的大脑来保持状态。

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

0
投票

您可以为其分配类似会话的内容。

我们正在执行登录操作。当我告诉他登录名时,它将绑定到主叫用户。优点是您可以将其存储在大脑中。缺点是一个用户只能进行一个会话。 (您可以通过让他们指定ID来克服这一问题。)


0
投票

这也可以使用Hubot Conversation插件来完成。这将添加一个可以与之交互的对话框对象。该对话框是脚本化的,而不是“自然的”脚本,但可用于创建聊天机器人路径以执行简单的工作流程。

您的示例可能如下工作:

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!");
            });
        });
    });
© www.soinside.com 2019 - 2024. All rights reserved.