Skip to main content

Slack Events API adapter for Python

https://badge.fury.io/py/slackeventsapi.svg https://github.com/slackapi/python-slack-events-api/actions/workflows/ci-build.yml/badge.svg https://codecov.io/gh/slackapi/python-slack-events-api/branch/main/graph/badge.svg

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.

💡 We wrote a blog post which explains how the Events API can help you, why we built these tools, and how you can use them to build production-ready Slack apps.

⚠️ Important Notice

If you use this library for Events API handling, you may need to use threads for stable event acknowledgement. See https://github.com/slackapi/python-slack-events-api/issues/84 for details.

If you’re looking for the best recommended library at this point, check Bolt for Python: https://github.com/slackapi/bolt-python

The framework covers not only Events API but also all the latest Slack Platform features.

🤖 Installation

pip install slackeventsapi

🤖 App Setup

Before you can use the Events API you must create a Slack App, and turn on Event Subscriptions.

💡 When you add the Request URL to your app’s Event Subscription settings, Slack will send a request containing a challenge code to verify that your server is alive. This package handles that URL Verification event for you, so all you need to do is start the example app, start ngrok and configure your URL accordingly.

✅ Once you have your Request URL verified, your app is ready to start receiving Team Events.

🔑 Your server will begin receiving Events from Slack’s Events API as soon as a user has authorized your app.

🤖 Development workflow:

  1. Create a Slack app on https://api.slack.com/apps

  2. Add a bot user for your app

  3. Start the example app on your Request URL endpoint

  4. Start ngrok and copy the HTTPS URL

  5. Add your Request URL and subscribe your app to events

  6. Go to your ngrok URL (e.g. https://myapp12.ngrok.com/) and auth your app

🎉 Once your app has been authorized, you will begin receiving Slack Events

⚠️ Ngrok is a great tool for developing Slack apps, but we don’t recommend using ngrok for production apps.

🤖 Usage

⚠️ 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_data):
  emoji = event_data["event"]["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_data):
  emoji = event_data["event"]["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.

🤔 Support

Need help? Join Slack Community and talk to us in #slack-api.

You can also create an Issue right here on GitHub.

Metadata

Release files for slackeventsapi 3.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slackeventsapi 3.0.3
File Size Uploaded
slackeventsapi-3.0.3.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slackeventsapi 3.0.3
File Interpreter ABI Platform
slackeventsapi-3.0.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 14.7 kB

Release files / slackeventsapi-3.0.3.tar.gz

Download URL slackeventsapi-3.0.3.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
3277b66893bd16f3684b38d860b1fd42af6eca64060c5b7d1f50afbc49eb3b23
BLAKE2b-256 checksum
How to use checksums
c7851ff2e9a47c5020260f806c82282b5590fbe088e9358d171e601f2fccb3ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.2

Release files / slackeventsapi-3.0.3-py2.py3-none-any.whl

Download URL slackeventsapi-3.0.3-py2.py3-none-any.whl
Size 7.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4bb62f55d9d5148c6d07ea355d201ff3c8febfd6c8f8e13ff36ae842ec5cc5fa
BLAKE2b-256 checksum
How to use checksums
a24044c2e3d23a44abbe3182f925c5e840d047a4de39fb8ab9f0785aa39dab63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

3.0.3 This release

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

1 release file

2.0.0

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page