【问题标题】:how to make slots output different responses如何使插槽输出不同的响应
【发布时间】:2021-09-08 20:26:33
【问题描述】:

做一些智能家居技能开发,我的意图是“我的电池要多久才能{状态}”状态是空的或充满的。我知道我可以用不同的意图来做到这一点,但我不想有两个很多的意图,因为我已经有很多了。我基本上希望用户说满或空来代替 {state}。然后从那里,根据他们的答案,给出完整或空的不同答案。网上查的不多,希望大家帮忙。我也是代码新手。

【问题讨论】:

    标签: alexa alexa-skills-kit alexa-slot


    【解决方案1】:

    您应该为您的电池状态创建一个插槽:

    {
      "name": "BatteryState",
      "values": [
          {
              "id": "empty",
              "name": {
                  "value": "empty"
              }
          },
          {
              "id": "full",
              "name": {
                  "value": "full"
              }
          }
      ]
    }
    

    您可以在 UI 中自行创建,也可以在 JSON 编辑器中粘贴到 types 集合中。

    然后在您的意图中使用创建的插槽

    {
      "name": "BatteryStateIntent",
      "slots": [
          {
              "name": "batteryState",
              "type": "BatteryState"
          }
      ],
      "samples": [
          "how long until my battery is {batteryState}"
      ]
    }
    

    您可以在 UI 中自行创建,也可以在 JSON 编辑器中粘贴到 intents 集合中。

    然后在代码中:

    const BatteryStateIntentHandler = {
      canHandle(handlerInput) {
        return (
          Alexa.getRequestType(handlerInput.requestEnvelope) === "IntentRequest" &&
          Alexa.getIntentName(handlerInput.requestEnvelope) === "BatteryStateIntent"
        );
      },
      handle(handlerInput) {
        const batteryStatus =
          handlerInput.requestEnvelope.request.intent.slots.batteryState.value;
        let speakOutput = "";
        if (batteryStatus === "full") {
          const timeToFull = 10; // or some sophisticated calculations ;)
          speakOutput += `Your battery will be full in ${timeToFull} minutes!`;
        } else if (batteryStatus === "empty") {
          const timeToEmpty = 5; // or some sophisticated calculations ;)
          speakOutput += `Your battery will be full in ${timeToEmpty} minutes!`;
        } else {
          speakOutput += "I don't know this status";
        }
    
        return handlerInput.responseBuilder
          .speak(speakOutput)
          .reprompt(speakOutput)
          .getResponse();
      },
    };
    

    确保您使用了与构建器中相同的意图名称。最后,将创建的意图处理程序添加到请求处理程序:

    exports.handler = Alexa.SkillBuilders.custom()
      .addRequestHandlers(
        BatteryStateIntentHandler,
        // ...
      )
      .lambda();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 2018-09-15
      • 1970-01-01
      • 2021-02-19
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      相关资源
      最近更新 更多