-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
75 lines (61 loc) · 1.97 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
import os
import re
import random
from flask import abort, Flask, jsonify, request
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/', methods=['POST'])
def rollDice():
if not is_request_valid(request):
abort(400)
command = request.form.get('command', None)
parameter_text = request.form.get('text', None)
# https://regex101.com/r/K6q4PR/1/
# not perfect - will allow for 1d66, 2d44, etc
DICE_REGEX = "([\d]+)d(4|6|8|100|10|12|20)+(\+\d)*"
matches = re.search(DICE_REGEX, parameter_text)
if not matches:
return jsonify({
'text': 'Rolld Result: ' + 'INVALID DICE - Try again!',
'attachments': [
{
'color': '#C70005',
'author_name': 'Rolld!',
'image_url': 'https://i.imgur.com/aSRSGkG.gif',
}
]
})
results = []
modifier = 0
numberOfRolls = int(matches.group(1))
dieToUse = int(matches.group(2))
if matches.group(3):
modifier = int(matches.group(3))
for x in range(numberOfRolls):
randNum = random.randint(1,dieToUse)
results.append(randNum)
total = sum(results)
user_name = request.form.get('user_name', None)
if (total > 1):
return jsonify({
'text': ':d20: Results for ' + str(user_name) + ': *' + str(total+modifier) + '*. Breakdown ' + str(results) +' +' + str(modifier) + '',
'response_type': 'in_channel'
})
else:
return jsonify({
'text': 'Rolld Result: ' + str(total+modifier) + '. Breakdown ' + str(results) +' +' + str(modifier),
'response_type': 'in_channel',
'attachments': [
{
'color': '#C70005',
'author_name': 'Rolld!',
'image_url': 'https://i.imgur.com/VCoWeaG.gif',
}
]
})
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
if __name__ == "__main__":
app.run(debug=True)