With the SDK imported, we can begin defining the skill’s logic. Our code will be organized into objects called response handlers, which (i) makes our code easier to maintain and (ii) allows us to define behavior specific to each situation.
We’ll define each request handler, then register them in a specific order. When a request is received by the Lambda function, the SDK will go down the line of handlers, checking if each handler can handle
the request. The SDK ‘picks’ the first handler in line that can handle it. The handler’s main functionality is executed to perform some logic and formulate a response.
As developers we write our request handlers like this:
const myRequestHandler = { canHandle(handlerInput) { }, handle(handlerInput) { } };
For each handler we write a canHandle
method, which returns true
if the handler can handle the request.
We also write a handle
method that contains the request processing logic, taking the request as input and returning a JSON response.
Instructions
Take a look at the diagram. Handlers are ‘asked’ in order: can you handle this request? The first to respond true
will have its handle
method executed.
If the second and third handlers can handle the request, which one’s handle
method will be executed? (See the next exercise for the answer.)