|
| 1 | +Example Slack events API bot |
| 2 | +============================= |
| 3 | + |
| 4 | +This example app shows how easy it is to implement the Slack Events API Adapter |
| 5 | +to receive Slack Events and respond to |
| 6 | +messages using Slack's Web API via python-slackclient. |
| 7 | + |
| 8 | +🤖 Setup and running the app |
| 9 | +------------------------------ |
| 10 | + |
| 11 | +**Set up your Python environment:** |
| 12 | + |
| 13 | +We're using virtualenv to keep the dependencies and environmental variables specific to this app. See `virtualenv`_ docs for more info. |
| 14 | + |
| 15 | +.. _virtualenv: https://virtualenv.pypa.io |
| 16 | + |
| 17 | +This example app works best in Python 2.7. If 2.7 is your default version, create a virtual environment by running: |
| 18 | + |
| 19 | +.. code:: |
| 20 | +
|
| 21 | + virtualenv env |
| 22 | +
|
| 23 | +Otherwise, if Python 3+ is your default, specify the path to your 2.7 instance: |
| 24 | + |
| 25 | +.. code:: |
| 26 | +
|
| 27 | + virtualenv -p /your/path/to/python2 env |
| 28 | +
|
| 29 | +Then initialize the virtualenv: |
| 30 | + |
| 31 | +.. code:: |
| 32 | +
|
| 33 | + source env/bin/activate |
| 34 | +
|
| 35 | +
|
| 36 | +**Install the app's dependencies:** |
| 37 | + |
| 38 | +.. code:: |
| 39 | +
|
| 40 | + pip install -r requirements.txt |
| 41 | +
|
| 42 | +**🤖 Create a Slack app** |
| 43 | + |
| 44 | +Create a Slack app on https://api.slack.com/apps/ |
| 45 | + |
| 46 | +.. image:: https://cloud.githubusercontent.com/assets/32463/24877733/32979776-1de5-11e7-87d4-b5dc9e3e7973.png |
| 47 | + |
| 48 | +**🤖 Add a bot user to your app** |
| 49 | + |
| 50 | +.. image:: https://cloud.githubusercontent.com/assets/32463/24877750/47a16034-1de5-11e7-989b-2a90b9d8e7e3.png |
| 51 | + |
| 52 | +**🤖 Install your app on your team** |
| 53 | + |
| 54 | +Visit your app's **Install App** page and click **Install App to Team**. |
| 55 | + |
| 56 | +.. image:: https://cloud.githubusercontent.com/assets/32463/24877770/61804c36-1de5-11e7-91ef-5cf2e0845729.png |
| 57 | + |
| 58 | +Authorize your app |
| 59 | + |
| 60 | +.. image:: https://cloud.githubusercontent.com/assets/32463/24877792/774ed94c-1de5-11e7-8857-ac8d662c5b27.png |
| 61 | + |
| 62 | +**🤖 Save your app's credentials** |
| 63 | + |
| 64 | +Once you've authorized your app, you'll be presented with your app's tokens. |
| 65 | + |
| 66 | +.. image:: https://cloud.githubusercontent.com/assets/32463/24877652/d8eebbb4-1de4-11e7-8f75-2cfb1e9d45ee.png |
| 67 | + |
| 68 | +Copy your app's **Bot User OAuth Access Token** and add it to your python environmental variables |
| 69 | + |
| 70 | +.. code:: |
| 71 | +
|
| 72 | + export SLACK_BOT_TOKEN=xxxXXxxXXxXXxXXXXxxxX.xXxxxXxxxx |
| 73 | +
|
| 74 | +Next, go back to your app's **Basic Information** page |
| 75 | + |
| 76 | +.. image:: https://user-images.githubusercontent.com/32463/43932347-63b21eca-9bf8-11e8-8b30-0a848c263bb1.png |
| 77 | + |
| 78 | +Add your app's **Signing Secret** to your python environmental variables |
| 79 | + |
| 80 | +.. code:: |
| 81 | +
|
| 82 | + export SLACK_SIGNING_SECRET=xxxxxxxxXxxXxxXxXXXxxXxxx |
| 83 | +
|
| 84 | +
|
| 85 | +**🤖 Start ngrok** |
| 86 | + |
| 87 | +In order for Slack to contact your local server, you'll need to run a tunnel. We |
| 88 | +recommend ngrok or localtunnel. We're going to use ngrok for this example. |
| 89 | + |
| 90 | +If you don't have ngrok, `download it here`_. |
| 91 | + |
| 92 | +.. _download it here: https://ngrok.com |
| 93 | + |
| 94 | + |
| 95 | +Here's a rudimentary diagream of how ngrok allows Slack to connect to your server |
| 96 | + |
| 97 | +.. image:: https://cloud.githubusercontent.com/assets/32463/25376866/940435fa-299d-11e7-9ee3-08d9427417f6.png |
| 98 | + |
| 99 | + |
| 100 | +💡 Slack requires event requests be delivered over SSL, so you'll want to |
| 101 | + use the HTTPS URL provided by ngrok. |
| 102 | + |
| 103 | +Run ngrok and copy the **HTTPS** URL |
| 104 | + |
| 105 | +.. code:: |
| 106 | +
|
| 107 | + ngrok http 3000 |
| 108 | +
|
| 109 | +.. code:: |
| 110 | +
|
| 111 | + ngrok by @inconshreveable (Ctrl+C to quit) |
| 112 | +
|
| 113 | + Session status online |
| 114 | + Version 2.1.18 |
| 115 | + Region United States (us) |
| 116 | + Web Interface http://127.0.0.1:4040 |
| 117 | +
|
| 118 | + Forwarding http://h7465j.ngrok.io -> localhost:9292 |
| 119 | + Forwarding https://h7465j.ngrok.io -> localhost:9292 |
| 120 | +
|
| 121 | +**🤖 Run the app:** |
| 122 | + |
| 123 | +You'll need to have your server and ngrok running to complete your app's Event |
| 124 | +Subscription setup |
| 125 | + |
| 126 | +.. code:: |
| 127 | +
|
| 128 | + python example.py |
| 129 | +
|
| 130 | +
|
| 131 | +**🤖 Subscribe your app to events** |
| 132 | + |
| 133 | +Add your **Request URL** (your ngrok URL + ``/slack/events``) and subscribe your app to `message.channels` under bot events. **Save** and toggle **Enable Events** to `on` |
| 134 | + |
| 135 | +.. image:: https://user-images.githubusercontent.com/1573454/30185162-644d0cb8-93ee-11e7-96af-55fe10d9d5c8.png |
| 136 | + |
| 137 | +.. image:: https://cloud.githubusercontent.com/assets/32463/24877931/e119181a-1de5-11e7-8b0c-fcbc3419bad7.png |
| 138 | + |
| 139 | +**🎉 Once your app has been installed and subscribed to Bot Events, you will begin receiving event data from Slack** |
| 140 | + |
| 141 | +**👋 Interact with your bot:** |
| 142 | + |
| 143 | +Invite your bot to a public channel, then say hi and your bot will respond |
| 144 | + |
| 145 | + hi @bot 👋 |
| 146 | + |
| 147 | +.. image:: https://cloud.githubusercontent.com/assets/32463/23047918/964defec-f467-11e6-87c3-9c7da11fc810.gif |
| 148 | + |
| 149 | +🤔 Support |
| 150 | +------------ |
| 151 | + |
| 152 | +Need help? Join `Bot Developer Hangout`_ and talk to us in `#slack-api`_. |
| 153 | + |
| 154 | +You can also `create an Issue`_ right here on GitHub. |
| 155 | + |
| 156 | +.. _Bot Developer Hangout: http://dev4slack.xoxco.com/ |
| 157 | +.. _#slack-api: https://dev4slack.slack.com/messages/slack-api/ |
| 158 | +.. _create an Issue: https://github.com/slackapi/node-slack-events-api/issues/new |
0 commit comments