在Alexa技能上更新Item DynamoDB表时出现问题

时间:2019-10-13 08:06:00

标签: amazon-dynamodb alexa alexa-skill

我正在尝试更新表上的项目,该技能只有一个意图,应该根据条件将项目“状态”更新为“开”或“关”。

const Alexa = require('ask-sdk');
var AWS = require("aws-sdk");
const dynamoDBTableName = "automation-test";
AWS.config.update({region: "ap-southeast-2"});
const tableName = "automation-test";
var docClient = new AWS.DynamoDB.DocumentClient();


const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Welcome';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};


const LivingRoomLightsIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'LivingRoomLightsIntent';
    },
    handle(handlerInput) {

        const LightStatus = handlerInput.requestEnvelope.request.intent.slots.LightStatus.resolutions.resolutionsPerAuthority[0].values[0].value.name;
        const deviceID = "livingRoomLights"

        if (LightStatus === "On") {

            const status = "true"
            const params = {
                TableName: tableName,
                Key: {
                  'deviceID' : deviceID,
                },
                UpdateExpression: 'set status = :s', 
                ExpressionAttributeValues: {
                  ':s' : status
                }
            };

        docClient.update(params, function(err, data) {
            if (err) {
              console.log("Error", err);
            } else {
              console.log("Success", data);
            }
          });

        const speakOutput = `The living room light has been set to ${LightStatus}`;
        return handlerInput.responseBuilder
          .speak(speakOutput)
          .reprompt()
          .getResponse();

        }




         else if (LightStatus === "Off") {

            const status = "false"
            const params = {
                TableName: tableName,
                Key: {
                  'deviceID' : deviceID,
                },
                UpdateExpression: 'set status = :s', 
                ExpressionAttributeValues: {
                  ':s' : status
                }
            };

        docClient.update(params, function(err, data) {
            if (err) {
              console.log("Error", err);
            } else {
              console.log("Success", data);
            }
          });

        const speakOutput = `The living room light has been set to ${LightStatus}`;
        return handlerInput.responseBuilder
          .speak(speakOutput)
          .reprompt()
          .getResponse();
           }
           } 
    };


const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'You can say hello to me! How can I help?';

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.getResponse();
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        LivingRoomLightsIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
        ) 

        .addErrorHandlers(ErrorHandler)
  .withTableName(dynamoDBTableName)
  .withAutoCreateTable(true)
    .lambda();

这是日志

  

无法导入模块“索引”:Errorat   函数。模块._resolveFilename(module.js:547:15)在   Module.require中的Function.Module._load(module.js:474:25)   (module.js:596:17)at需求(internal / module.js:11:18)at   宾语。 (/var/task/index.js:1:77)在Module._compile   (module.js:652:30)在Object.Module._extensions..js   (module.js:663:10)在Module.load(module.js:565:32)在tryModuleLoad   (module.js:505:12)在Function.Module._load(module.js:497:3)

请帮助!

1 个答案:

答案 0 :(得分:0)

DynamoDb使用2个唯一ID来检测特定行。 确保正确发送信息。然后将仅检测到您的行并更新值。