【问题标题】:Read data from DynamoDB and recall with Alexa从 DynamoDB 读取数据并使用 Alexa 调用
【发布时间】:2018-02-20 10:08:17
【问题描述】:

我正在尝试使用 Alexa 创建一项技能,以使用扫描或查询功能(或两者)从我的 DynamoDB 表中读取数据。

我表中的列是日期、时间和电影名称。

我是新手,但我已经设法将我的 Lambda 函数链接到 Alexa。我还创建了一个单独的 Lambda 函数,它会在我配置测试事件时从我的表中调用数据,因此当我输入特定日期时,它会调用相应的电影和时间。但是现在我想在 Alexa 中实现它,但不确定如何。

这是我当前的代码

console.log('Loading function');

var AWSregion = 'us-east-1';  // us-east-1
var AWS = require('aws-sdk');
var dclient = new AWS.DynamoDB.DocumentClient();

var getItems = (event, context, callback)=>{
    
    dclient.get(event.params,(error,data)=>{
        if(error){
            callback(null,"error occurerd");
        }
        else{
            callback(null,data);
        }
    });
};

exports.handler = getItems;

我必须在 Alexa 中启动该技能的代码是

exports.handler = (event, context, callback) => {
    try {

        var request = event.request;

        if (request.type === "LaunchRequest") {
            context.succeed(buildResponse({
                speechText: "Welcome to H.S.S.M.I skill, what would you like to find",
                repromptText: "I repeat, Welcome to my skill, what would you like to find",
                endSession: false
            }));
        }


        else if (request.type === "SessionEndedRequest") {
            options.endSession = true;
            context.succeed();
        }
        else {
            context.fail("Unknown Intent type");
        }
        
        



    } catch (e) {

    }


};

function buildResponse(options) {
    var response = {
        version: "1.0",
        response: {
            outputSpeech: {
                "type": "SSML",
                "ssml": `<speak><prosody rate="slow">${options.speechText}</prosody></speak>`
            },

            shouldEndSession: options.endSession
        }
    };

    if (options.repromptText) {
        response.response.reprompt = {
            outputSpeech: {
                "type": "SSML",
                "ssml": `<speak><prosody rate="slow">${options.repromptText}</prosody></speak>`
            }
        };
    }

    return response;
}

只要我能回忆起数据,我不介意将我的表从 DynamoDB 更改为其他表。

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-dynamodb alexa alexa-skills-kit


    【解决方案1】:

    请尝试以下代码,

    var DBHandler = require("./DBHandler")
    
    var DBHandler = require("./DBHandler")
    exports.handler = (event, context, callback) => {
        try {
            
            var request = event.request;
    
            if (request.type === "LaunchRequest") {
                context.succeed(buildResponse({
                    speechText: "Welcome to home remedy. Please tell me what is the problem you want remedy for?",
                    repromptText: "You can say for example, home remedy for headache",
                    endSession: false
                }));
            }
            else if (request.type === "IntentRequest") {
                let options = {};
    
                if (request.intent.name === "Cinema") {
    
    
    
                    DBHandler.getItems(ID, function (err, data) {
                        if (err) {
                            context.fail(err);
    
                        } else {
    
                            if (data.Item !== undefined) {
    
    
    
                                options.speechText = data.Item.YOURFIELDHERE
                                options.endSession = true;
                                context.succeed(buildResponse(options));
    
    
                            } else {
                                options.speechText = `I am Sorry, I couldn't find any data! `
                                options.endSession = true;
                                context.succeed(buildResponse(options));
                            }
                        }
    
                        callback(null, data)
                    });
    
    
    
                } else if (request.intent.name === "AMAZON.StopIntent" || request.intent.name === "AMAZON.CancelIntent") {
                    options.speechText = "ok, thanks for using my skill.";
                    options.endSession = true;
                    context.succeed(buildResponse(options));
                }
    
                else {
                    context.fail("Unknown Intent")
                }
            }
    
            else if (request.type === "SessionEndedRequest") {
                options.endSession = true;
                context.succeed();
            }
            else {
                context.fail("Unknown Intent type")
            }
    
    
    
        } catch (e) {
    
        }
    
    
    };
    
    
    function buildResponse(options) {
        var response = {
            version: "1.0",
            response: {
                outputSpeech: {
                    "type": "SSML",
                    "ssml": `<speak><prosody rate="slow">${options.speechText}</prosody></speak>`
                },
    
                shouldEndSession: options.endSession
            }
        };
    
        if (options.repromptText) {
            response.response.reprompt = {
                outputSpeech: {
                    "type": "SSML",
                    "ssml": `<speak><prosody rate="slow">${options.repromptText}</prosody></speak>`
                }
            };
        }
        
        return response;
    }

    DBHandler 会是,

    const AWS = require('aws-sdk');
    AWS.config.update({
        region: "Location"
    });
    
    var docClient = new AWS.DynamoDB.DocumentClient()
    
    var table = "TableName";
    
    var getItems = (Id,callback) => {   
      
        var params = {
            TableName: table,
            Key: {
                "Id": Id
            }
        };
    
        docClient.get(params, function (err, data) {
            callback(err, data);
        });
    
    };
    
    module.exports = {
        getItems
    };

    【讨论】:

    • 您是否将 DBHandler.js 添加为单独的 js 文件?
    • 对不起,我在你回复之前就这样做了,在测试我的 lambda 函数时它可以工作,我可以在 alexa 上打开技能。我现在需要重做我的意图模式,以便我可以从我的表中找到数据吗?
    • 您好,很抱歉再次打扰您,您知道有哪些教程可以帮助我提高技能吗?
    • 你准备好花 11.99 英镑了吗?那就买这个课程udemy.com/comprehensive-alexa-skill-development-course/learn/v4/…
    • 是的,谢谢,我试试看我去哪里
    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多