Alexa 技能,自定义插槽 - 日期和时间

Alexa skill, custom slot - date and time

我创建了一项技能,我希望能够在特定日期和时间从我的发电机数据库调用机器状态 table。

我的第一列是日期,我的排序键是时间。

我是否需要为一年中所有 365 天的日期创建一个自定义时段,还是有更快的方法来做到这一点?我还需要为一天中的每一分钟创建一个自定义时段吗?

代码:

var AWSregion = 'us-east-1';  // us-east-1
var AWS = require('aws-sdk');
var dbClient = new AWS.DynamoDB.DocumentClient();
AWS.config.update({
    region: "'us-east-1'"
});

let GetMachineStateIntent = (context, callback) => {    
  var params = {
    TableName: "updatedincident",
    Key: {
      date: "2018-03-28",
      time: "04:23",
    }
  };
  dbClient.get(params, function (err, data) {
    if (err) {
       // failed to read from table for some reason..
       console.log('failed to load data item:\n' + JSON.stringify(err, null, 2));
       // let skill tell the user that it couldn't find the data 
       sendResponse(context, callback, {
          output: "the data could not be loaded from your database",
          endSession: false
       });
    } else {
       console.log('loaded data item:\n' + JSON.stringify(data.Item, null, 2));
       // assuming the item has an attribute called "incident"..
       sendResponse(context, callback, {
          output: data.Item.incident,
          endSession: false
       });
    }
  });
};


function sendResponse(context, callback, responseOptions) {
  if(typeof callback === 'undefined') {
    context.succeed(buildResponse(responseOptions));
  } else {
    callback(null, buildResponse(responseOptions));
  }
}

function buildResponse(options) {
  var alexaResponse = {
    version: "1.0",
    response: {
      outputSpeech: {
        "type": "SSML",
        "ssml": `<speak><prosody rate="slow">${options.output}</prosody></speak>`
      },
      shouldEndSession: options.endSession
    }
  };
  if (options.repromptText) {
    alexaResponse.response.reprompt = {
      outputSpeech: {
        "type": "SSML",
        "ssml": `<speak><prosody rate="slow">${options.reprompt}</prosody></speak>`
      }
    };
  }
  return alexaResponse;
}

exports.handler = (event, context, callback) => {
  try {
    var request = event.request;
    if (request.type === "LaunchRequest") {
      sendResponse(context, callback, {
        output: "welcome to my skill. what data are you looking for?",
        endSession: false
      });
  }
    else if (request.type === "IntentRequest") {
      let options = {};         
      if (request.intent.name === "GetMachineStateIntent") {
        GetMachineStateIntent(context, callback);
      } else if (request.intent.name === "AMAZON.StopIntent" || request.intent.name === "AMAZON.CancelIntent") {
        sendResponse(context, callback, {
          output: "ok. good bye!",
          endSession: true
        });
      }
      else if (request.intent.name === "AMAZON.HelpIntent") {
        sendResponse(context, callback, {
          output: "you can ask me about incidents that have happened",
          reprompt: "what can I help you with?",
          endSession: false
        });
      }
      else {
        sendResponse(context, callback, {
          output: "I don't know that one! please try again!",
          endSession: false
        });
      }
    }
    else if (request.type === "SessionEndedRequest") {
      sendResponse(context, callback, ""); // no response needed
    }
    else {
      // an unexpected request type received.. just say I don't know..
      sendResponse(context, callback, {
          output: "I don't know that one! please try again!",
          endSession: false
      });
    }
  } catch (e) {
    // handle the error by logging it and sending back an failure
    console.log('Unexpected error occurred in the skill handler!', e);
    if(typeof callback === 'undefined') {
       context.fail("Unexpected error");
    } else {
       callback("Unexpected error");
    }
  }
};

简短的回答是

在您的交互模型中,您可以为日期和时间段提供以下内置槽类型:

文档解释了每种话语映射的类型。

例如,您可以在设置意图的地方创建一个交互模型,我们称之为 GetMachineStateIntent,然后将以下话语映射到该模型:

what was the machine state at {Time} on {Date}
what was the state of the machine at {Time} on {Date}
what was the machine state at {Time} {Date}
what was the state of the machine at {Time} {Date}
what was the machine state on {Date} at {Time} 
what was the state of the machine on {Date} {Time} 
what was the machine state {Date} at {Time} 
what was the state of the machine {Date} {Time} 

在您的技能中,您将处理 GetMachineStateIntent 并且在请求中您将收到两个插槽中每个插槽的填充值。

作为第一步,在构建交互模型时,最好让 Alexa 简单地回复语音以确认它收到了您请求的日期和时间段值。

例如,您可以包括如下内容:

if (request.type === "IntentRequest" && request.intent.name == "GetMachineStateIntent") {
    var dateSlot = request.intent.slots.Date != null ?
                   request.intent.slots.Date.value : "unknown date";
    var timeSlot = request.intent.slots.Time != null ?
                   request.intent.slots.Time.value : "unknown time";

    // respond with speech saying back what the skill thinks the user requested
    sendResponse(context, callback, {
      output: "You wanted the machine state at " 
                + timeSlot + " on " + dateSlot,
      endSession: true
    });

}