【问题标题】:Alexa: Requested skill error when selecting countryAlexa:选择国家时请求技能错误
【发布时间】:2018-10-01 23:45:24
【问题描述】:

我有一项旅行技能,可以为我提供有关某个国家/地区(如果存在)的信息。如果它不存在。我想调用 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: "$175 is the average daily price for traveling in France. The average price of food for one day is $36. The average price of a hotel for a couple is $206"
    },
    {
        country: "SPAIN",
        costs: "$135 is the average daily price for traveling in Spain. The average price of food for one day is $32. The average price of a hotel for a couple is $118"

    {
       country: "IRELAND",
       costs: "$125 is the average daily price for traveling in Ireland. The average price of food for one day is $36. The average price of a hotel for a couple is $122"
    },
    {
       country: "HUNGARY",
       costs: "$84 is the average daily price for traveling in Hungary. The average price of food for one day is $19. The average price of a hotel for a couple is $95"
    },
    {
       country: "CANADA",
       costs: "$127 is the average daily price for traveling in Canada. The average price of food for one day is $27. The average price of a hotel for a couple is $130"
    },
    {
      country: "VIETNAM",
      costs: "$41 is the average daily price for traveling in Vietnam. The average price of food for one day is $9.66. The average price of a hotel for a couple is $42"
    }
];

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 广告位。

【问题讨论】:

    标签: javascript node.js aws-lambda alexa


    【解决方案1】:

    您将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');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2018-09-24
      • 2020-08-28
      • 1970-01-01
      • 2021-05-11
      • 2018-08-17
      • 1970-01-01
      • 2019-02-06
      相关资源
      最近更新 更多