forked from nehakale8/slackpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
221 lines (171 loc) · 5.87 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
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
from commands.taskdone import TaskDone
from commands.leaderboard import Leaderboard
from flask import Flask, make_response, request, jsonify, Response
import json
from commands.help import Help
from models import db
from slack import WebClient
from slackeventsapi import SlackEventAdapter
from commands.viewpoints import ViewPoints
from configuration.env_config import Config
from commands.createtask import CreateTask
from helpers.errorhelper import ErrorHelper
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = Config.SQLALCHEMY_DATABASE_URI
db.init_app(app)
# instantiating slack client
slack_client = WebClient(Config.SLACK_BOT_TOKEN)
slack_events_adapter = SlackEventAdapter(
Config.SLACK_SIGNING_SECRET, "/slack/events", app
)
@app.route("/slack/interactive-endpoint", methods=["POST"])
def interactive_endpoint():
"""
All interactive events like button click, input fields are received in this endpoint. We use this endpoint to handle the click event of 'Add task' button of create-task command.
:param:
:type:
:raise:
:return: Response object with 200 HTTP status
:rtype: Response
"""
payload = json.loads(request.form.get("payload"))
if payload["type"] == "block_actions":
actions = payload["actions"]
if len(actions) > 0:
if actions[0]["action_id"] == "create_action_button":
# Create Task - button was clicked
channel_id = payload["container"]["channel_id"]
user_id = payload["user"]["id"]
helper = ErrorHelper()
ct = CreateTask()
state_values = payload["state"]["values"]
desc = None
deadline = None
points = None
for _, val in state_values.items():
if "create_action_description" in val:
desc = val["create_action_description"]["value"]
elif "create_action_deadline" in val:
deadline = val["create_action_deadline"]["selected_date"]
elif "create_action_points" in val:
if val["create_action_points"]["selected_option"] is not None:
points = val["create_action_points"]["selected_option"][
"value"
]
else:
points = None
if desc is None or deadline is None or points is None:
error_blocks = helper.get_error_payload_blocks("createtask")
slack_client.chat_postEphemeral(
channel=channel_id, user=user_id, blocks=error_blocks
)
else:
blocks = ct.create_task(desc=desc, points=points, deadline=deadline)
slack_client.chat_postEphemeral(
channel=channel_id, user=user_id, blocks=blocks
)
return make_response("", 200)
@app.route("/")
def basic():
"""
Health check endpoint
:param:
:type:
:raise:
:return: 'Hello World' - the official health check response text
:rtype: str
"""
return "Hello World"
@app.route("/viewpending", methods=["POST"])
def vpending():
"""
Endpoint to view the pending tasks
:param:
:type:
:raise:
:return: Response object with payload object containing details of pending tasks
:rtype: Response
"""
data = request.form
channel_id = data.get("channel_id")
user_id = data.get("user_id")
text = data.get("text")
vp = ViewPoints(progress=0.0)
payload = vp.get_list()
return jsonify(payload)
@app.route("/viewcompleted", methods=["POST"])
def vcompleted():
"""
Endpoint to view the completed tasks
:param:
:type:
:raise:
:return: Response object with payload object containing details of completed tasks
:rtype: Response
"""
data = request.form
channel_id = data.get("channel_id")
user_id = data.get("user_id")
text = data.get("text")
vp = ViewPoints(progress=1.0)
payload = vp.get_list()
return jsonify(payload)
@app.route("/taskdone", methods=["POST"])
def taskdone():
"""
Endpoint to mark a task as completed
:param:
:type:
:raise:
:return: Response object with payload containing task completion message
:rtype: Response
"""
data = request.form
td = TaskDone(data)
payload = td.update_points()
return jsonify(payload)
@app.route("/create", methods=["POST"])
def create():
"""
Endpoint to create a new task, this endpoint triggers an ephemeral message for the user to enter task details for creation
:param:
:type:
:raise:
:return: Response object with 200 HTTP status
:rtype: Response
"""
ct = CreateTask()
blocks = ct.create_task_input_blocks()
data = request.form
channel_id = data.get("channel_id")
user_id = data.get("user_id")
slack_client.chat_postEphemeral(channel=channel_id, user=user_id, blocks=blocks)
return Response(), 200
@app.route("/help", methods=["POST"])
def help():
"""
A helper endpoint to view all commands and how to use them
:param:
:type:
:raise:
:return: Response object with payload object containing details of all commands and how to use them
:rtype: Response
"""
h = Help()
payload = h.help_all()
return jsonify(payload)
@app.route("/leaderboard", methods=["POST"])
def leaderboard():
"""
Endpoint to view the leaderboard
:param:
:type:
:raise:
:return: Response object with payload object containing details of champions leading the SlackPoint challenge
:rtype: Response
"""
l = Leaderboard()
payload = l.view_leaderboard()
return jsonify(payload)
if __name__ == "__main__":
app.run(host="localhost", port=8000, debug=True)