Giter Club home page Giter Club logo

python-slack-events-api's Introduction

Slack Events API adapter for Python

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

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 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.

python-slack-events-api's People

Contributors

seratch avatar roach avatar aoberoi avatar psykzz avatar datashaman avatar calvinhp avatar jefffranklin avatar kirby-jobber avatar navinpai avatar maryum375 avatar shaydewael avatar 515hikaru avatar benoitlavigne avatar brad-alexander avatar dependabot[bot] avatar equal-l2 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.