Kapildev Kumar
Created November 20, 2016

Kollywood Heros

Kollywood Heros is a skill that asks the customers about their admired hero in Kollywood, one of the finest film industry in India.

203
Kollywood Heros

Things used in this project

Story

Read more

Schematics

Kollywood Heros Block diagram

Sketch of how it works

Code

Lambda Function

JSON
'use strict';

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: 'PlainText',
            text: output,
        },
        card: {
            type: 'Simple',
            title: `SessionSpeechlet - ${title}`,
            content: `SessionSpeechlet - ${output}`,
        },
        reprompt: {
            outputSpeech: {
                type: 'PlainText',
                text: repromptText,
            },
        },
        shouldEndSession,
    };
}

function buildResponse(sessionAttributes, speechletResponse) {
    return {
        version: '1.0',
        sessionAttributes,
        response: speechletResponse,
    };
}


// --------------- Functions that control the skill's behavior -----------------------

function getWelcomeResponse(callback) {
    const sessionAttributes = {};
    const cardTitle = 'Welcome';
    const speechOutput = 'Welcome to the Kollywood Hero Skill ' +
        'Please tell me your admired hero by saying, my admired hero is Rajinikanth';

    const repromptText = 'Please tell me your admired hero by saying, ' +
        'my admired hero is Rajinikanth';
    const shouldEndSession = false;

    callback(sessionAttributes,
        buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

function handleSessionEndRequest(callback) {
    const cardTitle = 'Session Ended';
    const speechOutput = 'Thank you for trying the Kollywood hero Skill. Have a nice day!';
    const shouldEndSession = true;

    callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}

function createAdmiredHeroAttributes(AdmiredHero) {
    return {
        AdmiredHero,
    };
}

function setHeroInSession(intent, session, callback) {
    const cardTitle = intent.name;
    const AdmiredHeroSlot = intent.slots.Hero;
    let repromptText = '';
    let sessionAttributes = {};
    const shouldEndSession = false;
    let speechOutput = '';

    if (AdmiredHeroSlot) {
        const AdmiredHero = AdmiredHeroSlot.value;
        sessionAttributes = createAdmiredHeroAttributes(AdmiredHero);
        speechOutput = `I now know your admired hero is ${AdmiredHero}. You can ask me ` +
            "your admired hero by saying, who's my admired hero ? ";
        repromptText = "You can ask me your admired hero by saying, who's my admired hero?";
    } else {
        speechOutput = "I'm not sure who your admired hero is. Please try again.";
        repromptText = "I'm not sure who your admired hero is. You can tell me your " +
            'admired hero by saying, my admired hero is Ranjikanth';
    }

    callback(sessionAttributes,
         buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

function getHeroFromSession(intent, session, callback) {
    let AdmiredHero;
    const repromptText = null;
    const sessionAttributes = {};
    let shouldEndSession = false;
    let speechOutput = '';

 
    if (session.attributes) {
        AdmiredHero = session.attributes.AdmiredHero;
    }

    if (AdmiredHero) {
        speechOutput = `Your admired hero is ${AdmiredHero}.  He is a very good actor in Kollywood'. Goodbye.`;
        
        shouldEndSession = true;
    } else {
        speechOutput = "I'm not sure who your admired hero is, you can say, my admired hero " +
            ' is Ajith';
    }

 callback(sessionAttributes,
         buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));

}

// --------------- Events -----------------------

/**
 * Called when the session starts.
 */
function onSessionStarted(sessionStartedRequest, session) {
    console.log(`onSessionStarted requestId=${sessionStartedRequest.requestId}, sessionId=${session.sessionId}`);
    
}

/**
 * Called when the user launches the skill without specifying what they want.
 */
function onLaunch(launchRequest, session, callback) {
    console.log(`onLaunch requestId=${launchRequest.requestId}, sessionId=${session.sessionId}`);

    // Dispatch to your skill's launch.
    getWelcomeResponse(callback);
}

/**
 * Called when the user specifies an intent for this skill.
 */
function onIntent(intentRequest, session, callback) {
    console.log(`onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}`);

    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;

    // Dispatch to your skill's intent handlers
    if (intentName === 'MyHeroIsIntent') {
        setHeroInSession(intent, session, callback);
    } else if (intentName === 'WhosMyHeroIntent') {
        getHeroFromSession(intent, session, callback);
    } else if (intentName === 'AMAZON.HelpIntent') {
        getWelcomeResponse(callback);
    } else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
        handleSessionEndRequest(callback);
    } else {
        throw new Error('Invalid intent');
    }
}

function onSessionEnded(sessionEndedRequest, session) {
    console.log(`onSessionEnded requestId=${sessionEndedRequest.requestId}, sessionId=${session.sessionId}`);
    // Add cleanup logic here
}


// --------------- Main handler -----------------------

exports.handler = (event, context, callback) => {
    try {
        console.log(`event.session.application.applicationId=${event.session.application.applicationId}`);

        /**
         * Uncomment this if statement and populate with your skill's application ID to
         * prevent someone else from configuring a skill that sends requests to this function.
         */
        /*
        if (event.session.application.applicationId !== 'amzn1.echo-sdk-ams.app.[unique-value-here]') {
             callback('Invalid Application ID');
        }
        */

       if (event.session.new) {
            onSessionStarted({ requestId: event.request.requestId }, event.session);
        }

        if (event.request.type === 'LaunchRequest') {
            onLaunch(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        } 
        else if (event.request.type === 'IntentRequest') {
            onIntent(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        } 
        else if (event.request.type === 'SessionEndedRequest') {
            onSessionEnded(event.request, event.session);
            callback();
        }
    } catch (err) {
        callback(err);
    }
};

Credits

Kapildev Kumar

Kapildev Kumar

3 projects • 8 followers
Always cool. Addicted to Work.

Comments