Javascript Alexa:选择国家/地区时请求的技能错误
我有一种旅行技巧,可以让我了解一个国家(如果有的话)。如果它不存在。我想调用旅行意图错误消息,但它会告诉我“请求的技能的响应有问题”,下面是示例对话:Javascript Alexa:选择国家/地区时请求的技能错误,javascript,node.js,aws-lambda,alexa,Javascript,Node.js,Aws Lambda,Alexa,我有一种旅行技巧,可以让我了解一个国家(如果有的话)。如果它不存在。我想调用旅行意图错误消息,但它会告诉我“请求的技能的响应有问题”,下面是示例对话: 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
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];
}
}
}
var-Alexa=require('Alexa-sdk');
const APP_ID=“”;
常数skillData=[
{
国家:“法国”,
成本:“$175是在法国旅行的平均每日价格。一天的平均食物价格是$36。一对夫妇的平均酒店价格是$206”
},
{
国家:“西班牙”,
费用:“$135是在西班牙旅行的平均每日价格。一天的平均食物价格是$32。一对夫妇的平均酒店价格是$118”
{
国家:“爱尔兰”,
费用:“$125是在爱尔兰旅行的平均每日价格。一天的平均食物价格是$36。一对夫妇的平均酒店价格是$122”
},
{
国家:“匈牙利”,
费用:“84美元是在匈牙利旅行的平均每日价格。一天的平均食物价格是19美元。一对夫妇的平均酒店价格是95美元。”
},
{
国家:“加拿大”,
成本:“$127是在加拿大旅行的平均每日价格。一天的平均食物价格是$27。一对夫妇的平均酒店价格是$130”
},
{
国家:“越南”,
成本:“41美元是在越南旅行的平均每日价格。一天的平均食品价格是9.66美元。一对夫妇的平均酒店价格是42美元。”
}
];
变量处理程序={
“LaunchRequest”:函数(){
这个。发射(“:askWithCard”,“欢迎来到旅行助手。告诉我你要去哪个国家。我会告诉你你平均需要花多少钱在食物和住宿上。”,“告诉我你要去哪个国家,我会告诉你平均需要花多少钱在食物和住宿上”,“旅行助手指南”,“你要去哪个国家?我会告诉你。”你平均需要花多少钱在食物和住宿上;
},
“旅行成本”:函数(){
var countrySlot=this.event.request.intent.slots.country.value;
如果(countrySlot==“未定义”){
this.emit(':tell','Sorry this not exist');
}否则{
emit(':tellWithCard',getSuggestion(skillData,'country',countrySlot.toUpperCase()).costs,'country','costs');
}
}
};
exports.handler=函数(事件、上下文){
var alexa=alexa.handler(事件、上下文);
alexa.登记员(处理员);
execute();
};
函数getSuggestion(数据、propName、propValue){
对于(变量i=0;i
我正在使用AMAZON.County插槽。您正在将
countrySlot
与字符串“未定义的”
进行比较,而不是未定义的
,删除“
可能会解决您的问题。但是当定义了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');
}