Alexa:选择国家时请求技能错误

Alexa: Requested skill error when selecting country

我有一项旅行技能,可以为我提供有关某个国家/地区(如果存在)的信息。如果它不存在。我想调用 Travel Intent 错误消息,但它给我“请求的技能的响应有问题”这是下面的示例对话:

User: "Alexa open travel costs"
Skill: " Welcome to the travel costs guide. Tell me what country you are going to and I will tell you how much you need on average to spend on food and accommodation. "
User: "how much is it to go to Mauritius"
Skill: "There was a problem with the requested skill's response" 

index.js

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

const APP_ID = '';

const skillData = [
    {
        country: "FRANCE",
        costs: "5 is the average daily price for traveling in France. The average price of food for one day is . The average price of a hotel for a couple is 6"
    },
    {
        country: "SPAIN",
        costs: "5 is the average daily price for traveling in Spain. The average price of food for one day is . The average price of a hotel for a couple is 8"

    {
       country: "IRELAND",
       costs: "5 is the average daily price for traveling in Ireland. The average price of food for one day is . The average price of a hotel for a couple is 2"
    },
    {
       country: "HUNGARY",
       costs: " is the average daily price for traveling in Hungary. The average price of food for one day is . The average price of a hotel for a couple is "
    },
    {
       country: "CANADA",
       costs: "7 is the average daily price for traveling in Canada. The average price of food for one day is . The average price of a hotel for a couple is 0"
    },
    {
      country: "VIETNAM",
      costs: " is the average daily price for traveling in Vietnam. The average price of food for one day is .66. The average price of a hotel for a couple is "
    }
];

var handlers = {
  'LaunchRequest': function () {
    this.emit(':askWithCard', 'Welcome to Travel Helper. Tell me what country your going to. I will tell you how much you need on average to spend on food and accommodation. ', 'Tell me what country your are going to and I will tell you much you need on average to spend on food and accomodation', 'Travel Helper Guide', 'What country are you going to? I will tell you much you need on average to spend on food and accomodation');
  },
  'TravelCosts': function() {
    var countrySlot = this.event.request.intent.slots.country.value;
    if(countrySlot === "undefined") {
      this.emit(':tell', 'Sorry this does not exist');
        } else {
          this.emit(':tellWithCard', getSuggestion(skillData, 'country', countrySlot.toUpperCase()).costs, 'country','costs');
        }
  }
};

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

function getSuggestion(data, propName, propValue) {
  for (var i=0; i < data.length; i++) {
    if (data[i][propName] == propValue) {
      return data[i];
    }
  }
}

我正在使用 AMAZON.County 插槽。

您正在将 countrySlot 与字符串 "undefined" 而不是 undefined 进行比较,删除 " 可能会解决您的问题。但是当定义了countrySlot时,如果找不到skillData中的国家就会出错。

您可以在 countrySlot !== undefined 时通过查找其索引来检查该国家/地区是否存在。否则告诉用户该国家/地区不在列表中。

var countrySlot = this.event.request.intent.slots.country.value;

if(countrySlot !== undefined && skillData.findIndex(element => element.country === countrySlot.toUpperCase()) >= 0) {
  console.log(`country ${countrySlot} exist`);
  this.emit(':tellWithCard', getSuggestion(skillData, 'country', countrySlot.toUpperCase()).costs, 'country','costs');
} 
else {
  console.log(`can't find country: ${countrySlot} in skillData`);
  this.emit(':tell', 'Sorry this does not exist');
}