Alexa + AWS Lambda Hello World in Javascript

Here is the Javascript version of my earlier tutorial Hello World in AWS Lambda + Amazon Alexa:

'use strict';

function buildResponsePayload( title, output, repromptText, shouldEndSession ) {
	return {
		outputSpeech: {
			type: 'PlainText',
			text: output
		},
		card: {
			type: 'Simple',
			title: 'alexaHelloWorld - ' + title,
			content: 'alexaHelloWorld - ' + output
		},
		reprompt: {
			outputSpeech: {
				type: 'PlainText',
				text: repromptText,
			},
		},
		shouldEndSession,
	};
}

function buildResponse( sessionAtts, speechResponse ) {
	return {
		version: '1.0',
		sessionAtts,
		response: speechResponse,
	};
}

function sayHelloWorld( callback ) {
	const sessionAtts = {};
	const cardTitle = 'alexaHelloWorld';
	const textOutput = 'Hello Javascript World';
	const shouldEndSession = false;

	callback( sessionAtts, buildResponsePayload( cardTitle, textOutput, textOutput, shouldEndSession ) );
}

exports.handler = ( event, context, callback ) => {
	try {
		sayHelloWorld( ( sessionAtts, speechResponse ) => {
			callback( null, buildResponse( sessionAtts, speechResponse ) );
		} );
	} catch( e ) {
		callback( e );
	}
};

Enjoy!

Leave a Reply