Skip to content

Commit 63b0999

Browse files
authored
Update example app for Single Team Apps (remove OAuth) (#7)
* Removed OAuth from Events API demo app * Updated README to reflect single team app install flow
1 parent de5a206 commit 63b0999

2 files changed

Lines changed: 50 additions & 97 deletions

File tree

example/README.rst

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Example Slack events API bot
2-
============================
2+
=============================
33

44
This example app shows how easy it is to implement the Slack Events API Adapter
55
to receive Slack Events, extend it to handle the OAuth flow and respond to
66
messages using Slack's Web API via python-slackclient.
77

88
🤖 Setup and running the app
9-
------------
9+
------------------------------
1010

1111
**Set up your Python environment:**
1212

@@ -30,20 +30,45 @@ See `virtualenv`_ docs for more info.
3030

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

33-
.. image:: https://cloud.githubusercontent.com/assets/32463/20549718/afdd98d0-b0e3-11e6-8d83-8ad7053deb80.png
33+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877733/32979776-1de5-11e7-87d4-b5dc9e3e7973.png
3434

35-
Add a **bot user** for your app
35+
**🤖 Add a bot user to your app**
3636

37-
.. image:: https://cloud.githubusercontent.com/assets/32463/20371297/9044e2a0-ac18-11e6-8f25-3ffbd8a3bf58.png
37+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877750/47a16034-1de5-11e7-989b-2a90b9d8e7e3.png
3838

39-
**🤖 Run the app:**
39+
**🤖 Install your app on your team**
4040

41-
You'll need to have your server and ngrok running to complete your app's Event
42-
Subscription setup
41+
Visit your app's **Install App** page and click **Install App to Team**.
42+
43+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877770/61804c36-1de5-11e7-91ef-5cf2e0845729.png
44+
45+
Authorize your app
46+
47+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877792/774ed94c-1de5-11e7-8857-ac8d662c5b27.png
48+
49+
**🤖 Save your app's credentials**
50+
51+
Once you've authorized your app, you'll be presented with your app's tokens.
52+
53+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877652/d8eebbb4-1de4-11e7-8f75-2cfb1e9d45ee.png
54+
55+
Copy your app's **Bot User OAuth Access Token** and add it to your python environmental variables
4356

4457
.. code::
4558
46-
python example.py
59+
export SLACK_BOT_TOKEN=xxxXXxxXXxXXxXXXXxxxX.xXxxxXxxxx
60+
61+
Next, go back to your app's **Basic Information** page
62+
63+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877833/950dd53c-1de5-11e7-984f-deb26e8b9482.png
64+
65+
Add your app's **Client ID**, **Client Secret** and **Verification Token** to your python environmental variables
66+
67+
.. code::
68+
69+
export SLACK_CLIENT_ID=xxxxxxxxxxx.xxxxxxxxxxxxx
70+
export SLACK_CLIENT_SECRET=XxxxXxxXXXxxXxxXX
71+
export SLACK_VERIFICATION_TOKEN=xxxxxxxxXxxXxxXxXXXxxXxxx
4772
4873
**🤖 Start ngrok**
4974

@@ -71,48 +96,24 @@ Run ngrok and copy the **HTTPS** URL
7196
Forwarding http://h7465j.ngrok.io -> localhost:9292
7297
Forwarding https://h7465j.ngrok.io -> localhost:9292
7398
74-
**🤖 Subscribe your app to events**
75-
76-
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`
77-
78-
.. image:: https://cloud.githubusercontent.com/assets/32463/20366593/b40d14a4-ac00-11e6-8413-b473c16ef997.png
79-
80-
.. image:: https://cloud.githubusercontent.com/assets/32463/20549612/e7ee2ed4-b0e2-11e6-8b9c-01ed08057c7c.png
81-
82-
**🤖 Save your app's credentials**
83-
84-
Go to your app's **Basic Information** page and save your app's **Client ID**, **Client Secret** and **Verification Token** to environmental variables
85-
86-
.. image:: https://cloud.githubusercontent.com/assets/32463/20445302/61ddfc54-ad89-11e6-8523-245a60c875b0.png
87-
88-
**🤖 Add your OAuth URL**
89-
90-
Add your OAuth redirect URL (your ngrok URL + ``/auth/slack/callback``)
91-
92-
.. image:: https://cloud.githubusercontent.com/assets/32463/20543629/63e41a26-b0bb-11e6-8eee-90c6f4f1dbb1.png
93-
94-
**🤖 Assign your app's credentials to environment variables:**
95-
96-
.. code::
97-
98-
export SLACK_CLIENT_ID=xxxxxxxxxxx.xxxxxxxxxxxxx
99-
export SLACK_CLIENT_SECRET=XxxxXxxXXXxxXxxXX
100-
export SLACK_VERIFICATION_TOKEN=xxxxxxxxXxxXxxXxXXXxxXxxx
99+
**🤖 Run the app:**
101100

102-
**🤖 Restart your app to load it's credentials**
101+
You'll need to have your server and ngrok running to complete your app's Event
102+
Subscription setup
103103

104104
.. code::
105105
106106
python example.py
107107
108+
**🤖 Subscribe your app to events**
108109

109-
**🤖 Auth your app**
110+
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`
110111

111-
Go to your ngrok URL (e.g. https://myapp12.ngrok.com/) and auth your app.
112+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877867/b39d4384-1de5-11e7-9676-9e47ea7db4e7.png
112113

113-
.. image:: https://cloud.githubusercontent.com/assets/32463/20575277/789c0c5a-b16d-11e6-86fd-e30c3a0d2e61.gif
114+
.. image:: https://cloud.githubusercontent.com/assets/32463/24877931/e119181a-1de5-11e7-8b0c-fcbc3419bad7.png
114115

115-
**🎉 Once your app has been authorized, you will begin receiving Slack events**
116+
**🎉 Once your app has been installed and subscribed to Bot Events, you will begin receiving event data from Slack**
116117

117118
**👋 Interact with your bot:**
118119

@@ -123,15 +124,12 @@ Invite your bot to a public channel, then say hi and your bot will respond
123124
.. image:: https://cloud.githubusercontent.com/assets/32463/23047918/964defec-f467-11e6-87c3-9c7da11fc810.gif
124125

125126
🤔 Support
126-
-------
127+
------------
127128

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

130131
You can also `create an Issue`_ right here on GitHub.
131132

132-
.. _Events API: https://api.slack.com/events-api
133-
.. _create a Slack App: https://api.slack.com/apps/new
134-
.. _Event Subscriptions: https://api.slack.com/events-api#subscriptions
135133
.. _Bot Developer Hangout: http://dev4slack.xoxco.com/
136134
.. _#slack-api: https://dev4slack.slack.com/messages/slack-api/
137135
.. _create an Issue: https://github.com/slackapi/node-slack-events-api/issues/new

example/example.py

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,35 @@
1111
SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"]
1212
slack_events_adapter = SlackEventAdapter(SLACK_VERIFICATION_TOKEN, "/slack/events")
1313

14-
# Since SlackEventAdapter uses a standard Flask server, we can extend it to handle OAuth
15-
# by simply adding a couple more route handlers.
16-
1714
# Slack App credentials for OAuth
1815
SLACK_CLIENT_ID = os.environ["SLACK_CLIENT_ID"]
1916
SLACK_CLIENT_SECRET = os.environ["SLACK_CLIENT_SECRET"]
17+
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
2018

19+
# Create a SlackClient for your bot to use for Web API requests
20+
CLIENT = SlackClient(SLACK_BOT_TOKEN)
2121

22-
# This route renders the installation page with 'Add to Slack' button.
23-
@slack_events_adapter.server.route("/", methods=["GET"])
24-
def pre_install():
25-
add_to_slack = """
26-
<a href="https://slack.com/oauth/authorize?scope=bot&client_id=%s">
27-
<img alt="Add to Slack" src="https://platform.slack-edge.com/img/add_to_slack.png"/>
28-
</a>
29-
""" % SLACK_CLIENT_ID
30-
return add_to_slack
31-
32-
33-
# This route is called by Slack after the user installs our app. It will
34-
# exchange the temporary authorization code Slack sends for an OAuth token
35-
# which we'll save on the bot object to use later.
36-
# To let the user know what's happened it will also render a thank you page.
37-
@slack_events_adapter.server.route("/auth/slack/callback", methods=["GET"])
38-
def thanks():
39-
40-
# Get the OAuth code to pass into the token request
41-
auth_code = request.args.get('code')
42-
43-
# Create a temporary Slack client to make the OAuth request.
44-
# This client doesn't need a token.
45-
slack_client = SlackClient("")
4622

47-
# Ask Slack for a bot token, using the auth code we received
48-
auth_response = slack_client.api_call(
49-
"oauth.access",
50-
client_id=SLACK_CLIENT_ID,
51-
client_secret=SLACK_CLIENT_SECRET,
52-
code=auth_code
53-
)
54-
55-
# Grab the user's team info and token from the OAuth response
56-
team_id = auth_response.get("team_id")
57-
team_name = auth_response.get("team_name")
58-
bot_token = auth_response["bot"].get("bot_access_token")
59-
60-
# Create a SlackClient for your bot to use for Web API requests
61-
CLIENTS[team_id] = SlackClient(bot_token)
62-
return "Your app has been installed on <b>%s</b>" % team_name
63-
64-
65-
# Now we'll set up some event listeners for our app to process and respond to
6623
# Example responder to greetings
6724
@slack_events_adapter.on("message")
6825
def handle_message(event_data):
69-
team_id = event_data["team_id"]
7026
message = event_data["event"]
27+
# If the incoming message contains "hi", then respond with a "Hello" message
7128
if message.get("subtype") is None and "hi" in message.get('text'):
7229
channel = message["channel"]
7330
message = "Hello <@%s>! :tada:" % message["user"]
74-
CLIENTS[team_id].api_call("chat.postMessage", channel=channel, text=message)
31+
CLIENT.api_call("chat.postMessage", channel=channel, text=message)
7532

7633

7734
# Example reaction emoji echo
7835
@slack_events_adapter.on("reaction_added")
7936
def reaction_added(event_data):
80-
team_id = event_data["team_id"]
8137
event = event_data["event"]
8238
emoji = event["reaction"]
8339
channel = event["item"]["channel"]
8440
text = ":%s:" % emoji
85-
CLIENTS[team_id].api_call("chat.postMessage", channel=channel, text=text)
86-
41+
CLIENT.api_call("chat.postMessage", channel=channel, text=text)
8742

88-
# Once we have our addon routes and event listeners configured, we can start the
43+
# Once we have our event listeners configured, we can start the
8944
# Flask server with the default `/events` endpoint on port 3000
9045
slack_events_adapter.start(port=3000)

0 commit comments

Comments
 (0)