-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
57 lines (43 loc) · 1.32 KB
/
flask_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
import json
from flask import Flask, request
from base_skill.skill import Response, Request
from test_skill.main import TestSkill
app = Flask(__name__)
SKILLS = [TestSkill()]
skill_dict = {skill.name: skill for skill in SKILLS}
sessionStorage = {
skill.name: {} for skill in SKILLS
}
@app.route('/<skill>', methods=['POST'])
def main(skill):
if skill in skill_dict:
req = request.json
return handle_dialog(req, skill_dict[skill])
return '404'
def prepare_res(req):
return {
'session': req['session'],
'version': req['version'],
'response': {
'end_session': False
}
}
def ping(req, res):
return req.text == 'ping'
def handle_dialog(req, skill):
res = Response(prepare_res(req))
req = Request(req)
session = sessionStorage[skill.name]
user_id = req.user_id
if not ping(req, res):
if req.new_session:
session[user_id] = {}
skill.command_handler.hello.execute(req=req, res=res, session=session[user_id])
else:
if user_id not in session:
session[user_id] = {}
skill.command_handler.execute(req=req, res=res, session=session[user_id])
skill.log(req=req, res=res, session=session[user_id])
else:
res.text = 'pong'
return json.dumps(res.res)