# Slack ## Events API > [!ai]+ Claude > > - **Purpose**: The Events API allows apps to receive notifications about activities in Slack. > - **Mechanism**: Slack sends HTTP POST requests to a designated public HTTP endpoint or via a WebSocket in Socket Mode. > - **Use Cases**: Responding to messages, tracking user activities, managing app installations, etc. > > ### Key Features > > - **Event Subscriptions**: Subscribe to specific event types relevant to your app. > - **Security**: Events are tied to OAuth permission scopes. > - **Retry Mechanism**: Slack retries event delivery if the initial attempt fails. > - **Socket Mode**: Receive events via WebSocket for enhanced security behind firewalls. > > ## Setting Up the Events API > > 1. **Create a Slack App**: If you haven't already, create a Slack app via the Slack API dashboard. > 2. **Enable Event Subscriptions**: In your app settings, navigate to "Event Subscriptions" and toggle it on. > 3. **Set Request URL**: Provide a public HTTP endpoint where Slack will send event notifications. > > ### URL Verification > > - **Challenge Request**: Slack sends a `url_verification` event to your Request URL to verify ownership. > - **Response**: Respond with the `challenge` value provided in the request. > > ```json > { > "token": "Jhj5dZrVaK7ZwHHjRyZWjbDl", > "challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P", > "type": "url_verification" > } > ``` > > ### Example Response > > ```http > HTTP 200 OK > Content-type: application/json > { > "challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P" > } > ``` > > ## Subscribing to Events > > ### Event Types > > - **Team Events**: Require corresponding OAuth scopes and are related to workspace activities. > - **Bot Events**: Subscribed on behalf of the bot user, covering events the bot is involved in. > > ### Configuring Subscriptions > > 1. **Select Event Types**: Choose the events your app needs to subscribe to. > 2. **OAuth Scopes**: Ensure your app requests the necessary OAuth scopes for the events. > > ## Handling Events > > ### Receiving Events > > - **Event Payload**: Slack sends events as JSON payloads to your Request URL. > - **Event Structure**: Each event contains metadata and the specific event data. > > ```json > { > "type": "event_callback", > "token": "XXYYZZ", > "team_id": "T123ABC456", > "api_app_id": "A123ABC456", > "event": { > "type": "name_of_event", > "event_ts": "1234567890.123456", > "user": "U123ABC456", > ... > }, > "event_context": "EC123ABC456", > "event_id": "Ev123ABC456", > "event_time": 1234567890, > "authorizations": [ > { > "enterprise_id": "E123ABC456", > "team_id": "T123ABC456", > "user_id": "U123ABC456", > "is_bot": false, > "is_enterprise_install": false, > } > ], > "is_ext_shared_channel": false, > "context_team_id": "T123ABC456", > "context_enterprise_id": null > } > ``` > > ### Responding to Events > > - **HTTP 200 OK**: Acknowledge receipt of the event with an HTTP 200 OK response. > - **Processing Events**: Implement business logic to handle the event data. > > ### Example Event Handling > > ```python > from flask import Flask, request, jsonify > > app = Flask(__name__) > > @app.route('/slack/events', methods=['POST']) > def slack_events(): > data = request.json > if data['type'] == 'url_verification': > return jsonify({'challenge': data['challenge']}) > elif data['type'] == 'event_callback': > event = data['event'] > # Process the event here > return '', 200 > > if __name__ == '__main__': > app.run(port=3000) > ``` > > ## Best Practices > > - **Security**: Verify the authenticity of events using the signing secret. > - **Scalability**: Decouple event ingestion from processing to handle high volumes. > - **Error Handling**: Implement robust error handling and retry mechanisms. > > ## Rate Limiting > > - **Limits**: Slack imposes a limit of 30,000 events per workspace per app per 60 minutes. > - **Rate Limited Events**: Handle `app_rate_limited` events to manage rate limiting. > > ```json > { > "token": "Jhj5dZrVaK7ZwHHjRyZWjbDl", > "type": "app_rate_limited", > "team_id": "T123ABC456", > "minute_rate_limited": 1518467820, > "api_app_id": "A123ABC456" > } > ``` > > ## Conclusion > > The Slack Events API is a versatile tool for building interactive and responsive Slack apps. By following best practices and understanding the API's capabilities, you can create robust integrations that enhance the Slack experience for users. > > Citations: > [1] https://api.slack.com/events-api > [2] https://api.slack.com/tutorials/tags/events-api > [3] https://medium.com/analytics-vidhya/consuming-slack-events-with-event-subscription-api-and-slack-app-in-python-681cab354571 > [4] https://api.slack.com/events > [5] https://www.ory.sh/building-slack-bot-using-slack-events-api/ > [6] https://api.slack.com/tutorials > [7] https://slack.dev/node-slack-sdk/events-api > [8] https://stackoverflow.com/questions/68432241/slack-events-api-only-able-to-subscribe-to-messages-posted-in-public-channels ## Integrations - Incoming webhook - Outgoing webhook - Slash command🛠 - ... ## Block Kit