有什么方法可以让 Hubot 理解消息之间的对话上下文?这样他就可以问我澄清问题?
例如:
me: hey, create a branch plz
Hubot: How should I name it?
me: super-duper
Hubot: Branch 'super-duper' created
我应该使用某种状态机吗?对此有何建议?
有什么方法可以让 Hubot 理解消息之间的对话上下文?这样他就可以问我澄清问题?
例如:
me: hey, create a branch plz
Hubot: How should I name it?
me: super-duper
Hubot: Branch 'super-duper' created
我应该使用某种状态机吗?对此有何建议?
您可以使用机器人的大脑来保持状态。
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
您可以为其分配类似会话的内容。
我们这样做是为了登录。当我告诉他登录时,它绑定到调用用户。优点是您可以将其存储在大脑中。缺点是一个用户只能有一个会话。(你可以通过让他们指定一个 id 来克服这个问题。)
这也可以通过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!");
});
});
});