-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·326 lines (286 loc) · 9.74 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import json
from sys import modules, stdout
from flask import Flask, request, jsonify, render_template
from flask_caching import Cache
app = Flask(__name__)
cache = Cache(app, config={
'CACHE_TYPE': 'simple',
})
app.cache = cache
with app.app_context():
# Some of these need to be aware of 'from flask import current_app'
import event
import commands
import libresponse
import dialog
import menu
import libcache
import botconfig
from lambda_exception import caught
import libfox
import auth
# PEP8 complains
menu
dialog
libcache
FIELD_OK = 'ok'
FIELD_MSG = 'msg'
FIELD_RESULT = 'result'
LOG = logging.getLogger(__name__)
LOG.setLevel(botconfig.DEFAULT_LOGGING)
available_loggers = sorted(logging.Logger.manager.loggerDict.keys())
LOG.info(
"The following Log Instances are Available to Set:\n%s",
json.dumps(available_loggers, indent=4, separators=(',', ' : '))
)
event_processors = [
event.pevent,
]
def get_request_data(request):
if request.data:
dat = json.loads(request.data)
elif request.form:
dat = json.loads(request.form)
else:
raise ValueError("post data from {} unavailable".format(request))
return dat
def pi_token_verify(headers):
token = headers.get('CTFTOKEN')
if token and botconfig.CTFTOKEN and token == botconfig.CTFTOKEN:
return True
else:
return False
def slack_token_verify(data):
token = data.get('token')
if botconfig.verifcation_token and token and token == botconfig.verifcation_token:
return True
else:
return False
@app.route("/hi", methods=["GET"])
def hi_there():
""" waves """
return "hi there"
@app.route("/add_fox", methods=["POST"])
def add_fox():
""" add new fox """
if not pi_token_verify(request.headers):
return libresponse.bad_token(request)
try:
data = get_request_data(request)
LOG.info("revieced update: {}".format(data))
res = libfox.add_fox(data, request.headers.__dict__)
res[FIELD_OK] = True
res[FIELD_MSG] = 'OK'
return jsonify(res)
except Exception as e:
LOG.exception("Error on add_fox")
return jsonify({
FIELD_OK: False,
FIELD_MSG: e.message,
})
@app.route("/delete_fox", methods=["POST"])
def delete_fox():
""" update single fox entry"""
if not pi_token_verify(request.headers):
return libresponse.bad_token(request)
try:
data = get_request_data(request)
LOG.info("revieced update: {}".format(data))
res = libfox.delete_fox(data)
res[FIELD_OK] = True
res[FIELD_MSG] = 'OK'
return jsonify(res)
except Exception as e:
LOG.exception("Error on add_fox")
return jsonify({
FIELD_OK: False,
FIELD_MSG: e.message,
})
@app.route("/update_fox", methods=["POST"])
def update_fox():
""" update single fox entry"""
if not pi_token_verify(request.headers):
return libresponse.bad_token(request)
try:
data = get_request_data(request)
LOG.info("revieced update: {}".format(data))
res = libfox.update_fox(data)
res[FIELD_OK] = True
res[FIELD_MSG] = 'OK'
return jsonify(res)
except Exception as e:
LOG.exception("Error on add_fox")
return jsonify({
FIELD_OK: False,
FIELD_MSG: e.message,
})
@app.route("/foxes", methods=["GET"])
def get_foxes():
""" return a json list of all foxes """
if not pi_token_verify(request.headers):
return libresponse.bad_token(request)
vals = libfox.get_foxes()
return jsonify(vals)
# Process form input
@app.route("/form", methods=["POST"])
def formprocess():
data = request.form.getlist('payload')
assert len(data) == 1, "Error, form data length != 1 @ {}".format(
len(data)
)
data = [json.loads(i) for i in data][0]
if not slack_token_verify(data):
return libresponse.bad_token(request)
LOG.debug(
"/form\n%s",
json.dumps(data, indent=4, separators=(',', ' : '))
)
response_url = data.get('response_url')
if not response_url:
raise Exception("Error, no response url detected")
callback = data.get('callback_id')
try:
mymodule = modules[__name__]
func_name = callback.split('.')[-1]
import_chain = callback.split('.')[:-1]
for mod in import_chain:
mymodule = getattr(mymodule, mod)
cb_func = getattr(mymodule, func_name)
process_data = cb_func(data)
error_list = process_data['error_list']
if error_list:
retarr = {
'errors': error_list,
}
pretty = json.dumps(retarr, indent=4, separators=(',', ' : '))
LOG.error("Returning Validation Error: {}".format(pretty))
return jsonify(retarr)
process_data['func'](process_data['data'])
return libresponse.ok_say()
except Exception as e:
LOG.exception("Form Exception")
caught(e, data, response_url)
return libresponse.error(e.message)
# Process menu calls
@app.route("/menu", methods=["POST"])
def menuprocess():
data = request.form.getlist('payload')
assert len(data) == 1, "Error, form data length != 1 @ {}".format(
len(data)
)
data = [json.loads(i) for i in data][0]
if not slack_token_verify(data):
return libresponse.bad_token(request)
try:
mymodule = modules[__name__]
func_name = data.get('name').split('.')[-1]
import_chain = data.get('name').split('.')[:-1]
for mod in import_chain:
mymodule = getattr(mymodule, mod)
cb_func = getattr(mymodule, func_name)
return cb_func(data)
except Exception as e:
LOG.exception("Menu Exception")
caught(e, data)
@app.route("/cmd", methods=["POST"])
def docmd():
data = request.form
if not data:
raise Exception("Error, data is empty in request")
if not slack_token_verify(data):
return libresponse.bad_token(request)
LOG.debug(
"/cmd\n%s",
json.dumps(data, indent=4, separators=(',', ' : '))
)
response_url = data.get('response_url')
try:
commands.handler(data)
return libresponse.ok_say()
except Exception as e:
LOG.exception("Command Exception")
caught(e, data, response_url)
return libresponse.error(e.message)
@app.route("/event", 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.
"""
if request.data:
slack_event = json.loads(request.data)
elif request.form:
slack_event = json.loads(request.form)
else:
raise Exception("Error, request.data/form not present")
stdout.flush()
LOG.debug("EVENT {}".format(
(json.dumps(slack_event, indent=4, separators=(',', ' : ')))
))
# ============= 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:
challenge = slack_event.get('challenge')
return jsonify(challenge)
# ============ 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 not slack_token_verify(slack_event):
return libresponse.bad_token(request)
# If our bot hears things that are not events we've subscribed to,
# send a quirky but helpful error response
if "event" not in slack_event:
return libresponse.not_found(
"[NO EVENT IN SLACK REQUEST] These are not the droids you're looking for.",
)
# ====== Process Incoming Events from Slack ======= #
# If the incoming request is an Event we've subcribed to
process_errors = []
for processor in event_processors:
try:
processor.Process(slack_event).process_event()
except Exception as e:
LOG.exception("Event Exception")
process_errors.append("Error processing event: {} error:{}\n{}".format(
processor.__name__,
e,
slack_event,
))
caught(e, slack_event)
if process_errors:
return libresponse.error("\n".join(process_errors))
return libresponse.ok_say()
@app.route("/callback", methods=["GET", "POST"])
def callback():
if not botconfig.ENABLE_CALLBACK:
return libresponse.error("no cb")
# Let's grab that temporary authorization code Slack's sent us from
# the request's parameters.
code_arg = request.args.get('code')
if not code_arg:
raise Exception(
"Error, code not specified during oauth setup, see: {}".format(
'https://api.slack.com/docs/slack-button',
)
)
# The bot's auth method to handles exchanging the code for an OAuth token
auth.application_auth(code_arg)
return render_template("thanks.html")
# This is pretty simple, pretty much a static html page based on class varibles
@app.route("/install", methods=["GET"])
def pre_install():
if not botconfig.ENABLE_CALLBACK:
return libresponse.error("no cb")
"This route renders the installation page with 'Add to Slack' button."
return render_template(
"install.html",
bot_name=botconfig.bot_name,
client_id=botconfig.client_id,
scope=botconfig.bot_scope,
)