Basic Lambda Function (VSK FTV)

In Step 1: Create Your Video Skill and Lambda Function, you integrated some basic code as a sample Lambda function to get started. For convenience and easy reference, this function is also added below.

Copied to clipboard.

/**
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License"). You may not use this file except in # compliance with the License.
* A copy of the License is located at http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, express or implied. See the License for the specific # language governing permissions and limitations under the License.
**/

// This file includes sample lambda code for a Video Skills API integration on Fire TV,
// including responses for Discovery, SearchAndPlay, FastForward, Channel Change, and other
// directive types. Partners should use this code as a starting point, but then add their
// own logic. Partners should test and validate any code before using it in production.
// Amazon's recommended approach is to take directives received by the lambda and send
// them directly to the app via ADM. Any further processing can occur within the app.
// NOTE: Responses back to Alexa should NOT be modified, only code to process/send directives.
// NOTE: You should send directives from the lambda to your app, but you should NOT try
// to send messages from your app back to the lambda. Success responses from the lambda
// are only meant to indicate that the message was received by the lambda, not the app.

var AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    console.log("Lambda was invoked by Alexa");
    console.log("Event: " + JSON.stringify(event, null, 2));
    console.log("Context: " + context);
    let directive = event.directive;
    if (directive)
    {
        console.log("Directive received from Alexa: " + directive);
        if (directive.header.name == "Discover")
        {
            console.log(directive.header.name, " directive received from Alexa");
            let resp = {
               event: {
                   header : {
                       messageId : directive.header.messageId,
                       name : "Discover.Response",
                       namespace: "Alexa.Discovery",
                       payloadVersion: "3"
                   },
                   "payload": {
                       "endpoints": [
                           {
                               "capabilities": [
                                   {
                                       "interface": "Alexa.RemoteVideoPlayer",
                                       "type": "AlexaInterface",
                                       "version": "1.0"
                                   },
                                   {
                                       "interface": "Alexa.PlaybackController",
                                       "type": "AlexaInterface",
                                       "version": "1.0"
                                   },
                                   {
                                       "interface": "Alexa.SeekController",
                                       "type": "AlexaInterface",
                                       "version": "1.0"
                                   },
                                   {
                                       "interface": "Alexa.ChannelController",
                                       "type": "AlexaInterface",
                                       "version": "1.0"
                                   }
                               ],
                               "endpointId": "VSKTV",
                               "description": "VSKTV is the default device used for video skills developed for Fire TV integrations",
                               "friendlyName": "VSKTV",
                               "manufacturerName": "Amazon"
                           }
                       ]
                    }
               }
            };
            console.log("Sending ", directive.header.name, " response back to Alexa: ", JSON.stringify(resp));
            callback(null, resp);
        }
        else if (  directive.header.name == "SearchAndPlay"
				|| directive.header.name == "SearchAndDisplayResults"
				|| directive.header.name == "Play"
				|| directive.header.name == "Resume"
				|| directive.header.name == "Pause"
				|| directive.header.name == "Next"
				|| directive.header.name == "Previous"
				|| directive.header.name == "Rewind"
				|| directive.header.name == "FastForward")
        {
            console.log(directive.header.name, " directive received from Alexa");

			// Partner-specific logic to handle the the directives goes here
			// Code for sending directive from the lambda to the app goes here

            // Below, we send a "success" response back to Alexa, indicating the directive was received by the lambda
            let resp = {
                event: {
                    "header" : {
                        "messageId": "not-required",
                        "correlationToken": "not-required",
                        "name": "Response",
                        "namespace": "Alexa",
                        "payloadVersion": "3"
                    },
                    "endpoint": {
                        "scope": {
                            "type": "DirectedUserId",
                            "directedUserId": "not-required"
                        },
                        "endpointId": "not-required"
                    },
                    "payload": { }
                }
            };
            console.log("Sending ", directive.header.name, " response back to Alexa: ", JSON.stringify(resp));
            callback(null, resp);
        }
        else if (directive.header.name == "AdjustSeekPosition")
        {
            console.log(directive.header.name, " directive received from Alexa");

			// Partner-specific logic to handle the AdjustSeekPosition directive goes here
			// Code for sending this directive from the lambda to the app goes here

            // Below, we send a "success" response back to Alexa, indicating the directive was received by the lambda
            let resp = {
                event: {
                    "header" : {
                        "messageId": "not-required",
                        "correlationToken": "not-required",
                        "name": "StateReport",
                        "namespace": "Alexa.SeekController",
                        "payloadVersion": "3"
                    },
                    "endpoint": {
                        "scope": {
                            "type": "DirectedUserId",
                            "directedUserId": "not-required"
                        },
                        "endpointId": "not-required"
                    },
                    "payload": {
                      "properties": [{
                        "name": "positionMilliseconds",
                        "value": 0
                      }]
                    }
                }
            };
            console.log("Sending ", directive.header.name, " response back to Alexa: ", JSON.stringify(resp));
            callback(null, resp);
        }
		else if (directive.header.name == "ChangeChannel")
        {
            console.log(directive.header.name, " directive received from Alexa");

			// Partner-specific logic to handle the ChangeChannel directive goes here
			// Code for sending this directive from the lambda to the app goes here

            // Below, we send a "success" response back to Alexa, indicating the directive was received by the lambda
            let resp = {
                context: {
                    "properties": [
                    {
                        "namespace": "Alexa.ChannelController",
                        "name": "channel",
                        "value": {
                            "number": "1234",
                            "callSign": "not-required",
                            "affiliateCallSign": "not-required"
                        },
                        "timeOfSample": "2017-02-03T16:20:50.52Z",
                        "uncertaintyInMilliseconds": 0
                    }
                    ]
                },
                event: {
                    "header" : {
                        "messageId": "not-required",
                        "correlationToken": "not-required",
                        "name": "Response",
                        "namespace": "Alexa",
                        "payloadVersion": "3"
                    },
                    "endpoint": {
                        "endpointId": "not-required"
                    },
                    "payload": { }
                }
            };
            console.log("Sending ", directive.header.name, " response back to Alexa: ", JSON.stringify(resp));
            callback(null, resp);
        }
        else if (directive.header.name == "AcceptGrant")
        {
            console.log(directive.header.name, " directive received from Alexa");
            let resp = {
                "event": {
                    "header": {
                        "messageId": "abc-123-def-456",
                        "namespace": "Alexa.Authorization",
                        "name": "AcceptGrant.Response",
                        "payloadVersion": "3"
                        },
                "payload": {}
              }
            };
            console.log("Sending ", directive.header.name, " response back to Alexa: ", JSON.stringify(resp));
            callback(null, resp);

        }
        else
        {
			console.log("Directive is not supported. Ignoring it.");
			callback(null, 'Directive is not supported. Ignoring it.');
        }
    }
    else
    {
		console.log( 'Directive is null. Ignoring it.');
        callback(null, 'Directive is null. Ignoring it.');
    }
};