This repository has been archived by the owner on Apr 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
/
app.py
146 lines (124 loc) · 5.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
"""
A routing layer for the onboarding bot tutorial built using
[Slack's Events API](https://api.slack.com/events-api) in Python
"""
import bot
from flask import Flask, request, make_response, render_template
pyBot = bot.Bot()
slack = pyBot.client
app = Flask(__name__)
def _event_handler(event_type, slack_event):
"""
A helper function that routes events from Slack to our Bot
by event type and subtype.
Parameters
----------
event_type : str
type of event received from Slack
slack_event : dict
JSON response from a Slack reaction event
Returns
----------
obj
Response object with 200 - ok or 500 - No Event Handler error
"""
team_id = slack_event["team_id"]
# ================ Team Join Events =============== #
# When the user first joins a team, the type of event will be team_join
if event_type == "team_join":
user_id = slack_event["event"]["user"]["id"]
# Send the onboarding message
pyBot.onboarding_message(team_id, user_id)
return make_response("Welcome Message Sent", 200,)
# ============== Share Message Events ============= #
# If the user has shared the onboarding message, the event type will be
# message. We'll also need to check that this is a message that has been
# shared by looking into the attachments for "is_shared".
elif event_type == "message" and slack_event["event"].get("attachments"):
user_id = slack_event["event"].get("user")
if slack_event["event"]["attachments"][0].get("is_share"):
# Update the onboarding message and check off "Share this Message"
pyBot.update_share(team_id, user_id)
return make_response("Welcome message updates with shared message",
200,)
# ============= Reaction Added Events ============= #
# If the user has added an emoji reaction to the onboarding message
elif event_type == "reaction_added":
user_id = slack_event["event"]["user"]
# Update the onboarding message
pyBot.update_emoji(team_id, user_id)
return make_response("Welcome message updates with reactji", 200,)
# =============== Pin Added Events ================ #
# If the user has added an emoji reaction to the onboarding message
elif event_type == "pin_added":
user_id = slack_event["event"]["user"]
# Update the onboarding message
pyBot.update_pin(team_id, user_id)
return make_response("Welcome message updates with pin", 200,)
# ============= Event Type Not Found! ============= #
# If the event_type does not have a handler
message = "You have not added an event handler for the %s" % event_type
# Return a helpful error message
return make_response(message, 200, {"X-Slack-No-Retry": 1})
@app.route("/install", methods=["GET"])
def pre_install():
"""This route renders the installation page with 'Add to Slack' button."""
# Since we've set the client ID and scope on our Bot object, we can change
# them more easily while we're developing our app.
client_id = pyBot.oauth["client_id"]
scope = pyBot.oauth["scope"]
# Our template is using the Jinja templating language to dynamically pass
# our client id and scope
return render_template("install.html", client_id=client_id, scope=scope)
@app.route("/thanks", methods=["GET", "POST"])
def thanks():
"""
This route is called by Slack after the user installs our app. It will
exchange the temporary authorization code Slack sends for an OAuth token
which we'll save on the bot object to use later.
To let the user know what's happened it will also render a thank you page.
"""
# Let's grab that temporary authorization code Slack's sent us from
# the request's parameters.
code_arg = request.args.get('code')
# The bot's auth method to handles exchanging the code for an OAuth token
pyBot.auth(code_arg)
return render_template("thanks.html")
@app.route("/listening", methods=["GET", "POST"])
def hears():
"""
This route listens for incoming events from Slack and uses the event
handler helper function to route events to our Bot.
"""
slack_event = request.get_json()
# ============= Slack URL Verification ============ #
# In order to verify the url of our endpoint, Slack will send a challenge
# token in a request and check for this token in the response our endpoint
# sends back.
# For more info: https://api.slack.com/events/url_verification
if "challenge" in slack_event:
return make_response(slack_event["challenge"], 200, {"content_type":
"application/json"
})
# ============ Slack Token Verification =========== #
# We can verify the request is coming from Slack by checking that the
# verification token in the request matches our app's settings
if pyBot.verification != slack_event.get("token"):
message = "Invalid Slack verification token: %s \npyBot has: \
%s\n\n" % (slack_event["token"], pyBot.verification)
# By adding "X-Slack-No-Retry" : 1 to our response headers, we turn off
# Slack's automatic retries during development.
make_response(message, 403, {"X-Slack-No-Retry": 1})
# ====== Process Incoming Events from Slack ======= #
# If the incoming request is an Event we've subscribed to
if "event" in slack_event:
event_type = slack_event["event"]["type"]
# Then handle the event by event_type and have your bot respond
return _event_handler(event_type, slack_event)
# If our bot hears things that are not events we've subscribed to,
# send a quirky but helpful error response
return make_response("[NO EVENT IN SLACK REQUEST] These are not the droids\
you're looking for.", 404, {"X-Slack-No-Retry": 1})
if __name__ == '__main__':
app.run(debug=True)