【问题标题】:Getting AMAZON.NUMBER type slot in node.js在 node.js 中获取 AMAZON.NUMBER 类型插槽
【发布时间】:2017-11-04 22:58:24
【问题描述】:

目前正在尝试编写我的第一个 Alexa 技能,一个简单的计算器。我试图通过处理附加意图来开始它。我收到了一连串的错误,并且没有发现任何关于这个问题的有据可查的东西。

这里是相关的node.js代码:

var https = require('https');
var Alexa = require('alexa-sdk');

exports.handler = (event, context) => {

try {

if (event.session.new) {
  // New Session
  console.log("NEW SESSION");
}

switch (event.request.type) {

  case "LaunchRequest":
    // Launch Request
    console.log(`LAUNCH REQUEST`);
    context.succeed(
      generateResponse(
        buildSpeechletResponse("Launch Request", "Welcome to Pro Calculator", "", true),
        {}
      )
    );
    break;

  case "IntentRequest":
    // Intent Request
    console.log(`INTENT REQUEST`);
            onIntent(event.request,
                event.session,
              function callback(sessionAttributes, speechletResponse){
                    context.succeed(generateResponse(sessionAttributes, speechletResponse));
                });
    break;

  case "SessionEndedRequest":
    // Session Ended Request
    console.log(`SESSION ENDED REQUEST`);
    break;

  default:
    context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);

}

  } catch(error) { context.fail(`Exception: ${error}`) }

};

// Called when the user specifies an intent for this skill.
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId
    + ", sessionId=" + session.sessionId);

    var cardTitle = "Addition";

var intent = intentRequest.intent,
    intentName = intentRequest.intent.name;

// dispatch custom intents to handlers here
    switch(intentName){

        //addition
        case "addIntent":
            var valA = this.event.request.intent.slots.valA;
            var valB = this.event.request.intent.slots.valB;
            var ans = valA + valB;
            callback(session.attributes, buildSpeechletResponse(cardTitle, `The answer is ${ans}`, "", "true"));
            break;

        default:
            throw "Invalid intent";
    }
}

这里是相关的json代码:

{
    "intent": "addIntent"
},
{
  "slots": [
    {
      "name": "valA",
      "type": "AMAZON.NUMBER"
    },
    {
      "name": "valB",
      "type": "AMAZON.NUMBER"
    }
  ],
}

最后,生成的错误:

{
  "errorMessage": "Exception: TypeError: Cannot read property 'request' of undefined"
}

任何帮助将不胜感激

【问题讨论】:

  • 请分享你的处理函数
  • @VijayanathViswanathan 更新以包含更多 json 代码
  • 您的意图架构看起来不正确。我已经在答案中更新了它。请尝试一下

标签: json node.js amazon alexa alexa-skills-kit


【解决方案1】:

您在交互模型中的意图架构看起来不正确。请尝试以下意图架构,

{
    "intents": [
      {
        "slots": [
          {
            "name": "valA",
            "type": "AMAZON.NUMBER"
          },
          {
            "name": "valB",
            "type": "AMAZON.NUMBER"
          }
        ],
        "intent": "addIntent"
      }
      
    ]
  }

【讨论】:

    【解决方案2】:

    首先,@Vijayanath Viswanathan 告诉您,您的意图架构不正确,请通过以下架构更改您的意图架构:

    {
        "intents": [
          {
            "intent": "addIntent",
            "slots": [
              {
                "name": "valA",
                "type": "AMAZON.NUMBER"
              },
              {
                "name": "valB",
                "type": "AMAZON.NUMBER"
              }
            ]
          }  
        ]
    }
    

    然后你必须改变你的代码来获取槽值。

    改成这样,

    var valA = this.event.request.intent.slots.valA.value;

    var valB = this.event.request.intent.slots.valB.value;

    而不是这个,

    var valA = this.event.request.intent.slots.valA;

    var valB = this.event.request.intent.slots.valB;

    通过此更改,您将获得插槽的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      相关资源
      最近更新 更多