I'm trying to write a hubot script that answers to two different kinds of input. A user can either input the name of a stop for the local public transport or optionally postfix this with a delay.
The input can therefore be dvb zellescher weg
or dvb albertplatz
for the first option or dvb zellescher weg in 5
or dvb albertplatz in 10
for the second.
("dvb" here being the keyword for my script and "zellescher weg" and "albertplatz" being two examples for stop names)
On trying to match these with regex I'm running into an issue where a regex I've gotten to work on different testing sites (like regex101 which seems to be recommended here and does JS) won't work in my code. The regex for matching input without a number is /^dvb (\D*)$/
and I'm using /dvb\s+(.*)in (\d*)/
to match the cases where the user has entered a delay.
A minimal code example for my hubot that isn't matching for reasons unbeknownst to me looks like this:
robot.respond /^dvb (\D*)$/, (res) ->
hst = res.match[1]
res.send hst
Thanks for any help on this.
According to the source respond
code comments:
# Public: Adds a Listener that attempts to match incoming messages directed
# at the robot based on a Regex. All regexes treat patterns like they begin
# with a'^'
The regex from respond
goes to respondPattern
that escapes ^
and warns against using anchors:
if re[0] and re[0][0] is '^'
@logger.warning \
"Anchors don't work well with respond, perhaps you want to use 'hear'"
So, you need to either remove the ^
, or use a .hear
method that does not use any "smart" regex pre-processing:
hear: (regex, options, callback) ->
@listeners.push new TextListener(@, regex, options, callback)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With