json 在node.js中获取AMAZON.NUMBER类型插槽

nuypyhwy  于 2023-03-13  发布在  Node.js
关注(0)|答案(2)|浏览(106)

目前正在尝试写我的第一个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"
}

任何帮助都将不胜感激

ct2axkht

ct2axkht1#

交互模型中的Intent架构看起来不正确。请尝试使用以下Intent架构,

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

fxnxkyjh2#

首先,您的Intent架构不正确,如@Vijayanath Viswanathan所述,请通过以下架构更改您的Intent架构:

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

然后,您必须更改代码以获取插槽值。
换了这个,
var valA =此事件请求意图插槽valA值;
var valB =此事件请求意图插槽valB值;
代替这一点,
var valA =此事件请求意图插槽valA;
变量valB =此事件请求意图插槽valB;
通过此更改,您将获得插槽的值。

相关问题