Amazon alexa skill 在 lambda中获取IntentName slotName slots sessionId等相关值
我们做skill方面的开发,一定会遇到获取skill上面的一些值的问题,也就是下面我截图里面的一些东西。下面我介绍几种获取方法
1、获取IntentName、sessionId、userId
//获取intentName、SessionId、userId
let request = handlerInput.requestEnvelope.request;
//intentName
let intentName = request.intent.name;
//sessionId
let sessionId = handlerInput.requestEnvelope.session.sessionId;
//userId
let userId = handlerInput.requestEnvelope.session.user.userId
//你获取其他的值方法是差不多的 他这里是一个json 最重要的就是:
// let request = handlerInput.requestEnvelope.request
// 当然你要观察键值对的关系
2、获取slots slotValue
我先说和上面那种方法的获取方式
// 观察我给出的json图片 看slot位于什么地方
let request = handlerInput.requestEnvelope.request;
//slots 输出出来是一个数组,你需要自己转化
let slots = request.intent.slots;
// slotVale
let slotValue = request.intent.slots.Food_ct.value; //Food_ct 为你的slot名称
//slotName
let slotName = request.intent.slots.name;
下面是在gtihub上研究别人的skill时发现的获取slot的方法,也挺好用的
//定一个获取slot的函数
function getSlotValues(filledSlots) {
const slotValues = {};
console.log(`The filled slots: ${JSON.stringify(filledSlots)}`);
Object.keys(filledSlots).forEach((item) => {
const name = filledSlots[item].name;
if (filledSlots[item] &&
filledSlots[item].resolutions &&
filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
case 'ER_SUCCESS_MATCH':
slotValues[name] = {
synonym: filledSlots[item].value,
resolved: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
isValidated: true,
};
break;
case 'ER_SUCCESS_NO_MATCH':
slotValues[name] = {
synonym: filledSlots[item].value,
resolved: filledSlots[item].value,
isValidated: false,
};
break;
default:
break;
}
} else {
slotValues[name] = {
synonym: filledSlots[item].value,
resolved: filledSlots[item].value,
isValidated: false,
};
}
}, this);
return slotValues;
}
//获取filledSlots
const filledSlots = handlerInput.requestEnvelope.request.intent.slots;
// 调用函数 这里获取的出是整个 request里面的和slot相关的值
const slotValues = getSlotValues(filledSlots);
// 获取slotValue
let slotValue = slotValues.Temp_stt.synonym //Temp_stt 是slot名字
//如果你需要其他相关的数据 也是可以抓出来的
//把上面的 slotValues数组转化
let slotValuess = JSON.stringify(filledSlots)
如果你要获取其他值 ,你仔细观察alexa 的JSON Input 都是可以取出来的