This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
130 lines (101 loc) · 3.53 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
import json
import boto3
import logging
import os
from chalice import Chalice
from chalicelib.action import Action
from chalicelib.lib.helpers import parse_config
from chalicelib.lib.reminder import remind_users
from chalicelib.lib.slack import (
Slack,
slack_payload_extractor,
slack_responder,
verify_token,
)
from chalicelib.lib.sqs import send_message
app = Chalice(app_name="timereport")
app.debug = True
logger = logging.getLogger()
dir_path = os.path.dirname(os.path.realpath(__file__))
config = parse_config(f"{dir_path}/chalicelib/config.yaml")
config["backend_url"] = os.getenv("backend_url")
config["bot_access_token"] = os.getenv("bot_access_token")
config["signing_secret"] = os.getenv("signing_secret")
config["enable_lock_reminder"] = os.getenv("enable_lock_reminder")
config["format_str"] = "%Y-%m-%d"
config["command_queue"] = f"timereport-slack-command-{os.getenv('environment')}"
config["interactive_queue"] = f"timereport-slack-interactive-{os.getenv('environment')}"
config["enable_queue"] = os.getenv("enable_queue")
logger.setLevel(config["log_level"])
def dummy():
"""
The sole purpose is to force Chalice to generate the right permissions in the policy.
Does nothing and returns nothing.
"""
sqs = boto3.client("sqs")
sqs.send_message()
sqs.get_queue_url()
@app.route(
"/interactive",
methods=["POST"],
content_types=["application/x-www-form-urlencoded"],
)
def interactive():
if False:
dummy()
def _handle_message(payload):
send_message(
config["enable_queue"],
config["interactive_queue"],
payload,
interactive_handler,
)
return handle_slack_request(_handle_message)
@app.route(
"/command", methods=["POST"], content_types=["application/x-www-form-urlencoded"]
)
def command():
def _handle_message(payload):
action_instance = Action.create(payload, config)
if action_instance.is_valid():
send_message(
config["enable_queue"],
config["command_queue"],
payload,
command_handler,
)
return handle_slack_request(_handle_message)
def handle_slack_request(action):
payload = None
try:
req = app.current_request.raw_body.decode()
req_headers = app.current_request.headers
if not verify_token(req_headers, req, config["signing_secret"]):
return "Slack signing secret not valid"
payload = slack_payload_extractor(req)
logger.info(f"Slack extracted payload for command: {payload}")
action(payload)
except Exception:
logger.critical("Caught unhandled exception.", exc_info=True)
if isinstance(payload, dict) and "response_url" in payload:
slack_responder(
payload["response_url"], "Failed to handle request, try again!"
)
return ""
@app.on_sqs_message(queue=config["command_queue"])
def command_handler(event):
for record in event:
action = Action.create(json.loads(record.body), config)
action.perform_action()
@app.on_sqs_message(queue=config["interactive_queue"])
def interactive_handler(event):
for record in event:
action = Action.create(json.loads(record.body), config)
action.perform_interactive()
@app.schedule("rate(1 day)")
def check_user_locks(event):
if config["enable_lock_reminder"]:
remind_users(
slack=Slack(slack_token=config["bot_access_token"]),
backend_url=config["backend_url"],
)