Выполнение AWS-Lex с помощью AWS-Lambda - PullRequest
0 голосов
/ 09 июня 2018

Я должен интегрировать Amazon lex с Amazon лямбда.Я столкнулся с одной проблемой.Я новичок в этом, поэтому, пожалуйста, помогите мне.Я хочу запросить продукт с помощью Lex.« Где я могу найти мясо », и мясо будет сохранено в слоте «SearchProduct». Затем оно выполнит поиск в базе данных и ответит через lex.Например, «Я нашел мясо в проходе № 4»

Здесь я могу получить значение прохода № 4, сканируя в DynamodB, но я не могу отправить ответ.

 'use strict';

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({ region: "us-east-1" });
var reply = ' ';


// --------------- Helpers to build responses which match the structure of the necessary dialog actions -----------------------

function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message, responseCard) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'ElicitSlot',
            intentName,
            slots,
            slotToElicit,
            message,
            responseCard,
        },
    };
}

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

function delegate(sessionAttributes, slots) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'Delegate',
            slots,
        },
    };
}



// ---------------- Helper Functions --------------------------------------------------

// build a message for Lex responses
function buildMessage(messageContent) {
    return {
        contentType: 'PlainText',
        content: messageContent,
    };
}



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

/**
 * Performs dialog management and fulfillment for ordering a beverage.
 * (we only support ordering a mocha for now)
 */
function ItemSearch(intentRequest, callback) {

    const outputSessionAttributes = intentRequest.sessionAttributes;
    const source = intentRequest.invocationSource;

    if (source === 'FulfillmentCodeHook') {
        const slots = intentRequest.currentIntent.slots;
        const requestProductName = (slots.SearchProduct ? slots.SearchProduct : null);

        var scanningParameters = {
            TableName: "my table name",
            ProjectionExpression: "#pro, Aisle",
            FilterExpression: "contains (#pro, :productname)",
            ExpressionAttributeNames: {
                "#pro": "ProductName",
            },
            ExpressionAttributeValues: {
                ":productname": requestProductName
            }
        };

        docClient.scan(scanningParameters, function(err, data) {
            if (err) {

                callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', content: 'not found' }));
            }
            else {
                console.log(data);
                if (data.Count == 0) {
                    reply = 'not found';
                    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', content: 'not found' }));
                }
                else {
                    reply = requestProductName + ' can be found in Aisle No: ' + data.Items[0].Aisle;
                    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', content: requestProductName + ' can be found in Aisle No: ' + data.Items[0].Aisle }));

                }
            }
        });
    }

    callback(close(outputSessionAttributes, 'Fulfilled', {
        contentType: 'PlainText',
        content: `Thanks for using CoffeeBot! `  // i want the reply from the search here but i always end up with null
    }));
}

// --------------- Intents -----------------------

/**
 * Called when the user specifies an intent for this skill.
 */
function dispatch(intentRequest, callback) {

    console.log(`dispatch userId=${intentRequest.userId}, intent=${intentRequest.currentIntent.name}`);

    const name = intentRequest.currentIntent.name;

    // dispatch to the intent handlers
    if (name.startsWith('Product')) {
        return ItemSearch(intentRequest, callback);
    }
    throw new Error(`Intent with name ${name} not supported`);
}

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

// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {

    console.log(JSON.stringify(event));

    try {
        console.log(`event.bot.name=${event.bot.name}`);

        // fail if this function is for a different bot
        if (!event.bot.name.startsWith('Aowi')) {
            callback('Invalid Bot Name');
        }
        dispatch(event, (response) => callback(null, response));
    }
    catch (err) {
        callback(err);
    }
};

Я получаю ответ от поиска, но не могу отправить ответ Лексу.Часть содержимого всегда пуста.

Response:
{
  "sessionAttributes": {},
  "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": " "
    }
  }
}

Lex отправит слот с именем 'SearchProduct' == 'meat'.

enter image description here

Я не уверен, в какой части я делаю это неправильно.Цените это, если кто-нибудь может помочь мне улучшить код.Спасибо

Ответы [ 2 ]

0 голосов
/ 16 апреля 2019

Вы должны отправить ответ в определенном формате.Для Node.js ниже приведен пример

 const response = {
          dialogAction: {
                        type: "Close",
                        fulfillmentState: "Fulfilled",
                        message: {
                            contentType: "PlainText",
                            content: "i have found Meat in Aisle no 4"
                        }
                    }
                };
                callback(null, response);

0 голосов
/ 18 июня 2018

Эта ошибка связана с тем, что Amazon Lex ожидает ответы в определенном формате JSON.Судя по всему, вы написали код в Node.js.Я не эксперт по узлам, но могу предоставить вам рабочий пример того, как я могу отправить ответ обратно в lex.

Поток кода выглядит примерно так:

Intent вызывается-> Вызвана лямбда-функция -> (Ваш лямбда-код запускается и обрабатывает данные, предоставленные lex) -> Вы генерируете ответ для отправки обратно в Lex -> Lex читает Response json и интерпретирует его на основе того, что вы вернули.

def close(fulfillment_state, message):
response = {
    'dialogAction': {
        'type': 'Close',
        'fulfillmentState': fulfillment_state,
        'message': message
    }
}
return response
response_to_lex =  close('Fulfilled',{'contentType': 'PlainText','content': 'Message i want to send to lex'})

Функция close создает событие fullfilment типа «Закрыть» для lex и генерирует соответствующее ответное сообщение.Примечание: type, executeullmentState и message являются обязательными параметрами, которые необходимо передать обратно в lex.

Эта ссылка может быть полезна для подробного понимания: Lex Docs

Также, глядя на документацию для LEX и Node Lambda по адресу здесь я могу видеть, что метод для вызова функции диспетчеризации отличается.Но я мог бы быть совершенно неверным здесь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...