-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
196 lines (164 loc) · 5.54 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from flask import Flask, request, jsonify
from newsapi import NewsApiClient
from slackclient import SlackClient
from threading import Thread
import json
import os
NEWS_API_TOKEN = os.environ.get('NEWS_API_TOKEN')
SLACK_BOT_TOKEN = os.environ.get('SLACK_BOT_TOKEN')
SLACK_APP_TOKEN = os.environ.get('SLACK_APP_TOKEN')
SLACK_VERIFICATION_TOKEN = os.environ.get('SLACK_VERIFICATION_TOKEN')
BOT_USER_ID = os.environ.get('BOT_USER_ID')
newsapi = NewsApiClient(api_key=NEWS_API_TOKEN)
sc_bot = SlackClient(SLACK_BOT_TOKEN)
sc_app = SlackClient(SLACK_APP_TOKEN)
app = Flask(__name__)
def search_news_api(payload):
"""
Uses the News API to get news stories based on a search query
"""
user_id = payload['user_id']
channel_id = payload['channel_id']
# Make sure the bot is in the channel it's being called from
sc_app.api_call(
"conversations.invite",
channel=channel_id,
users=BOT_USER_ID
)
search_term = payload['text']
all_headlines = newsapi.get_top_headlines(q=search_term)
top_headlines = all_headlines['articles'][:5]
# If there are top headlines, reply to the user
if len(top_headlines) > 0:
messages = create_interactive_messages(top_headlines)
for message in messages:
sc_bot.api_call(
"chat.postEphemeral",
channel=channel_id,
attachments=message,
user=user_id
)
# If there are not top headlines, try getting any headlines
else:
all_headlines = newsapi.get_everything(q=search_term, sort_by='relevancy')
top_headlines = all_headlines['articles'][:5]
# If there are any headlines, reply to the user
if len(top_headlines) > 0:
messages = create_interactive_messages(top_headlines)
for message in messages:
sc_bot.api_call(
"chat.postEphemeral",
channel=channel_id,
attachments=message,
user=user_id
)
# If there are no headlines, respond saying so
else:
sc_bot.api_call(
"chat.postEphemeral",
channel=channel_id,
text="No news found with the search `%s`" % search_term,
user=user_id
)
def create_interactive_messages(headlines):
"""
Creates attatchemtns for interactive messages with buttons to share and/or dismiss them.
"""
messages = []
for headline in headlines:
message = [
{
"title": headline["title"],
"author_name": headline["source"]["name"],
"text": headline["description"],
"title_link": headline["url"],
"thumb_url": headline["urlToImage"],
},
{
"title": "Share in channel?",
"callback_id": "share",
"attachment_type": "default",
"actions": [
{
"name": "share",
"text": "Share",
"type": "button",
"value": json.dumps(headline), # Used to recreate the message when pushed
"style": "primary"
},
{
"name": "dismiss",
"text": "Dismiss",
"type": "button",
"value": "",
"style": "danger"
}
]
}
]
messages.append(message)
return messages
def create_story_message(headline):
"""
Creates attatchement for headline being shared
"""
return [
{
"title": headline["title"],
"author_name": headline["source"]["name"],
"text": headline["description"],
"title_link": headline["url"],
"thumb_url": headline["urlToImage"],
}
]
def authorize(token):
"""
Verfiy the message is coming from Slack
"""
if token == SLACK_VERIFICATION_TOKEN:
return True
else:
return False
@app.route('/', methods=['GET'])
def index():
"""
Confirm that the server is working
"""
return ('Working.')
@app.route('/get-news', methods=['POST'])
def get_news():
"""
Handles slash command webhooks being sent to this server
"""
if not authorize(request.form["token"]):
return (None, 403, None)
# Start getting and posting the news in a new thread
thr = Thread(target=search_news_api, args=[request.form])
thr.start()
return ('', 204)
@app.route('/interactive-messages', methods=['POST'])
def interactive_messages():
"""
Handles interactive message webhooks being sent to this server
"""
payload = json.loads(request.form['payload'])
if not authorize(payload["token"]):
return (None, 403, None)
# If the share button was pressed
if payload["actions"][0]["name"] == "share":
headline = json.loads(payload["actions"][0]["value"])
message = create_story_message(headline)
channel_id = payload["channel"]["id"]
user_id = payload["user"]["id"]
sc_bot.api_call(
"chat.postMessage",
text="<@%s> shared this:" % user_id,
channel=channel_id,
attachments=message,
)
# Regardless, delete the message
return jsonify({
'delete_original': True
})
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)