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.

Copied to clipboard.

const response = handlerInput.responseBuilder
  .speak('foo')
  .reprompt('bar')
  .withSimpleCard('title', 'cardText')
  .getResponse();

Copied to clipboard.

const response = handlerInput.responseBuilder
  .speak('foo')
  .reprompt('bar')
  .withSimpleCard('title', 'cardText')
  .getResponse();

Image and text helpers

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

Copied to clipboard.

const Alexa = require('ask-sdk-core');

const myImage = new Alexa.ImageHelper()
  .withDescription('FooDescription')
  .addImageInstance('http://BarImageSource')
  .getImage();

Copied to clipboard.

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

Copied to clipboard.

const Alexa = require('ask-sdk-core');

const myTextContent = new Alexa.PlainTextContentHelper()
  .withPrimaryText('Foo')
  .withSecondaryText('Bar')
  .withTertiaryText('Baz')
  .getTextContent();

Copied to clipboard.

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

Copied to clipboard.

const Alexa = require('ask-sdk-core');

const myTextContent = new Alexa.RichTextContentHelper()
  .withPrimaryText('Foo')
  .withSecondaryText('Bar')
  .withTertiaryText('Baz')
  .getTextContent();

Copied to clipboard.

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();

Was this page helpful?

Last updated: Nov 28, 2023