首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS实现AWS

AWS实现AWS
EN

Stack Overflow用户
提问于 2018-06-08 22:51:22
回答 2查看 1.8K关注 0票数 0

我必须将Amazon和Amazon集成在一起。我遇到了一个问题。我是新来的,所以请帮帮我。我想要一个使用Lex的产品。“在哪里可以找到肉类”,肉类将被存储到插槽'SearchProduct‘,然后它将在数据库中搜索并通过lex回复。比如“我在4号通道找到了肉”

在这里,我可以通过在dynamodb中进行扫描来获得通道no 4的值,但是我无法发送响应。

代码语言:javascript
复制
 '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);
    }
};

我正在从搜索中得到答复,但我无法将答复发送到Lex。内容部分始终为空。

代码语言:javascript
复制
Response:
{
  "sessionAttributes": {},
  "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": " "
    }
  }
}

莱克斯将发送名为“SearchProduct‘==’肉类”的插槽。

我不知道我在哪部分做错了。如果有人能帮我改进代码,我会很感激的。谢谢

EN

回答 2

Stack Overflow用户

发布于 2018-06-18 11:42:38

此错误是因为Amazon要求以某种JSON格式进行响应。从您用Node.js编写的代码中可以看出。我不是节点专家,但我可以为您提供一个工作示例,说明我如何将响应发送回lex。

代码流如下所示:

意图称为-> Lambda函数调用-> (您的lamba代码运行并处理Lex给出的数据) ->生成一个响应发送回Lex ->,->读取响应json并根据返回的内容解释它。

代码语言:javascript
复制
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函数为lex创建一个“关闭”类型的fullfilment事件,并生成适当的响应消息。注意:类型、fulfullmentState和message是需要传递回lex的强制参数。

这个链接有助于深入理解它:Lex Docs

另外,查看这里的LEX和Node文档,我可以看到调用调度函数的方法是不同的。但我在这里可能完全不对。

票数 0
EN

Stack Overflow用户

发布于 2019-04-16 11:52:14

您必须以特定格式发送响应。对于Node.js,下面的示例是

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

如果这个答案对你有帮助,你能接受吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50769311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档