/
teamtalk.py
52 lines (45 loc) · 1.43 KB
/
teamtalk.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
from flask import Flask, request, jsonify, abort, make_response
from util import getenv
from client import Client
from commands import handle_command
from callbacks import handle_callback
import json
# Load constants from .env file
VERIFICATION_TOKEN = getenv("VERIFICATION_TOKEN")
BOT_ACC_TOKEN = getenv("BOT_ACC_TOKEN")
PORT = getenv("PORT")
# Pretty print server preamble
print("--------------")
print("Running on port", PORT, "with:")
print("Verification token:", VERIFICATION_TOKEN)
print("Bot User OAuth Access token:", BOT_ACC_TOKEN)
print("--------------")
# Define the app & routes
app = Flask(__name__)
client = Client(BOT_ACC_TOKEN)
# Handles slash commands
@app.route("/commands", methods=["POST"])
def commands():
# print(request.form)
# Validate the token
token = request.form.get("token")
if token != VERIFICATION_TOKEN:
abort(401)
# Delegate command to handlers
return handle_command(client, request)
# Handles dialog submissions, button clicks, etc
@app.route("/callbacks", methods=["POST"])
def callbacks():
payload = request.form.get("payload")
if not payload:
abort(500)
data = json.loads(payload)
print(data)
# Validate token
token = data.get("token")
if token != VERIFICATION_TOKEN:
abort(401)
return handle_callback(client, data)
# Finally, run the damn thing
if __name__ == "__main__":
app.run(host='0.0.0.0', port=PORT, debug=True)