scrapinghub / slackbot

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

Use ArgParse instead of having to regex the text. #204

Open Ryanb58 opened 4 years ago

Ryanb58 commented 4 years ago

In a local project I am using this library, but then utilizing ArgParse to get information from the command sent to the bot. This works well when your bot is more of a command based tool.

For example:

@listen_to("^!client payments (.+)")
@respond_to("client payments (.+)")
def client_payments(message, text):
    # Parse the arguments
    parser = ArgumentParser()
    parser.add_argument('--client-id', type=str, dest='client_id')
    parser.add_argument('--email', type=str, dest='email')
    parser.add_argument('--trans-id', type=str, dest='trans_id')
    parsed_args = parser.custom_parse(message, text)

This allows us to onboard new devs quicker and prevents Havok when someone commits the wrong regex that accepts one too many weird things.

Was wondering how anyone else felt about this?

One thing that would be even cooler is if this could be a decorator...

like...

@listen_to("^!client payments (.+)")
@respond_to("client payments (.+)")
@arg('--client-id', type=str, dest='client_id')
@arg('--email', type=str, dest='email'')
@arg('--trans-id', type=str, dest='trans_id')
def client_payments(message, client_id, email, trans_id):
    pass

Anyone have any thoughts or opinions?