slack.events - Events

class slack.events.Event(raw_event: collections.abc.MutableMapping, metadata: Optional[collections.abc.MutableMapping] = None)[source]

MutableMapping representing a slack event coming from the RTM API or the Event API.

metadata

Metadata dispatched with the event when using the Event API (see slack event API documentation)

clone() → slack.events.Event[source]

Clone the event

Returns:slack.events.Event
classmethod from_http(raw_body: collections.abc.MutableMapping, verification_token: Optional[str] = None, team_id: Optional[str] = None) → slack.events.Event[source]

Create an event with data coming from the HTTP Event API.

If the event type is a message a slack.events.Message is returned.

Parameters:
  • raw_body – Decoded body of the Event API request
  • verification_token – Slack verification token used to verify the request came from slack
  • team_id – Verify the event is for the correct team
Returns:

slack.events.Event or slack.events.Message

Raises:

slack.exceptions.FailedVerification – when verification_token or team_id does not match the incoming event’s.

classmethod from_rtm(raw_event: collections.abc.MutableMapping) → slack.events.Event[source]

Create an event with data coming from the RTM API.

If the event type is a message a slack.events.Message is returned.

Parameters:raw_event – JSON decoded data from the RTM API
Returns:slack.events.Event or slack.events.Message
class slack.events.EventRouter[source]

When receiving an event from the RTM API or the slack API it is useful to have a routing mechanisms for dispatching event to individual function/coroutine. This class provide such mechanisms for any slack.events.Event.

dispatch(event: slack.events.Event) → Iterator[Any][source]

Yields handlers matching the routing of the incoming slack.events.Event.

Parameters:eventslack.events.Event
Yields:handler
register(event_type: str, handler: Any, **detail) → None[source]

Register a new handler for a specific slack.events.Event type (See slack event types documentation for a list of event types).

The arbitrary keyword argument is used as a key/value pair to compare against what is in the incoming slack.events.Event

Parameters:
  • event_type – Event type the handler is interested in
  • handler – Callback
  • **detail – Additional key for routing
class slack.events.Message(msg: Optional[collections.abc.MutableMapping] = None, metadata: Optional[collections.abc.MutableMapping] = None)[source]

Type of slack.events.Event corresponding to a message event type

response(in_thread: Optional[bool] = None) → slack.events.Message[source]

Create a response message.

Depending on the incoming message the response can be in a thread. By default the response follow where the incoming message was posted.

Parameters:in_thread (boolean) – Overwrite the threading behaviour
Returns:a new slack.event.Message
serialize() → dict[source]

Serialize the message for sending to slack API

Returns:serialized message
class slack.events.MessageRouter[source]

When receiving an event of type message from the RTM API or the slack API it is useful to have a routing mechanisms for dispatching the message to individual function/coroutine. This class provide such mechanisms for any slack.events.Message.

The routing is based on regex pattern matching of the message text and the receiving channel.

dispatch(message: slack.events.Message) → Iterator[Any][source]

Yields handlers matching the routing of the incoming slack.events.Message

Parameters:messageslack.events.Message
Yields:handler
register(pattern: str, handler: Any, flags: int = 0, channel: str = '*', subtype: Optional[str] = None) → None[source]

Register a new handler for a specific slack.events.Message.

The routing is based on regex pattern matching the message text and the incoming slack channel.

Parameters:
  • pattern – Regex pattern matching the message text.
  • handler – Callback
  • flags – Regex flags.
  • channel – Slack channel ID. Use * for any.
  • subtype – Message subtype