Alexa技能,自定义广告位-日期和时间 [英] Alexa skill, custom slot - date and time

查看:107
本文介绍了Alexa技能,自定义广告位-日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一项技能,并且希望能够在dynamo db表中的特定日期和时间调用计算机状态。

I've created a skill and i want to be able to call upon a machine state at a certain date and time from my dynamo db table.

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

my first column is date and my sort key is time.

我是否需要为一年中所有365天的日期创建一个自定义时段,还是有一种更快的方法?我还需要为一天中的每一分钟创建一个自定义广告位。

Would i need to create a custom slot for date with all 365 daysof the year or is there a quicker way to do this? Also would i need to create a custom slot for every minute of the day.

代码:

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");
    }
  }
};

推荐答案

简短的答案是 no

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

In your interaction model you can supply the following built in slot types for your date and time slots:

  • built-in Date slot: https://developer.amazon.com/docs/custom-skills/slot-type-reference.html#date
  • built-in Time slot: https://developer.amazon.com/docs/custom-skills/slot-type-reference.html#time

文档解释了每种类型的发声对应的类型。

The docs explain what type of utterances map to each.

例如,您可以创建一个交互模型,在其中建立意图,我们称其为 GetMachineStateIntent ,然后将以下话语映射到该模型:

For instance, you could create an interaction model where you set up an intent, let's call it GetMachineStateIntent and then map the following utterances to this model:

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 ,然后在请求中,您将收到两个插槽中每个插槽的填充值。

In your skill, you would handle the GetMachineStateIntent and in the request you will receive the filled in values for each of the two slots.

第一步,在建立交互模型时,让Alexa简单地以语音回复,确认它已收到您的请求中的日期和时隙值。

As a first step, while building the interaction model, it would be good to have Alexa simply respond back with speech confirming that it received the date and time slot values from your request.

例如,您可能会包含以下内容:

For example, you might include something like:

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
    });

}

这篇关于Alexa技能,自定义广告位-日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆