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

filter event by type and subtype #37

Closed
selfcontained opened this issue Aug 9, 2016 · 5 comments
Closed

filter event by type and subtype #37

selfcontained opened this issue Aug 9, 2016 · 5 comments
Labels
enhancement M-T: A feature request for new functionality

Comments

@selfcontained
Copy link
Contributor

I think it would be convenient to be able to filter an event by type and subtype. This can be accomplished currently w/ custom match() or catch-all event(type) handlers, but having a way to match via the event() fn would keep things more terse.

I can think of 2 ways to tackle this:

  1. Overload first param to allow a subtype identified by a . in the string - depends on Slack not adding any .'s to their event types/subtypes
slapp.event('message.channel_join', (msg) => {
  //...
})
  1. Add an optional 2nd param of subtype, could be string or regex just like first param
slapp.event('message', /channel_join/, (msg) => {
  //...
})
@selfcontained selfcontained added the enhancement M-T: A feature request for new functionality label Aug 9, 2016
@selfcontained
Copy link
Contributor Author

Could also use a separator that is less likely to exist in an type/subtype from slack, like :.

slapp.event('message:channel_join', (msg) => {
  //...
})

The more I think about it the more I like doing something like that over adding another optional param (since we also have the optional filter for direct_message etc.) If we go this route, we should update the default logging format to separate the type/subtype w/ a : also so it's consistent.

@aoberoi
Copy link
Contributor

aoberoi commented Dec 5, 2018

I like the idea of a convenience form of the API to filter by subtype. However, most commonly I'd like to filter for events of type message which have no subtype. This API falls short of solving that use case, and I think we might be able to do better.

@aoberoi
Copy link
Contributor

aoberoi commented Dec 5, 2018

At this point in time, subtypes are a concept only used in events of type message.

Knowing this, here's another possible solution. A new method that specifically handles events of type message, which takes an optional subtype argument. When that argument is explicitly set to undefined, it would trigger the listenerFn for messages with no subtype.

slapp.onMessage(subtype: string|undefined, listenerFn);
slapp.onMessage(listenerFn);

What I like about this is that it still shortens the API call for existing usages quite a bit. It also seems fully backwards-compatible.

// existing
slapp.event('message', listenerFn);
// proposed
slapp.onMessage(listenerFn);

Very much open to other ideas.

@shaydewael
Copy link
Contributor

We added support for this in Bolt v1 by

  1. adding a built-in middleware function that you can attach to your listener called subtype()
  2. allowing listeners to call multiple middleware functions.

As an example, to listen to a message with a channel_join subtype you could use:

app.event('message', subtype('channel_join'), ({ message }) => {
     console.log(message);
     // Your logic here
}

@rareseu
Copy link

rareseu commented Apr 11, 2022

Why this was added only for the message event, subtype? It would be great to sort all of the events through all of the params available.
For example, I have user_change event:

app.event('user_change', async ({ event, logger, client}) => {
  if(event.user.profile.status_emoji || event.user.profile.status_text) {
    try {
      CountPlusPlus('user_change');
      await client.chat.postMessage({
        channel: a
        text: b,
        thread_ts: c
      });
    } catch (error) {
      logger.error(error);
    }
  }
});

Is the approach correct? I'm interested only in status_text or status_emoji changes, but as I can guess, the Event API counts even if the status_text or status_emoji are undefined/not specified, so if I don't need it for extra params, wouldn't be great to sort just like subtype event for any other params?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement M-T: A feature request for new functionality
Projects
None yet
Development

No branches or pull requests

4 participants