-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalice_sdk.py
65 lines (49 loc) · 1.48 KB
/
alice_sdk.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
import json
class AliceRequest(object):
def __init__(self, request_dict):
self._request_dict = request_dict
@property
def version(self):
return self._request_dict['version']
@property
def session(self):
return self._request_dict['session']
@property
def user_id(self):
return self.session['user_id']
@property
def is_new_session(self):
return bool(self.session['new'])
@property
def command(self):
return self._request_dict['request']['command']
@property
def payload(self):
return self._request_dict['request']['payload']
def __str__(self):
return str(self._request_dict)
class AliceResponse(object):
def __init__(self, alice_request):
self._response_dict = {
"version": alice_request.version,
"session": alice_request.session,
"response": {
"end_session": False
}
}
self.is_end = False
def dumps(self):
return json.dumps(
self._response_dict,
ensure_ascii=False,
indent=2
)
def set_text(self, text):
self._response_dict['response']['text'] = text[:1024]
def set_buttons(self, buttons):
self._response_dict['response']['buttons'] = buttons
def end(self):
self._response_dict["response"]["end_session"] = True
self.is_end = True
def __str__(self):
return self.dumps()