I have the following script:
module.exports = (robot) ->
robot.respond /petsit/i, (res) ->
user_name = res.envelope.user.name
res.reply "Starting new petsitting under: @" + user_name + "\n"
res.send "What is the dog's name? Reply with 'dog `INSERT_DOG_NAME`'"
robot.respond /dog (.*)/i, (msg) ->
dog_name = msg.match[1]
user_dog_name = "" + user_name + dog_name
if robot.brain.get(user_dog_name)
msg.send "Petsitting is already in progress for @" + user_name + " Dog: " + dog_name
else
robot.brain.set(user_app_name, true)
msg.send "#{dog_name} " + robot.brain.get(user_app_name)
I'm confused why I'm getting multiple messages when I call petsit multiple times?
For example:
@hubot petsit
@hubot dog lucky
@hubot petsit
@hubot dog kenny ---> This will give me multiple messages.
My thought is that this is async and the 2nd message is being run by the 1st dialouges/conversation? How do i fix this so that the 1st conversation does not interfere with following conversations?
You are getting multiple messages because the robot.respond /dog (.*)/i
listener is attached every time the robot.respond /petsit/i
is called. That is the first time you send petsit the /dog (.*)/i
listener is attached for the first time. When you send dog ... only one response will be triggered. Every subsequent time you send petsit the second listener will bind itself on the same robot instance, thus sending you multiple responses.
By default there is no native support from Hubot for managing conversations. Instead you should have a look at a 3rd party module like hubot-conversation that adds this type of functionality or implement your own logic.