召回Alexa的dynamodb表

时间:2018-02-27 11:03:54

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

我正在尝试创建一种能够扫描或查询我的dynamodb表的技能,其中包括日期列,filmanme和时间。

这是我到目前为止的代码。



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;

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 === "IntentRequest") {
            let options = {};         


            if (request.intent.name === "cinema") {

                if (request.intent.slots.cimema !== undefined)
                    var sign = request.intent.slots.cinema.value;


                //Check sign is valid
                if (sign === undefined || sign === null) {
                    options.speechText = " sorry, i didn't understant your question. can you say that again?";
                    options.endSession = false;
                    context.succeed(buildResponse(options));
                    return;
                }

                if (request.intent.slots.zodiac !== undefined && !ValidateZodiacSign(sign)) {
                    options.speechText = ` The Zoadiac sign ${sign} is not a valid one. Please tell a valid zodiac sign .`;
                    options.endSession = false;
                    context.succeed(buildResponse(options));
                    return;
                }


                cinema(sign, function (cinema, error) {
                    if (error) {
                        context.fail(error);
                        options.speechText = "There has been a problem with the request.";
                        options.endSession = true;
                        context.succeed(buildResponse(options));
                    } else {
                        options.speechText = todaysFortune;
                        options.speechText += " . Have a nice day ahead . ";
                        options.sign = sign;
                        options.cardText = todaysFortune;
                        options.endSession = true;
                        context.succeed(buildResponse(options));
                    }
                });
            } else if (request.intent.name === "AMAZON.StopIntent" || request.intent.name === "AMAZON.CancelIntent") {
                options.speechText = "ok, good bye.";
                options.endSession = true;
                context.succeed(buildResponse(options));
            }
             else if (request.intent.name === "AMAZON.HelpIntent") {
                options.speechText = "My skill will read your table depending on what is asked. For example, you can ask what about a specific date. Please refer to skill description for all possible utterences.";
                options.repromptText = "What is the data sign you want to know  about today? If you want to exit from my  skill please say stop or cancel."
                options.endSession = false;
                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;
}

function readDynamoItem(params, callback) {
    
    var AWS = require('aws-sdk');
    AWS.config.update({region: AWSregion});
    var dynamodb = new AWS.DynamoDB();
    console.log('reading item from DynamoDB table');

    dynamodb.scan(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else{
            console.log(data); // successful response
            callback(JSON.stringify(data));
        }
    });
    var docClient = new AWS.DynamoDB.DocumentClient();
    //Get item by key
    docClient.get(params, (err, data) => {
        if (err) {
            console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("GetItem succeeded:", JSON.stringify(data, null, 2));

            callback(data.Item.message);  // this particular row has an attribute called message

        }
    });

}

///////////////////////////////////////////////////////////////////////////////
&#13;
&#13;
&#13;

这是我的DBHandlder

&#13;
&#13;
const AWS = require('aws-sdk');
AWS.config.update({
    region: "'us-east-1'"
});

var docClient = new AWS.DynamoDB.DocumentClient();

var table = "Cinema";

var getItems = (Id,callback) => {   
  
    var params = {
        TableName: "cinema",
        Key: {
            "Id": Id
        }
    };

    docClient.get(params, function (err, data) {
        callback(err, data);
    });

};

module.exports = {
    getItems
};
&#13;
&#13;
&#13;

我对此非常陌生,我无法在网上找到很多支持我想要制作的技能或任何类似的技能。

我创建了一个lambda函数,当我配置测试函数以查找指定日期时,它会找到相应的电影,但这不适用于alexa

任何输入都有帮助。

0 个答案:

没有答案