2017-11-04 9 views
0

현재 첫 번째 Alexa 기술 인 간단한 계산기를 작성하려고합니다. 나는 단지 추가 의도로 작업을 시작하려고 노력하고 있습니다. 나는 실수를 많이하고 있으며이 문제에 관해 잘 기록 된 것을 발견하지 못했습니다. 여기 node.js에서 AMAZON.NUMBER 유형 슬롯을 얻는 중

는 관련 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" 
} 

어떤 도움이라도 대단히 감사하겠습니다.

+0

처리기 기능을 공유하십시오 –

+0

@VijayanathViswanathan이 더 많은 json 코드를 포함하도록 업데이트 됨 – gannon96

+0

의도 스키마가 올바르지 않습니다. 나는 대답에서 그것을 업데이트했다. 그걸로 시도하십시오. –

답변

0

상호 작용 모델의 의도 스키마가 올바르지 않습니다. @Vijayanath 비스와 나탄에 의해 말대로 적절한 아래 스키마에 의해 의도 스키마를 변경하지

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

0

모든 의도 스키마 우선, 아래의 의도 스키마한다 시도하십시오 :

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

그러면 슬롯 값을 얻기 위해 코드를 변경해야합니다.

이로 변경,

var에 발라 = this.event.request.intent.slots.valA.value;

var valB = this.event.request.intent.slots.valB.value; 이 대신

,

VAR 발라 = this.event.request.intent.slots.valA;

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

이렇게 변경하면 슬롯 값이 표시됩니다.