Building Response
Standard response
If you are using the lambda as your skill endpoint, you are only responsible for providing the response body in order for Alexa to respond to a customer request. For more details about the JSON structure of the response body, see Response Format.
A response body contains the following properties:
- version
- sessionAttributes
- response
ASK SDK v2 for Node.js helps filling the version and sessionAttributes so you can focus on building the response instead of writing boilerplate code.
ResponseBuilder
The ResponseBuilder
includes helper methods for constructing the response. A Response
may contain multiple elements, and the helper methods aid in generating responses, reducing the need to initialize and set the elements of each response. ResponseBuilder
is available to handlers via the HandlerInput
container object. The detailed description of ResponseBuilder
can be found in the TypeDoc.
Available methods
speak(speechOutput: string, playBehavior? : ui.PlayBehavior): this;
reprompt(repromptSpeechOutput: string, playBehavior? : ui.PlayBehavior): this;
withSimpleCard(cardTitle: string, cardContent: string): this;
withStandardCard(cardTitle: string, cardContent: string, smallImageUrl?: string, largeImageUrl?: string): this;
withLinkAccountCard(): this;
withAskForPermissionsConsentCard(permissionArray: string[]): this;
withCanFulfillIntent(canFulfillIntent : CanFulfillIntent) : this;
addDelegateDirective(updatedIntent?: Intent): this;
addElicitSlotDirective(slotToElicit: string, updatedIntent?: Intent): this;
addConfirmSlotDirective(slotToConfirm: string, updatedIntent?: Intent): this;
addConfirmIntentDirective(updatedIntent?: Intent): this;
addAudioPlayerPlayDirective(playBehavior: interfaces.audioplayer.PlayBehavior, url: string, token: string, offsetInMilliseconds: number, expectedPreviousToken?: string, audioItemMetadata? : AudioItemMetadata): this;
addAudioPlayerStopDirective(): this;
addAudioPlayerClearQueueDirective(clearBehavior: interfaces.audioplayer.ClearBehavior): this;
addRenderTemplateDirective(template: interfaces.display.Template): this;
addHintDirective(text: string): this;
addVideoAppLaunchDirective(source: string, title?: string, subtitle?: string): this;
withShouldEndSession(val: boolean): this;
addDirective(directive: Directive): this;
addDirectiveToReprompt(directive : Directive) : this;
getResponse(): Response;
The following example shows how to construct a response using
ResponseBuilder
helper methods.
const response = handlerInput.responseBuilder
.speak('foo')
.reprompt('bar')
.withSimpleCard('title', 'cardText')
.getResponse();
const response = handlerInput.responseBuilder
.speak('foo')
.reprompt('bar')
.withSimpleCard('title', 'cardText')
.getResponse();
SsmlUtils
to help you escape the following invalid XML characters:•
&
-> &
•
>
-> >
•
<
-> <
•
'
-> '
•
"
-> "
Image and text helpers
To learn about APL, see Add Visuals and Audio to Your Skill.
The Alexa Design System for APL includes a set of templates that are similar to the display templates, so you can transition your skill. For details about which templates to use, see Replace display templates with responsive templates.
ASK SDK v2 for Node.js provides the following helper classes to help you build text and image elements that are widely used in Echo Show compatible skills.
ImageHelper
const Alexa = require('ask-sdk-core');
const myImage = new Alexa.ImageHelper()
.withDescription('FooDescription')
.addImageInstance('http://BarImageSource')
.getImage();
import { ImageHelper } from 'ask-sdk-core';
import { interfaces } from 'ask-sdk-model';
import Image = interfaces.display.Image;
const myImage : Image = new ImageHelper()
.withDescription('FooDescription')
.addImageInstance('http://BarImageSource')
.getImage();
PlainTextContentHelper
const Alexa = require('ask-sdk-core');
const myTextContent = new Alexa.PlainTextContentHelper()
.withPrimaryText('Foo')
.withSecondaryText('Bar')
.withTertiaryText('Baz')
.getTextContent();
import { PlainTextContentHelper } from 'ask-sdk-core';
import { interfaces } from 'ask-sdk-model';
import TextContent = interfaces.display.TextContent;
const myTextContent : TextContent = new PlainTextContentHelper()
.withPrimaryText('Foo')
.withSecondaryText('Bar')
.withTertiaryText('Baz')
.getTextContent();
RichTextContentHelper
const Alexa = require('ask-sdk-core');
const myTextContent = new Alexa.RichTextContentHelper()
.withPrimaryText('Foo')
.withSecondaryText('Bar')
.withTertiaryText('Baz')
.getTextContent();
import { RichTextContentHelper } from 'ask-sdk-core';
import { interfaces } from 'ask-sdk-model';
import TextContent = interfaces.display.TextContent;
const myTextContent : TextContent = new RichTextContentHelper()
.withPrimaryText('Foo')
.withSecondaryText('Bar')
.withTertiaryText('Baz')
.getTextContent();
Last updated: Nov 28, 2023