Slack Events API adapter for Python
The Slack Events Adapter is a Python-based solution to receive and parse events from Slack’s Events API. This library uses an event emitter framework to allow you to easily process Slack events by simply attaching functions to event listeners.
This adapter enhances and simplifies Slack's Events API by incorporating useful best practices, patterns, and opportunities to abstract out common tasks.
pip install slackeventsapi
🤖 App Setup
🤖 Development workflow:
- Create a Slack app on https://api.slack.com/apps/
- Add a bot user for your app
- Start the example app on your Request URL endpoint
- Start ngrok and copy the HTTPS URL
- Add your Request URL and subscribe your app to events
- Go to your ngrok URL (e.g. https://myapp12.ngrok.com/) and auth your app
⚠️Ngrok is a great tool for developing Slack apps, but we don't recommend using ngrok for production apps.
⚠️Keep your app's credentials safe!
- For development, keep them in virtualenv variables.
- For production, use a secure data store.
- Never post your app's credentials to github.
SLACK_SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"]
Create a Slack Event Adapter for receiving actions via the Events API
Using the built-in Flask server:
from slackeventsapi import SlackEventAdapter slack_events_adapter = SlackEventAdapter(SLACK_SIGNING_SECRET, endpoint="/slack/events") # Create an event listener for "reaction_added" events and print the emoji name @slack_events_adapter.on("reaction_added") def reaction_added(event): emoji = event.get("reaction") print(emoji) # Start the server on port 3000 slack_events_adapter.start(port=3000)
Using your existing Flask instance:
from flask import Flask from slackeventsapi import SlackEventAdapter # This `app` represents your existing Flask app app = Flask(__name__) # An example of one of your Flask app's routes @app.route("/") def hello(): return "Hello there!" # Bind the Events API route to your existing Flask app by passing the server # instance as the last param, or with `server=app`. slack_events_adapter = SlackEventAdapter(SLACK_SIGNING_SECRET, "/slack/events", app) # Create an event listener for "reaction_added" events and print the emoji name @slack_events_adapter.on("reaction_added") def reaction_added(event): emoji = event.get("reaction") print(emoji) # Start the server on port 3000 if __name__ == "__main__": app.run(port=3000)
For a comprehensive list of available Slack Events and more information on Scopes, see https://api.slack.com/events-api
🤖 Example event listeners
See example.py for usage examples. This example also utilizes the SlackClient Web API client.
You can also create an Issue right here on GitHub.