Reputation:
Mostly trying to check if what I am trying to do is possible as I have struggled to find any similar examples online.
I'm trying to create a series of menu's using hubot's framework so that individual commands and values do not have to be memorised. Instead you simply enter one command at the beginning, give relevant information once where these values will be stored for use multiple times later down the menus.
Something like this:
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"
I have been using listener middleware to make sure that you cannot access the menu's out of order:
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)
Everything seems to work as expected the first time I go through the menu's however the problems start if I try and launch from the start for a second time. Values seem to get remembered even if I set them to null at the start or end of my methods. And when I get closer to the end it will start printing out things twice.
I assume this is because values are getting cached/stored elsewhere and I need to reset this. I also assume that the reason it's printing out twice is because hubot remembers that I have already been through the menu's once and there's two instances running at once (it will start printing out three times if I go through a third time). However it only seems to do this towards the end and will print out as expected for the first few methods.
Simply, is there a way to make hubot forget everything perhaps in the 'end' method so it runs like I'm running it for the first time every time? I've looked into this but things like robot.shutdown don't seem to work.
If the above isn't possible, is there are workaround?
Edit: If it at all helps, I'm trying to make something similar to botkit's conversation feature: https://github.com/howdyai/botkit#multi-message-replies-to-incoming-messages
Upvotes: 1
Views: 475
Reputation:
I got linked to this on my thread on github asking the same question:
https://github.com/lmarkus/hubot-conversation
Currently trying it out to see if it solves the issue I'm having, if not hopefully it will help out other people having a similar problem to me.
Upvotes: 1