0

문제는 quizIntent 핸들러의 루프에서 질문의 배열을 반복 할 수 없다는 것입니다. 첫 번째 질문 만 표시되고 더 이상 이동하지 않습니다. 나머지 코드는 정상적으로 작동합니다. 내가 원하는 것은 배열이 다 떨어질 때까지 계속 질문하고 사용자가 틀린 답을 줄 때 루프에서 빠져 나옵니다.Alexa Intent에서 객체 배열을 반복하는 방법은 무엇입니까?

나는 Alexa Programming의 초보자입니다. 도와주세요. 여기

내 배열 여기

const got = [ 
    { 
     question: "Grey Wind, Lady, Ghost, Shaggydog, Summer and the sixth direwolve's name is?", 
     answer: 'nymeria', 
    }, 
    { 
     question: "What was the name of the sinister castle where Arya and Gendry were held prisoner in season two?", 
     answer: 'harrenhal', 
    }, 
    { 
     question: "What is a person called that can enter the minds of animals?", 
     answer: 'warg', 
    }, 
    { 
     question: "What was the name of the Stark ancestral sword that was melted down by Tywin Lannister?", 
     answer: 'ice', 
    } 
]; 

내 알렉사 의도 여기

var handlers = { 
    "customIntent": function() { 
      this.response.speak("Would you like to appear for a trial by combat"); 
     this.emit(":responseReady"); 
    }, 
    "quizIntent": function() { 
     var mydecision = this.event.request.intent.slots.decision.value; 
     if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){ 
     this.response.speak("A five year old has more courage than you."); 
     this.emit(":responseReady"); 
     } 

     for(var i = 0; i <got.length; i++){ 
      var myanswer = this.event.request.intent.slots.answer.value; 
      var item = got[i].question; 
      this.response.speak(item).listen(); 
      if(myanswer!=got[i].answer){ 
       this.response.speak("Wrong Answer. You are dead"); 
       this.emit(':responseReady'); 

      } 
      if(i==got.length-1){ 
       this.response.speak("You won"); 
       this.emit(':responseReady'); 
      } 

     } 


    }, 
    "LaunchRequest": function() { 
    this.response.speak("Valar Morghulis").listen("You are supposed to say Valar Dohareis"); 
    this.emit(":responseReady"); 
    } 

}; 

인 경우에는 내 의도 스키마 당신이에서 싶어 보면 너무

{ 
    "languageModel": { 
    "types": [ 
     { 
     "name": "answerSlot", 
     "values": [ 
      { 
      "id": null, 
      "name": { 
       "value": "Nymeria", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Harrenhal", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Warg", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Ice", 
       "synonyms": [] 
      } 
      } 
     ] 
     }, 
     { 
     "name": "decisionSlot", 
     "values": [ 
      { 
      "id": null, 
      "name": { 
       "value": "Yes", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "No", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Naah", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Yeah", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Yup", 
       "synonyms": [] 
      } 
      }, 
      { 
      "id": null, 
      "name": { 
       "value": "Nope", 
       "synonyms": [] 
      } 
      } 
     ] 
     } 
    ], 
    "intents": [ 
     { 
     "name": "AMAZON.CancelIntent", 
     "samples": [] 
     }, 
     { 
     "name": "AMAZON.HelpIntent", 
     "samples": [] 
     }, 
     { 
     "name": "AMAZON.StopIntent", 
     "samples": [] 
     }, 
     { 
     "name": "customIntent", 
     "samples": [ 
      "Valar Dohareis", 
      "hello", 
      "hola" 
     ], 
     "slots": [] 
     }, 
     { 
     "name": "quizIntent", 
     "samples": [ 
      "{decision}", 
      "The answer is {answer}" 
     ], 
     "slots": [ 
      { 
      "name": "decision", 
      "type": "decisionSlot" 
      }, 
      { 
      "name": "answer", 
      "type": "answerSlot" 
      } 
     ] 
     } 
    ], 
    "invocationName": "quiz game" 
    } 
} 
+0

AWS 람다 스킬입니까? – MangoBoy

+0

예. 문제는 람다 함수를 사용하는 것입니다. – thedreamsaver

+0

람다 함수의 시간 초과를 시도하고 늘리십시오. Speaking은 비동기 호출이므로 람다 함수가 시간 초과되었습니다. – MangoBoy

답변

0

한 가지 방법이었다

var i = 0; 
var handlers = { 
    "customIntent": function() { 
      this.response.speak("Would you like to take part in a trial by combat?").listen(); 
     this.emit(":responseReady"); 
    }, 
    "quizIntent": function() { 
     var mydecision = this.event.request.intent.slots.decision.value; 
     if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){ 
     this.response.speak("Battles have been won against harder odds! Even little Lyanna Mormont has more courage than you."); 
     this.emit(":responseReady"); 
     } 

     if(i<=got.length){ 
      var item = got[i].question; 
      if(i == 0){ 
       this.response.speak("Be attentive; just like life, I won't repeat or give you a second chance. Here you go; " + item).listen(); 
       this.emit(":responseReady"); 
      } 
      else { 
      this.response.speak(item).listen(); 
      this.emit(":responseReady"); 
      } 
     } 
    }, 

    "answerIntent": function() { 
      var myanswer = this.event.request.intent.slots.answer.value; 

      if(myanswer!=got[i].answer){ 
       this.response.speak("Wrong Answer. The correct answer is " + got[i].answer + ". You are dead."); 
       this.emit(':responseReady'); 

      } 
      i++; 
      if(i==got.length){ 
       i=0; 
       this.response.speak("You emerged the ultimate victor. The best in all of Planetos."); 
       this.emit(':responseReady'); 
      } 
      this.response.speak("You survived. Say ready, when you are, for the next combat!").listen(); 
      this.emit(':responseReady'); 
    }, 

아이디어는 퀴즈의 의도와 대답의 의도를 앞뒤로 전환하는 것입니다.

1

루프 아무튼입니다 은 모든 사용자에게 단일 응답을 제공하도록 설계되어있어 단일 호출. 그래서 당신은 루프 일 것입니다 만, 첫 번째 반복의 listen 메소드가 새로운 호출이기 때문에 처음부터 시작될 것이므로 실행으로 두 번째 응답을 내 보내지 않을 것입니다.

달성하려는 목표는 세션 속성을 사용하여 성공적으로 달성 할 수 있습니다. 나는이 문제를 해결하기 위해 알아 냈

var iterations = 0; //defined globally 

// Later on, for every iteration, you simply need to call 
// into the attributes property of the alexa object to change the value. 

this.attributes['iterations'] = iterations + 1; 
관련 문제