Amazon Alexa - 未定义的语音响应

Posted

技术标签:

【中文标题】Amazon Alexa - 未定义的语音响应【英文标题】:Amazon Alexa - Undefined speech response 【发布时间】:2018-08-16 08:34:59 【问题描述】:

我正在尝试使用 Alexa 事实技能蓝图调用“SetLanguageIntent”中的“AskQuestion”来构建对用户的语音。

'SetMyLanguageIntent': function() 
    this.attributes['language'] = this.event.request.intent.slots.languages.value;
    var language = this.attributes['language'];

    this.response
      .speak('Okay, I will ask you some questions about ' +
        language + '. Here is your first question.' +
        this.AskQuestion).listen(this.AskQuestion);
    this.emit(':responseReady');
  ,

'AskQuestion': function() 
    var language = this.attributes['language'];
    var currentQuestion = flashcardsDictionary[this.attributes['currentFlashcardIndex']].question;

    return 'In ' + language + ', ' + currentQuestion;
  ,

这是服务器的响应:


  "version": "1.0",
  "response": 
    "outputSpeech": 
      "ssml": "<speak> Okay, I will ask you some questions about undefined. Here is your first question.undefined </speak>",
      "type": "SSML"
    ,
    "reprompt": 
      "outputSpeech": 
        "ssml": "<speak> undefined </speak>",
        "type": "SSML"
      
    ,

Alexa 在回复中说出“未定义”一词,对话如下:“好的,我会问你一些关于未定义的问题。这是你的第一个问题。未定义”

为什么响应“未定义”?以及如何解决这个问题?

这是整个代码:

'use strict';

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

var flashcardsDictionary = [
    question: 'how do you find the length of a string?',
    rubyAnswer: 'length',
    pythonAnswer: 'Len',
    javascriptAnswer: 'length'
  ,
  
    question: 'how do you print to the console or terminal?',
    rubyAnswer: 'puts',
    pythonAnswer: 'print',
    javascriptAnswer: 'console.log'
  ,
  
    question: 'are boolean terms capitalized or not capitalized?',
    rubyAnswer: 'not capitalized',
    pythonAnswer: 'capitalized',
    javascriptAnswer: 'not capitalized'
  
];

var DECK_LENGTH = flashcardsDictionary.length;

var handlers = 
  // Open Codecademy Flashcards
  'LaunchRequest': function() 
    this.attributes['language'] = '';
    this.attributes['numberCorrect'] = 0;
    this.attributes['currentFlashcardIndex'] = 0;

    this.response
      .listen('Welcome to Flashcards. In this session, do you want to test' +
        ' your knowledge in Ruby, Python, or Javascript?').speak(
        'Which language would you like to practice?');
    this.emit(':responseReady');
  ,

  'SetMyLanguageIntent': function() 
    this.attributes['language'] = this.event.request.intent.slots.languages.value;
    var language = this.attributes['language'];

    this.response
      .speak('Okay, I will ask you some questions about ' +
        language + '. Here is your first question.' +
        this.AskQuestion).listen(this.AskQuestion);
    this.emit(':responseReady');
  ,

  // User gives an answer
  'AnswerIntent': function() 
    var userAnswer = this.event.request.intent.slots.answer.value;
    var language = this.attributes['language'];
    var languageAnswer = language + userAnswer;
    var correctAnswer = flashcardsDictionary[this.attributes['currentFlashcardIndex']][languageAnswer];


    if (userAnswer === correctAnswer) 
      this.attributes['numberCorrect']++;
      var numberCorrect = this.attributes['numberCorrect'];

      this.response
        .speak('Nice job! The correct answer is ' + correctAnswer + '. You ' +
          'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
          language + ' questions correct.' + this.AskQuestion)
        .listen(this.AskQuestion);


     else 
      var numberCorrect = this.attributes['numberCorrect'];

      this.response
        .speak('Sorry, the correct answer is ' + correctAnswer + '. You ' +
          'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
          language + ' questions correct. Here is your next question.' +
          this.AskQuestion).listen(this.AskQuestion);
    

    this.attributes['currentFlashcardIndex']++;

    this.emit(':responseReady');
  ,

  // Test my language knowledge
  'AskQuestion': function() 
    var language = this.attributes['language'];
    var currentQuestion = flashcardsDictionary[this.attributes['currentFlashcardIndex']].question;

    return 'In ' + language + ', ' + currentQuestion;
  ,

  // Stop
  'AMAZON.StopIntent': function() 
    this.response.speak('Ok, let\'s play again soon.');
    this.emit(':responseReady');
  ,

  // Cancel
  'AMAZON.CancelIntent': function() 
    this.response.speak('Ok, let\'s play again soon.');
    this.emit(':responseReady');
  
;

exports.handler = function(event, context, callback) 
  var alexa = Alexa.handler(event, context);
  alexa.registerHandlers(handlers);
  alexa.execute();
;

【问题讨论】:

你能发布回复吗? ?有未定义,因为 var language = this.attributes['language']; (语言未定义)检查来自 Alexa 的请求中是否存在会话属性。 【参考方案1】:

使用this.AskQuestion() 而不是this.AskQuestion,因为AskQuestion 是一个函数而不是一个对象。

【讨论】:

以上是关于Amazon Alexa - 未定义的语音响应的主要内容,如果未能解决你的问题,请参考以下文章

Amazon Alexa进阶到播报员 以播报员的讲话风格念新闻

继Amazon Alexa机智云成功对接Google Home语音控制

开发阶段如何通过电脑测试amazon alexa自定义技能?

取消意图未调用 Alexa

通过Gokit玩转Echo语音控制开发技能

Alexa 技能未在 Amazon Alexa 应用程序中调用,但可在开发控制台中使用