scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 394 forks source link

respond_to messages with colon #154

Open DanielPaque opened 7 years ago

DanielPaque commented 7 years ago

Hi,

the decorator @respond_to fails at messages containing a colon after the first word, e.g. "18:45"

Example: The following method will not react at all to the message "18:45"

@respond_to('18')
def foo(message):
    # do something

The following method should respond to any text while receiving the full message. However will receive only "45" as text, although the user sent "18:45"

@respond_to('(.*$)')
def process_input(message, text)
    # do something

So it seems that characters in front of the first colon are not transferred to the respont_to part. (An exception to this behavior is any string containing a whitespace before the colon)

I could pinpoint the problem to following line of code in slackbot/dispatcher.py: self.AT_MESSAGE_MATCHER = re.compile(r'^(?:\<@(?P<atuser>\w+)\>:?||(?P<username>\w+):{})...) See here for full code.

This regex is used for two purposes:

  1. filter out the @username when talking to another person
  2. filter out any alphanumeric word at the beginning of the message which is followed by a colon

The first purpose seems fine for me, the second one is causing the trouble.

I fixed the problem for me by deleting the username part in the regex. At first sight, this seems to work quite fine.

So maybe you should consider how to handle messages like "18:45".

Regards, Daniel

jtatum commented 7 years ago

This is a bit tricky. Depending on your slack team settings, typing botname and pressing tab in a channel or group will either put @botname or botname:. in the latter case, we want to filter the bot's name out before matching handlers.

That said.. maybe we should actually compile the username into the regular expression, rather than reading it at runtime? Or, perhaps we could append the captured username value to the string we match on, if the username isn't the bot's name. Or, maybe we can read the team settings and figure out whether to expect @botname or botname:.

zachsirotto commented 7 years ago

I think it would make sense to append the username to the string if it doesn't match the bot's name. This is definitely an issue I would like to see resolved asap.

hopkinsnji commented 4 years ago

Any updates in this? It would be nice to at least exclude https: so that we can respond when someone posts a url.