scrapinghub / slackbot

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

Handle multiline messages? #164

Open Resisty opened 6 years ago

Resisty commented 6 years ago

It looks like this should work out of the box (judging by https://github.com/Rycieos/slack-compilebot) but I can't get my bot to respond to multiline messages, like:

@slackbot multiline
message
test

I've tried creating handlers like:

@slackbot.bot.respond_to(re.compile(r'(.*)', re.I))
def log_all(message, *groups):
    print(message.body)

and various other patterns like r'([\s\S\n]*)', r'(.*)', re.I|re.S but I can't figure out how get the bot to respond to multiline messages. Any advice or tips would be much appreciated!

wanshot commented 6 years ago

There are the following ways to receive multiline messages.

@listen_to('XXXX')
def hoge(message):
    text = message.body['text']
    # text is multiline messages
nafets33 commented 6 years ago

I have the bot up and running ... so 1st off thank you

but I am not able to get the bot to listen and respond with multiline messages...I presume I'm missing a simple detail.

For Ex. anytime the bot name is mentioned...I want to the bot to look at the message and if any of my keywords are within the message then proceed to do something

@listen_to("my bot name") ---> here is where I presume I'm going wrong. I need the bot to look at the message and then see if any keywords ??? def listening(message): msg = message.body['text'] if key_word list in msg: do stuff

Appreciate the help

tily commented 6 years ago

This worked for me.

def main():
    bot = Bot()
    # make @respond_to receive multi-line messages
    bot._dispatcher.AT_MESSAGE_MATCHER = re.compile(
        bot._dispatcher.AT_MESSAGE_MATCHER.pattern, re.S | re.M
    )
    bot.run()