【发布时间】:2016-11-28 10:51:16
【问题描述】:
主要是尝试检查我正在尝试做的事情是否可行,因为我一直在努力在网上找到任何类似的例子。
我正在尝试使用 hubot 的框架创建一系列菜单,这样就不必记住单个命令和值。相反,您只需在开始时输入一个命令,提供相关信息一次,这些值将存储在菜单中以供以后多次使用。
类似这样的:
robot.hear /blah/i, id:'start', (msg) ->
currentPosition = 'start'
msg.send "Please select an Option"
selectOption msg
selectOption = (msg) ->
currentPosition = 'selectOption'
msg.send "Option 1"
msg.send "Option 2"
robot.hear /(.*)/i, id:'selectOption', (msg) ->
displayOption1 msg if msg.match is '1'
displayOption2 msg if msg.match is '2'
displayOption1 = (msg) ->
currentPosition = 'displayOption1'
msg.send "Please enter some information"
robot.hear /(.*)/i, id: 'displayOption1', (msg) ->
# if information is entered do something with information
# pass the information onto another method etc...
# ....
# methods to do something with information and feedback results
# ....
# ....
# methods corresponding to option 2
# ....
# ....
# methods to do something with information and feedback results
# ....
end = (msg) ->
currentPosition = 'none'
msg.send "Thank you for using our service"
我一直在使用侦听器中间件来确保您无法无序访问菜单:
robot.listenerMiddleware (context, next, done) ->
listenerID = context.listener.options?.id
return unless listenerID?
try
if listenerID is 'start'
if currentPosition is 'none'
next()
else
done()
if listenerID is 'selectOption'
if currentPosition is 'selectOption'
next()
# etc...
# Other similar examples to above for every "menu" ...
catch err
robot.emit ('error', err, context.response)
当我第一次浏览菜单时,一切似乎都按预期工作,但是如果我第二次尝试从头启动,问题就会出现。即使我在方法的开始或结束时将它们设置为 null,它们似乎也会被记住。当我接近尾声时,它会开始打印两次。
我认为这是因为值被缓存/存储在其他地方,我需要重置它。我还假设它打印两次的原因是因为 hubot 记得我已经通过菜单一次并且有两个实例同时运行(如果我通过第三次,它将开始打印三次)。但是,它似乎只在最后执行此操作,并且会按前几个方法的预期打印出来。
简单地说,有没有办法让hubot忘记所有可能在'end'方法中的东西,所以它运行起来就像我每次第一次运行它一样?我已经对此进行了调查,但是 robots.shutdown 之类的东西似乎不起作用。
如果上述方法不可行,是否有解决方法?
编辑:如果有帮助,我正在尝试制作类似于 botkit 的对话功能的东西:https://github.com/howdyai/botkit#multi-message-replies-to-incoming-messages
【问题讨论】:
标签: coffeescript hubot