-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
140 lines (117 loc) · 5.74 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
# -*- 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 src.bot as bot
from flask import Flask, request, make_response, render_template
from src.categorizer import get_summarized_article, get_category
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
"""
# ================ Message Sent Events =============== #
# If a user has posted a message
# and that message has attachments
# and the attachment has 1 (or more) urls:
# Make a summary of the content at that url
# Send that summary to Google's NLP API
# Create a message with the return value from Google
if event_type == "message" and "event" in slack_event:
# Alias to make referencing easier
event = slack_event["event"]
if "message" in event and "subtype" in event:
if event["subtype"] != "message_deleted" and "attachments" in event["message"]:
# Get the URL for the first attachment
first_url = event["message"]["attachments"][0]["title_link"]
# Get the contents of the URL and summarize
summaraized_article = get_summarized_article(first_url)
# Send the summarized contents of the URL to Google
category = get_category(summaraized_article)
# Send the message, with the summarized article
# the category, and some of the event details
pyBot.categorized_message(category=category,
thread_id=event["previous_message"]["ts"],
channel=event["channel"],
summarized_text=summaraized_article)
return make_response("Categorized Message Sent", 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)