-
Notifications
You must be signed in to change notification settings - Fork 4
/
dialog.py
67 lines (54 loc) · 2.5 KB
/
dialog.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
from __future__ import print_function
import json
from collections import defaultdict
from lib.dialog_utils import Request
import pkgutil
import voice_handlers
import inspect
from config.config import INTENT_SCHEMA, NON_INTENT_REQUESTS
def initialize_handlers():
"""
Automatically populate function handlers from the handlers
"""
# If no handler is specified, backoff to default handler
init_default_handler = lambda : voice_handlers.default_handler
all_handlers_map = defaultdict(init_default_handler)
intent_handlers_map = defaultdict(init_default_handler)
# Load intent schema to verify that handlers are mapped to valid intents
all_intents = {intent["intent"] : { slot["name"] : slot["type"] for slot in intent['slots'] }
for intent in INTENT_SCHEMA['intents'] }
#Loaded functions in the handlers module
member_functions = inspect.getmembers(voice_handlers, inspect.isfunction)
for (name, function) in member_functions:
if hasattr(function, 'voice_handler'): #Function has been decorated as a voice_handler
if 'request_type' in function.voice_handler:
if function.voice_handler['request_type'] in NON_INTENT_REQUESTS:
# Function is a valid request voice handler
all_handlers_map[function.voice_handler['request_type']] = function
elif 'intent' in function.voice_handler:
if function.voice_handler['intent'] in all_intents:
# Function is a valid intent voice handler
intent_handlers_map[function.voice_handler['intent']] = function
all_handlers_map['IntentRequest'] = intent_handlers_map
return all_handlers_map
"""
The REGISTERED_HANDLERS global variable contains a python dict
which contains mappings from the intent name to the handler
e.g.
handler = REGISTERED_HANDLERS["IntentRequest"][INTENT_NAME]
gives you the appropriate handler.
"""
REGISTERED_HANDLERS = initialize_handlers()
def route_intent(request_json):
"""
This code routes requests to the appropriate handler
"""
request = Request(request_json)
# hack for twitter app
if not request.access_token():
return voice_handlers.default_handler(request)
# end hack for twitter
voice_handler = REGISTERED_HANDLERS[request.request_type()]
if request.intent_name():
voice_handler = voice_handler[request.intent_name()]
return voice_handler(request)