|
| 1 | +Slack Events API adapter for Python |
| 2 | +=================================== |
| 3 | + |
| 4 | +The Slack Events Adapter is a Python-based solution to receive and parse events |
| 5 | +from Slack’s Events API. This library uses an event emitter framework to allow |
| 6 | +you to easily process Slack events by simply attaching functions |
| 7 | +to event listeners. |
| 8 | + |
| 9 | +🤖 Installation |
| 10 | +------------ |
| 11 | + |
| 12 | +.. code:: |
| 13 | +
|
| 14 | + pip install slackeventsapi |
| 15 | +
|
| 16 | +🤖 App Setup |
| 17 | +-------------------- |
| 18 | + |
| 19 | +Before you can use the `Events API`_ you must |
| 20 | +`create a Slack App`_, and turn on |
| 21 | +`Event Subscriptions`_. |
| 22 | + |
| 23 | +.. _Events API: https://api.slack.com/events-api |
| 24 | +.. _create a Slack App: https://api.slack.com/apps/new |
| 25 | +.. _Event Subscriptions: https://api.slack.com/events-api#subscriptions |
| 26 | + |
| 27 | +💡 When you add the Request URL to your app's Event Subscription settings, |
| 28 | +Slack will send a request containing a `challenge` code to verify that your |
| 29 | +server is alive. This package handles that URL Verification event for you, so |
| 30 | +all you need to do is start the example app, start ngrok and configure your |
| 31 | +URL accordingly. |
| 32 | + |
| 33 | +✅ Once you have your `Request URL` verified, your app is ready to start |
| 34 | +receiving Team Events. |
| 35 | + |
| 36 | +🔑 Your server will begin receiving Events form Slack's Events API as soon as a |
| 37 | +user has authorized your app. |
| 38 | + |
| 39 | +🤖 Development workflow: |
| 40 | +------------------------------ |
| 41 | + |
| 42 | +(1) Create a Slack app on https://api.slack.com/apps/ |
| 43 | +(2) Add a `bot user` for your app |
| 44 | +(3) Start the example app on your **Request URL** endpoint |
| 45 | +(4) Start ngrok and copy the **HTTPS** URL |
| 46 | +(5) Add your **Request URL** and subscribe your app to events |
| 47 | +(6) Go to your ngrok URL (e.g. https://myapp12.ngrok.com/) and auth your app |
| 48 | + |
| 49 | +**🎉 Once your app has been authorized, you will begin receiving Slack Events** |
| 50 | + |
| 51 | + ⚠️ We strongly discourage using ngrok for |
| 52 | + anything but development. It’s not well-suited for production use. |
| 53 | + |
| 54 | + 💡 See the example app included in this repository for more information |
| 55 | + on implementing OAuth. |
| 56 | + |
| 57 | +🤖 Usage |
| 58 | +----- |
| 59 | + **⚠️ Keep your app's credentials safe!** |
| 60 | + |
| 61 | + - For development, keep them in virtualenv variables. |
| 62 | + |
| 63 | + - For production, use a secure data store. |
| 64 | + |
| 65 | + - Never post your app's credentials to github. |
| 66 | + |
| 67 | +.. code:: python |
| 68 | +
|
| 69 | + SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"] |
| 70 | +
|
| 71 | +Create a Slack Event Adapter for receiving actions via the Events API |
| 72 | + |
| 73 | +.. code:: python |
| 74 | +
|
| 75 | + from slackeventsapi import SlackEventAdapter |
| 76 | +
|
| 77 | + slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, endpoint="/slack_events") |
| 78 | +
|
| 79 | +Create an event listener for "reaction_added" events and print the emoji name |
| 80 | + |
| 81 | +.. code:: python |
| 82 | +
|
| 83 | + @slack_events_adapter.on("reaction_added") |
| 84 | + def reaction_added(event): |
| 85 | + emoji = event.get("reaction") |
| 86 | + print(emoji) |
| 87 | +
|
| 88 | +
|
| 89 | +Start the server on port 3000 |
| 90 | + |
| 91 | +.. code-block:: python |
| 92 | +
|
| 93 | + slack_events_adapter.start(port=3000) |
| 94 | +
|
| 95 | +For a comprehensive list of available Slack `Events` and more information on |
| 96 | +`Scopes`, see https://api.slack.com/events-api |
| 97 | + |
| 98 | +🤖 Examples |
| 99 | +-------- |
| 100 | + |
| 101 | +See `example.py`_ for usage examples. This example also utilizes OAuth and the |
| 102 | +SlackClient Web API client. |
| 103 | + |
| 104 | +.. _example.py: /example/ |
| 105 | + |
| 106 | +🤔 Support |
| 107 | +------- |
| 108 | + |
| 109 | +Need help? Join `dev4slack`_ and talk to us in `#slack-api`_. |
| 110 | + |
| 111 | +You can also `create an Issue`_ right here on GitHub. |
| 112 | + |
| 113 | +.. _Events API: https://api.slack.com/events-api |
| 114 | +.. _create a Slack App: https://api.slack.com/apps/new |
| 115 | +.. _Event Subscriptions: https://api.slack.com/events-api#subscriptions |
| 116 | +.. _dev4slack: http://dev4slack.xoxco.com/ |
| 117 | +.. _#slack-api: https://dev4slack.slack.com/messages/slack-api/ |
| 118 | +.. _create an Issue: https://github.com/slackapi/python-slack-events-api/issues/new |
0 commit comments