Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchers are called for every message when using @app.message #561

Closed
caddac opened this issue Jan 4, 2022 · 1 comment · Fixed by #577
Closed

matchers are called for every message when using @app.message #561

caddac opened this issue Jan 4, 2022 · 1 comment · Fixed by #577
Assignees
Labels
Milestone

Comments

@caddac
Copy link

caddac commented Jan 4, 2022

When adding a matcher to @app.message handlers, matchers are called before middlewares, including the middleware that checks for the keyword in @app.message. This means the matcher is called for every message in the channel. In my case, my matcher is going to do several network calls, and I don't want to do that for messages that don't contain the keyword.

I do think I can work around this by using a middleware rather than a matcher, but a matcher seems to perfectly fit my use case.

Reproducible in:

1.11.1 of bolt (and probably prior versions)

Steps to reproduce:

Using the most basic app configuration, setup a matcher and message event handler as below:

def not_brennan(message, logger, client):
    """
    For now, a matcher to filter out brennan by ID
    soon, this will get the users email, and check if a user is in the gcp group
    """
    user = message.get('user')
    logger.info(f'user: {user}')

    email = get_email(client=client, user_id=user)
    logger.info(f'email: {email}')
    # TODO: see if this email address is in a certain GCP group

    if user != "U02RK1ZMZSM":
        return True
    return False


@app.message(":wave:", matchers=[not_brennan])
def wave(say, message, logger):
    user = message['user']
    logger.info(f"message object: {message}")
    say(f"Hi there, <@{user}>!")

Expected result:

The matcher function should only be called for messages that contain :wave:

(Tell what you expected to happen)

Actual result:

The matcher function is called for every single message typed in the channel. This is because the keyword for the message function is added as the first middleware, but matchers are called before middlewares. Here:

middleware.insert(0, MessageListenerMatches(keyword))

(Tell what actually happened with logs, screenshots)
You can see I messaged random, then :wave: and the bot responded (slack and logs). I would not expect the 2nd log message checking not_brennan for the message "random"
image
image

@misscoded misscoded added question Further information is requested enhancement New feature or request and removed question Further information is requested labels Jan 4, 2022
@misscoded
Copy link
Contributor

Hi @caddac! Thanks for surfacing this and for providing details around your use case. We agree that it's entirely reasonable to expect the filtering of the keyword condition to occur before the matcher.

We'll add this issue to the list of enhancements to be introduced in a coming release.

@misscoded misscoded added this to the 1.12.0 milestone Jan 5, 2022
@seratch seratch self-assigned this Jan 15, 2022
seratch added a commit to seratch/bolt-python that referenced this issue Jan 15, 2022
seratch added a commit that referenced this issue Jan 17, 2022
…t match (#577)

* Fix #561 matchers can be called even when app.message keyword does not match

* Fix a bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants