【问题标题】:Alexa - How to write if-else statment using slots in node.jsAlexa - 如何使用 node.js 中的插槽编写 if-else 语句
【发布时间】:2019-08-23 15:11:42
【问题描述】:

我正在 node.js 中编写一个 Alexa 应用程序来回答产品规范问题。例如,我想问“(产品)的最大容量是多少?”目前,我对每个产品的每个问题都有不同的意图,这导致带有大量处理程序的代码非常混乱。例如,我想做的只是有一个 MaxCapacity 意图,它使用插槽(包含不同的产品)来分配事实。我在 node.js 上还是比较新的,所以如果这真的很草率,我很抱歉。我知道如何在开发者控制台上创建新插槽,只是不知道如何在后端编写代码。这是处理程序之一:

const GetNewFactHandler1 = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'LaunchRequest'
      || (request.type === 'IntentRequest'
        && request.intent.name === 'UMaxCapacityIntent');

      */ And if slot type == *product*, then: */

  },
  handle(handlerInput) {
    const factArr = data;
    const randomFact = factArr[1]; //Array with all the answers
    const speechOutput = GET_FACT_MESSAGE + randomFact;

    return handlerInput.responseBuilder
    .speak(speechOutput)
    .withSimpleCard(SKILL_NAME, randomFact)
    .getResponse();
  },
};

我希望这是有道理的,但我很乐意进一步解释。谢谢!

【问题讨论】:

    标签: node.js amazon-web-services alexa slots


    【解决方案1】:

    如果我理解正确,您想在插槽“产品”存在时调用此处理程序?如果是这样,您可以使用 'getSlot' 辅助方法。如果请求中存在槽,则返回该槽,否则返回 null。所以只需在canHandle 条件&& getSlot(handlerInput.requestEnvelope, 'product') 上添加额外的检查就可以了。

    【讨论】:

    • 感谢您的回复!插槽将永远存在(我不能问什么都没有的最大容量)。我将如何包含 getSlot 方法?我有 const Alexa = require('ask-sdk');在我的代码的顶部,但它没有处理它。只是为了澄清问题,假设该产品是笔记本电脑。插槽中的值是不同类型的笔记本电脑。如果产品 == 纬度(我现在正在使用的笔记本电脑),我想把它放在哪里,然后用某个事实做出回应。这有意义吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多