Ant*_*hko 6 state-machine hubot
有没有办法让Hubot理解消息之间的对话背景?这样他可以问我澄清问题吗?
例如:
me: hey, create a branch plz
Hubot: How should I name it?
me: super-duper
Hubot: Branch 'super-duper' created
Run Code Online (Sandbox Code Playgroud)
我应该使用某种状态机吗?有什么建议吗?
ton*_*ral 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
Run Code Online (Sandbox Code Playgroud)