-
Notifications
You must be signed in to change notification settings - Fork 0
/
kangaroo_court.py
402 lines (365 loc) · 11.7 KB
/
kangaroo_court.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
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
import os
from time import sleep
import requests
from flask import abort, Flask, jsonify, request, make_response, Response
import json
from slack import WebClient
import boto3
from datetime import datetime
import pandas as pd
app = Flask(__name__)
client = WebClient(token=os.environ['SLACK_BOT_TOKEN'])
def is_request_valid(request):
is_token_valid = request.form['token'] == os.environ['SLACK_VERIFICATION_TOKEN']
is_team_id_valid = request.form['team_id'] == os.environ['SLACK_TEAM_ID']
return is_token_valid and is_team_id_valid
@app.route('/kangaroo_court', methods=['POST'])
def kangaroo_court():
if not is_request_valid(request):
abort(400)
trigger_id = request.form['trigger_id']
#callback_id = request.form['callback_id']
view = {
"type": "modal",
"callback_id": "modal-identifier",
"title": {
"type": "plain_text",
"text": "Kangaroo Court",
"emoji": True
},
"submit": {
"type": "plain_text",
"text": "Submit",
"emoji": True
},
"close": {
"type": "plain_text",
"text": "Cancel",
"emoji": True
},
"blocks": [
{
"type": "input",
"block_id": "defendant",
"element": {
"type": "multi_users_select",
"placeholder": {
"type": "plain_text",
"text": "Defendant",
"emoji": True
}
},
"label": {
"type": "plain_text",
"text": "Defendant",
"emoji": True
}
},
{
"type": "input",
"block_id": "anonymous",
"element": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Yes or No",
"emoji": True
},
"options": [
{
"text": {
"type": "plain_text",
"text": "Yes",
"emoji": True
},
"value": "yes"
},
{
"text": {
"type": "plain_text",
"text": "No",
"emoji": True
},
"value": "no"
}
]
},
"label": {
"type": "plain_text",
"text": "Want to be anonymous?",
"emoji": True
}
},
{
"type": "input",
"block_id": "credOrFine",
"element": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Credit or Fine",
"emoji": True
},
"options": [
{
"text": {
"type": "plain_text",
"text": "Credit",
"emoji": True
},
"value": "credit"
},
{
"text": {
"type": "plain_text",
"text": "Fine",
"emoji": True
},
"value": "fine"
}
]
},
"label": {
"type": "plain_text",
"text": "Credit or Fine",
"emoji": True
}
},
{
"type": "input",
"block_id": "fineAmount",
"element": {
"type": "plain_text_input",
"placeholder": {
"type": "plain_text",
"text": "Amount in dollars ($)",
"emoji": True
}
},
"label": {
"type": "plain_text",
"text": "Amount",
"emoji": True
}
},
{
"type": "input",
"block_id": "act",
"element": {
"type": "plain_text_input",
"multiline": True,
"placeholder": {
"type": "plain_text",
"text": "What did the defendants do?",
"emoji": True
}
},
"label": {
"type": "plain_text",
"text": "Act",
"emoji": True
}
}
]
}
open_view = client.views_open(trigger_id = trigger_id,
view=view)
#view_id = open_dialog['view']['id']
return make_response("", 200)
@app.route('/submit', methods=['POST'])
def submit():
#client.views_update(trigger_id= )
#
payload = json.loads(request.form["payload"])
print(payload)
user = payload['user']['id']
plaintiff = payload['user']['id']
blocks = payload['view']['blocks']
view_state = payload['view']['state']['values']
dynamodb = boto3.resource('dynamodb',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name='us-east-2',
use_ssl=False)
DBclient = boto3.client('dynamodb')
tableKangarooCourt = dynamodb.Table('kangaroo_court')
response = tableKangarooCourt.scan()
currentCnt = int(response['Count'])
if currentCnt == 0:
newCnt = 1
else:
newCnt = currentCnt + 1
eventDateTime = (datetime.now()).strftime("%Y%m%d")
#print(blocks)
#print(view_state)
for block in blocks:
if block['type'] == 'input':
block_id = block['block_id']
action_id = block['element']['action_id']
if block['block_id'] == 'defendant':
selectedUsers = view_state[block_id][action_id]['selected_users']
selUserString = ''
for i in range(len(selectedUsers)):
if i+1 == len(selectedUsers):
selUserString += f'<@{selectedUsers[i]}>'
else:
selUserString += f'<@{selectedUsers[i]}>, '
elif block['block_id'] == 'credOrFine':
credOrFine = view_state[block_id][action_id]['selected_option']['text']['text']
elif block['block_id'] == 'fineAmount':
fineAmount = view_state[block_id][action_id]['value']
fineAmount = ''.join(filter(str.isdigit, fineAmount))
elif block['block_id'] == 'act':
act = view_state[block_id][action_id]['value']
elif block['block_id'] == 'anonymous':
anonymous = view_state[block_id][action_id]['selected_option']['value']
if anonymous == 'yes':
plaintiff = 'Anonymous'
elif anonymous == 'no':
continue
tableKangarooCourt.put_item(
Item={
'courtCaseCnt': int(newCnt),
'courtSubmitDate': eventDateTime,
'plaintiff': plaintiff,
'defendant': selectedUsers,
'credOrFine': credOrFine,
'fineAmount': fineAmount,
'act': act
})
client.chat_postEphemeral(user=user,
channel='#kangaroo_court',
text=f'*Plaintiff*:<@{plaintiff}>\n *Defendant*: {selUserString}\n *Credit or Fine*: {credOrFine}\n *Amount*: {fineAmount}\n *Act*: {act}')
#if not is_request_valid(payload):
# abort(400)
#if payload["type"] == "view_submission":
# print(payload["state"])
#client.chat_postMessage(channel='#testbot',
# text=f'{}')
return make_response("",200)
@app.route('/judgement_day', methods=['POST'])
def judgement_day():
userID = request.form['user_id']
if userID not in ['UDZ1W9XP1', 'UG5DHURTP']:
client.chat_postEphemeral(user=userID,
channel='#kangaroo_court',
text="You're not the judge, you sneaky fox!")
return make_response("", 200)
else:
# begin judging
blocks = [
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Cases brought to your honor, judge Sonabend*"
},
},
{
"type": "divider"
}
]
dynamodb = boto3.resource('dynamodb',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name='us-east-2',
use_ssl=False)
DBclient = boto3.client('dynamodb')
tableKangarooCourt = dynamodb.Table('kangaroo_court')
response = tableKangarooCourt.scan()
df = pd.DataFrame(response['Items'])
df = df.sample(frac=1)
print(df)
for idx, r in df.iterrows():
act = r.act
courtCaseNumber = r.courtCaseCnt
courtSubmitDate = r.courtSubmitDate
plaintiff = r.plaintiff
fineAmount = r.fineAmount
credOrFine = r.credOrFine
defendants = r.defendant
defendantString = ''
for i in range(len(defendants)):
if i+1 == len(defendants):
defendantString += f'<@{defendants[i]}>'
else:
defendantString += f'<@{defendants[i]}>, '
credOrFine = r.credOrFine
blocks.append({
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": f"Plaintiff: <@{plaintiff}>"
},
{
"type": "mrkdwn",
"text": "|"
},
{
"type": "mrkdwn",
"text": f"Defendant: {defendantString}"
},
{
"type": "mrkdwn",
"text": "|"
},
{
"type": "mrkdwn",
"text": f"{credOrFine}"
},
{
"type": "mrkdwn",
"text": "|"
},
{
"type": "mrkdwn",
"text": f"Amount: {fineAmount}"
}
]
})
blocks.append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*{act}*"
}
})
blocks.append({
"type": "divider"
})
client.chat_postEphemeral(user=userID,
channel='#kangaroo_court',
blocks=blocks)
return make_response("", 200)
@app.route('/mycomplaints', methods=['POST'])
def my_complaints():
userID = request.form['user_id']
dynamodb = boto3.resource('dynamodb',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name='us-east-2',
use_ssl=False)
DBclient = boto3.client('dynamodb')
tableKangarooCourt = dynamodb.Table('kangaroo_court')
response = tableKangarooCourt.scan()
df = pd.DataFrame(response['Items'])
df = df[df['plaintiff'] == userID]
for idx, r in df.iterrows():
selUserString = ''
defendants = list(r.defendant)
for i in range(len(defendants)):
if i+1 == len(defendants):
selUserString += f'<@{defendants[i]}>'
else:
selUserString += f'<@{defendants[i]}>, '
client.chat_postEphemeral(user=userID,
channel='#kangaroo_court',
text=f'*Plaintiff*:<@{r.plaintiff}>\n *Defendant*: {selUserString}\n *Credit or Fine*: {r.credOrFine}\n *Amount*: {r.fineAmount}\n *Act*: {r.act}')
print(request.get_data())
return make_response("", 200)
if __name__ == "__main__":
app.run()