Search code examples
node.jsamazon-web-servicesaws-lambdaamazon-lex

Amazon Lex Bot - Replying to Hello


I have started learning Amazon lex, gone through their Documentation and Example Bots.

Problem i am facing is that all the bots are Q&A types, if i have to make the bot reply to Hello, what should be the correct way or how to do it?

According to my understanding :

I am thinking of creating an intent for Hello and when it gets fulfilled i can make the bot reply How can i help you? with Lambda Function, this is the way it is supposed to be done?

There can be many other direct question that user can ask, do i have to reply all the question for an intent with lambda function? I am using java script.

I am stuck, suggest any method ?

EDIT 1 : How to give response based on user response in Amazon Lex?

This is what i was looking for, still any suggestion will be helpful.


Solution

  • To implement returning a formatted response from JavaScript (Node.js) in a Lambda Function:

    First create some handy functions for building proper Lex response formats.

    function close(sessionAttributes, fulfillmentState, message) {
        return {
            sessionAttributes,
            dialogAction: {
                type: 'Close',
                fulfillmentState,
                message,
            },
        };
    }
    

    You can find more Node response-building functions like that in AWS-Lex-Convo-Bot-Example index.js

    Then just call that function and pass it what it needs, like this:

    var message = {
        'contentType': 'PlainText', 
        'content': 'Hi! How can I help you?'
    }
    
    var responseMsg = close( sessionAttributes, 'Fulfilled', message );
    

    (write your message inside 'content', if using SSML tags, change 'contentType' to 'SSML')

    Then pass responseMsg to the callback of exports.handler.


    Put it all together and you get:

    function close(sessionAttributes, fulfillmentState, message) {
        return {
            sessionAttributes,
            dialogAction: {
                type: 'Close',
                fulfillmentState,
                message,
            },
        };
    }
    
    exports.handler = (event, context, callback) => {
        console.log( "EVENT= "+JSON.stringify(event) );
    
        const intentName = event.currentIntent.name;
        var sessionAttributes = event.sessionAttributes;
    
        var responseMsg = "";
    
        if (intentName == "HelloIntent") { // change 'HelloIntent' to your intent's name
            var message = {
                'contentType': 'PlainText', 
                'content': 'Hi! How can I help you?'
            }
    
            responseMsg = close( sessionAttributes, 'Fulfilled', message );
        }
        // else if (intentName == "Intent2") { 
        //      build another response for this intent
        // }
        else {
            console.log( "ERROR unhandled intent named= "+intentName );
            responseMsg = close( sessionAttributes, 'Fulfilled', {"contentType":"PlainText", "content":"Sorry, I can't help with that yet."});
        }
    
        console.log( "RESPONSE= "+JSON.stringify(responseMsg) );
        callback(null, responseMsg);
    }