2017-11-04 4 views
0

現在、私の最初のAlexaスキル、単純な電卓を書こうとしています。私はちょうど追加意図に取り組むことによってそれを始めさせようとしています。私は間違いを覚えており、この問題について十分に文書化されているものは何も見つかりませんでした。ここでnode.jsのAMAZON.NUMBERタイプスロットを取得する

は、関連するNode.jsのコードです:

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

そして最後に、エラーが生成されている:

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

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コードですどんな助けでも大いに感謝されます

+0

あなたハンドラ関数 –

+0

@VijayanathViswanathanを共有してください。私は答えでそれを更新しました。試してみてください – gannon96

+0

あなたの意図スキーマが正しく見えない多くのJSONコードを含むように更新 –

答えて

0

インタラクションモデルのインテントスキーマが正しく表示されません。 @Vijayanath Viswanathanので語ったよう適切に、以下のスキーマによって、あなたの意思スキーマを変更しない

{ 
 
    "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。この変更により、

、あなたはスロットの値を取得します。

関連する問題