forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py.save
520 lines (494 loc) · 19.8 KB
/
main.py.save
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
# Copyright 2023 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import slack
import os
import multiprocessing as mp
from flask import Flask, request, Response
from slackeventsapi import SlackEventAdapter
from datetime import datetime
from gevent.pywsgi import WSGIServer
from autotrack_create import autotrack_create
from autotrack_edit import autotrack_edit
from autotrack_stop import autotrack_stop
from list_autotrack_all import list_autotrack_all
from case_details import case_details
from case_updates import case_updates
from get_firestore_tracked_cases import get_firestore_tracked_cases
from list_tracked_cases import list_tracked_cases
from list_tracked_cases_all import list_tracked_cases_all
from post_help_message import post_help_message
from sitrep import sitrep
from stop_tracking import stop_tracking
from support_add_comment import support_add_comment
from support_change_priority import support_change_priority
from support_close_case import support_close_case
from support_escalate import support_escalate
from support_subscribe_email import support_subscribe_email
from track_case import track_case
from asset_auto_cc import asset_auto_cc
from stop_asset_auto_cc import stop_asset_auto_cc
from edit_asset_auto_cc import edit_asset_auto_cc
from list_asset_auto_cc_subscriptions import list_asset_auto_cc_subscriptions
from google.cloud import logging
# To run this on the cheapest possible VM, we will only log Warnings and Errors
logging_client = logging.Client()
# The name of the log to write to
log_name = "my-log"
# Selects the log to write to
logger_gcp = logging_client.logger(log_name)
app = Flask(__name__)
client = slack.WebClient(token=os.environ.get("SLACK_TOKEN"))
ORG_ID = os.environ.get("ORG_ID")
SLACK_SIGNING_SECRET = os.environ.get("SIGNING_SECRET")
MAX_RETRIES = 3
slack_events = SlackEventAdapter(SLACK_SIGNING_SECRET, "/slack/events", app)
tracked_cases = get_firestore_tracked_cases()
# Handle all calls to the support bot
@app.route("/", methods=["POST"])
def gcp_support() -> Response:
"""
Takes a user's slash command from Slack and executes it. Multiprocessing
is used on commands that modify the case to prevent Slack timeouts.
Parameters
----------
request : Request
message and metadata that was submitted by Slack
Returns
-------
Response
tells Slack that the command was received and not to throw a timeout alert
200
HTTP 200 OK
403
HTTP 403 Forbidden, received if the request signature can"t be verified
"""
# Verify that the request is coming from our Slack
slack_timestamp = request.headers.get("X-Slack-Request-Timestamp")
slack_signature = request.headers.get("X-Slack-Signature")
result = slack_events.server.verify_signature(slack_timestamp,
slack_signature)
if result is False:
return Response(), 403
data = request.form
channel_id = data.get("channel_id")
channel_name = data.get("channel_name")
user_id = data.get("user_id")
user_name = data.get("user_name")
user_inputs = data.get("text").split(" ", 1)
command = user_inputs[0]
if command == "track-case":
try:
case = user_inputs[1]
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The track-case command expects argument [case_number]."
" The case number provided did not match with any cases in your org"
))
else:
track_case(channel_id, channel_name, case, user_id)
elif command == "add-comment":
try:
parameters = user_inputs[1].split(" ", 1)
case = parameters[0]
comment = parameters[1]
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The add-comment command expects arguments [case_number] [comment]."
" The comment does not need to be encapsulated in quotes."
" Your case number did not match with any cases in your org."))
else:
p = mp.Process(target=support_add_comment,
args=(
channel_id,
case,
comment,
user_id,
user_name,
))
p.start()
elif command == "change-priority":
try:
parameters = user_inputs[1].split(" ", 1)
case = parameters[0]
priority = parameters[1]
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=(
"The change-priority command expects arguments "
"[case_number] [priority, must be either P1|P2|P3|P4]."
" Your case number did not match with any cases in your org,"
" or the priority did not match the expected values."))
else:
p = mp.Process(target=support_change_priority,
args=(
channel_id,
case,
priority,
user_id,
))
p.start()
elif command == "subscribe":
try:
parameters = user_inputs[1].split(" ", 1)
case = parameters[0]
emails = parameters[1].split()
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=(
"The subscribe command expects arguments "
"[case_number] [email_1] ... [email_n]."
" Your case number did not match with any cases in your org,"
" or your command did not match the expected input format."
))
else:
p = mp.Process(target=support_subscribe_email,
args=(
channel_id,
case,
emails,
user_id,
))
p.start()
elif command == "escalate":
try:
parameters = user_inputs[1].split(" ", 2)
case = parameters[0]
reason = parameters[1]
justification = parameters[2]
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The escalate command expects arguments"
"[reason, must be either RESOLUTION_TIME|TECHNICAL_EXPERTISE"
"|BUSINESS_IMPACT] [justification]. The justification does not need"
" to be encapsulated in quotes. Either your case number did not"
" match with any cases in your org, the reason did not match one of"
" the expected values, or the justification was missing"))
else:
p = mp.Process(target=support_escalate,
args=(
channel_id,
case,
user_id,
reason,
justification,
user_name,
))
p.start()
elif command == "close-case":
try:
case = user_inputs[1]
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text="The close-case command expects arguments [case_number]")
else:
p = mp.Process(target=support_close_case,
args=(
channel_id,
case,
user_id,
))
p.start()
elif command == "stop-tracking":
try:
case = user_inputs[1]
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text="The stop-tracking command expects arguments [case_number]."
)
else:
p = mp.Process(target=stop_tracking,
args=(
channel_id,
channel_name,
case,
user_id,
))
p.start()
elif command == "list-tracked-cases":
p = mp.Process(target=list_tracked_cases,
args=(
channel_id,
channel_name,
user_id,
))
p.start()
elif command == "list-tracked-cases-all":
p = mp.Process(target=list_tracked_cases_all,
args=(
channel_id,
user_id,
))
p.start()
elif command == "case-details":
case = user_inputs[1]
p = mp.Process(target=case_details,
args=(
channel_id,
case,
user_id,
))
p.start()
elif command == "sitrep":
p = mp.Process(target=sitrep,
args=(
channel_id,
))
p.start()
elif command == "help":
context = ""
post_help_message(channel_id, user_id, context)
elif command == "auto-subscribe":
try:
parameters = user_inputs[1].split(" ", 2)
asset_type = parameters[0]
asset_id = parameters[1]
emails = parameters[2].split()
if asset_type not in ["organizations", "folders", "projects"]:
raise IndexError
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The auto-subscribe command expects arguments "
"[asset_type] [asset_id] [email_1] ... [email_n]. "
"asset_type must be one of the following: organizations, folders,"
" projects "
"Your command did not match the expected input format."))
else:
p = mp.Process(target=asset_auto_cc,
args=(
channel_id,
channel_name,
asset_type,
asset_id,
user_id,
emails,
))
p.start()
elif command == "edit-auto-subscribe":
try:
parameters = user_inputs[1].split(" ", 2)
asset_type = parameters[0]
asset_id = parameters[1]
emails = parameters[2].split()
logger_gcp.log_text(
f"type: {asset_type}, id: {asset_id}, emails: {emails}")
if asset_type not in ["organizations", "folders", "projects"]:
raise IndexError
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The edit-subscribe command expects arguments "
"[asset_type] [asset_id] [email_1] ... [email_n]. "
"asset_type must be one of the following: organizations, folders,"
" projects. "
"A pre-existing asset subscribtion must already exist. "
"Your command did not match the expected input format."))
else:
p = mp.Process(target=edit_asset_auto_cc,
args=(
channel_id,
channel_name,
asset_type,
asset_id,
user_id,
emails,
))
p.start()
elif command == "stop-auto-subscribe":
try:
parameters = user_inputs[1].split(" ", 1)
asset_type = parameters[0]
asset_id = parameters[1]
logger_gcp.log_text(f"type: {asset_type}, id: {asset_id}")
if asset_type not in ["organizations", "folders", "projects"]:
raise IndexError
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The stop-auto-subscribe command expects arguments [asset_type]"
" [asset_id]."
" asset_type must be one of the following: organizations, folders"
", projects"))
else:
p = mp.Process(target=stop_asset_auto_cc,
args=(
channel_id,
channel_name,
asset_type,
asset_id,
use_id,
))
p.start()
elif command == "list-auto-subscriptions-all":
p = mp.Process(target=list_asset_auto_cc_subscriptions,
args=(
channel_id,
channel_name,
))
p.start()
elif command == "autotrack-create":
try:
parameters = user_inputs[1].split(" ", 2)
asset_type = parameters[0]
asset_id = parameters[1]
priorities = parameters[2].split()
if asset_type not in ["organizations", "folders", "projects"]:
raise IndexError
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The autotrack-create command expects arguments "
"[asset_type] [asset_id] P1 ... P4. "
"asset_type must be one of the following: organizations, folders,"
" projects "
"Your command did not match the expected input format."))
else:
p = mp.Process(target=autotrack_create,
args=(
channel_id,
channel_name,
asset_type,
asset_id,
user_id,
priorities,
))
p.start()
elif command == "autotrack-edit":
try:
parameters = user_inputs[1].split(" ", 2)
asset_type = parameters[0]
asset_id = parameters[1]
priorities = parameters[2].split()
logger_gcp.log_text(
f"type: {asset_type}, id: {asset_id}, emails: {emails}")
if asset_type not in ["organizations", "folders", "projects"]:
raise IndexError
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The autotrack-edit command expects arguments "
"[asset_type] [asset_id] P1 ... P4. "
"asset_type must be one of the following: organizations, folders,"
" projects. "
"A pre-existing asset tracker must already exist. "
"Your command did not match the expected input format."))
else:
p = mp.Process(target=autotrack_edit,
args=(
channel_id,
channel_name,
asset_type,
asset_id,
user_id,
priorities,
))
p.start()
elif command == "autotrack-stop":
try:
parameters = user_inputs[1].split(" ", 1)
asset_type = parameters[0]
asset_id = parameters[1]
logger_gcp.log_text(f"type: {asset_type}, id: {asset_id}")
if asset_type not in ["organizations", "folders", "projects"]:
raise IndexError
except IndexError as e:
error_message = f"{e} : {datetime.now()}"
logger_gcp.log_text(error_message)
client.chat_postEphemeral(
channel=channel_id,
user=user_id,
text=
("The autotrack-stop command expects arguments [asset_type]"
" [asset_id]."
" asset_type must be one of the following: organizations, folders"
", projects"))
else:
p = mp.Process(target=autotrack_stop,
args=(
channel_id,
channel_name,
asset_type,
asset_id,
use_id,
))
p.start()
elif command == "list-autotrack-all":
p = mp.Process(target=list_autotrack_all,
args=(
channel_id,
channel_name,
))
p.start()
else:
context = "Sorry, that wasn't a recognized command. "
post_help_message(channel_id, user_id, context)
return Response(), 200
if __name__ == "__main__":
mp.set_start_method("spawn")
case_updates_process = mp.Process(target=case_updates, args=(False,))
case_updates_process.start()
http_server = WSGIServer(("", 5000), app)
http_server.serve_forever()
case_updates_process.join()