Amazon Lex Bot - Replying to Hello

≡放荡痞女 提交于 2019-12-24 00:26:36

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/48834037/amazon-lex-bot-replying-to-hello

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!