-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
106 lines (79 loc) · 3.08 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
import random
from datetime import datetime
from flask import Flask, request, make_response, jsonify
from pprint import pprint, pformat
from loguru import logger
import json
import os
from config.config_parser import TacoTuesdayConfigParser
from config.system_config import TacoTuesdaySystemConfig
from lib.api.taco_tuesday_api_handler import TacoTuesdayApiHandler
from lib.proc.handler.employee import EmployeeHandler
from lib.proc.handler.error import ErrorHandler
from lib.proc.handler.interaction import InteractionHandler
from lib.proc.handler.running_order import RunningOrderHandler
from lib.proc.handler.view import ViewHandler
TacoTuesdayConfigParser.parse()
API_HANDLER = TacoTuesdayApiHandler()
IH = InteractionHandler()
# Flask web server for incoming traffic from Slack
app = Flask(__name__)
def get_arg(_request, key):
arg = _request.form.get(key)
logger.debug(key, ':', arg)
return arg
@app.route("/slack/interact/admin", methods=["POST"])
def admin_slash_command():
denials = [
"Yeah. Like I'd ever let you do that.",
"You do not have access to this command!",
"Don't make me tell on you.",
"You are not in the sudoers file. This incident will be reported."
]
denial = random.choice(denials)
slack_id = get_arg(request, 'user_id')
channel_id = get_arg(request, 'channel_id')
trigger_id = get_arg(request, 'trigger_id')
text = get_arg(request, 'text').lower()
try:
employee = API_HANDLER.get_employee_by_slack_id(slack_id)
if not employee.admin:
return make_response(denial, 200)
IH.admin(trigger_id)
return make_response("", 200)
except Exception as e:
ErrorHandler.handle(e)
@app.route("/slack/interact/order", methods=["POST"])
def order_slash_command():
for arg in request.args.keys():
pprint(request.args.get(arg))
slack_id = get_arg(request, 'user_id')
channel_id = get_arg(request, 'channel_id')
trigger_id = get_arg(request, 'trigger_id')
text = get_arg(request, 'text').lower()
try:
if text == 'cancel':
if not RunningOrderHandler.has_employee_order(slack_id):
EmployeeHandler.discipline_employee(slack_id, channel_id, "You haven't placed an order yet you goober!")
else:
ViewHandler.send_order_cancel_modal(trigger_id)
else:
IH.order(channel_id, trigger_id)
return make_response("", 200)
except Exception as e:
return ErrorHandler.handle(e)
@app.route("/slack/interact", methods=["POST"])
def message_actions():
try:
# Parse the request payload
payload = json.loads(request.form["payload"])
logger.debug(pformat(f'Payload: \n{payload}'))
response = IH.handle_interaction(payload)
if response: response = jsonify(response)
return response
except Exception as e:
return ErrorHandler.handle(e)
if __name__ == "__main__":
from pathlib import Path
logger.add(sink=Path(TacoTuesdaySystemConfig().get_log_dir(), '{time}.log'), rotation="00:00")
app.run(host='0.0.0.0')