coffeescripthubot

Hubot Hear Variable


I was hoping to find away to get hubot to here a variable. Ex

name = "Peter"
module.exports = (robot) ->
   robot.hear /hello name/i, (msg) ->
       msg.send "Peter?! No I am Hubot."

I have already tried "#{}" syntax like below, but got nowhere with it.

name = "Peter"
module.exports = (robot) ->
   robot.hear /hello #{name}/i, (msg) ->
       msg.send "Peter?! No I am Hubot."

Any help would be greatly appreciated.

Regards,

Austin


Solution

  • Since your regex is not a constant, you should use new Regex():

    Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

    Code

    name = "Peter"
    regx = new Regex("hello #{ name }", 'i')
    module.exports = (robot) ->
      robot.hear regx, (msg) ->
          msg.send "Peter?! No I am Hubot."
    

    Edit With name as a parameter

    module.exports = (robot, name) ->
      regx = new Regex("hello #{ name }", 'i')
      robot.hear regx, (msg) ->
          msg.send "#{ name }?! No I am Hubot."