dn0912 / python-slack-events-api

Slack Events API adapter for Python

Home Page:https://api.slack.com/events

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Slack Events API adapter for Python

https://travis-ci.org/slackapi/python-slack-events-api.svg?branch=master

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.

πŸ€– 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 form 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

⚠️ We strongly discourage using ngrok for anything but development. It’s not well-suited for production use.

πŸ€– 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_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"]

Create a Slack Event Adapter for receiving actions via the Events API

from slackeventsapi import SlackEventAdapter

slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, 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)

For a comprehensive list of available Slack Events and more information on Scopes, see https://api.slack.com/events-api

πŸ€– Examples

See example.py for usage examples. This example also utilizes the SlackClient Web API client.

πŸ€” Support

Need help? Join Bot Developer Hangout and talk to us in #slack-api.

You can also create an Issue right here on GitHub.

About

Slack Events API adapter for Python

https://api.slack.com/events

License:MIT License


Languages

Language:Python 100.0%