在Alexa技能中正确使用动态实体示例。
动态实体在Alexa技能中非常有用,可以让用户输入或选择不同的选项。但是,有时候Alexa可能无法正确识别用户请求的动态实体。
要解决这个问题,需要在技能代码中正确地定义和使用动态实体示例。以下是一个基本的示例:
const handlers = {
'LaunchRequest': function () {
this.emit('MyDynamicEntityIntent');
},
'MyDynamicEntityIntent': function () {
const myEntity = this.event.request.intent.slots.MyDynamicEntity.value;
//处理用户请求中的动态实体
this.emit(':tell', '您选择了' + myEntity);
},
//定义动态实体示例
'AMAZON.FallbackIntent': function () {
const dynamicEntitiesList = ['实体1', '实体2', '实体3'];
const updatedEntities = this.event.request.intent.slots;
console.log(updatedEntities);
const dynamicEntityResolution = {
'resolutionsPerAuthority': [
{
'authority': 'amzn1.er-authority.echo-sdk.amzn.com',
'status': {
'code': 'ER_SUCCESS_MATCH'
},
'values': []
}
]
};
const dynamicEntityMatches = [];
Object.keys(updatedEntities).forEach((entityName) => {
if (dynamicEntitiesList.includes(updatedEntities[entityName].value)) {
dynamicEntityMatches.push(updatedEntities[entityName].value);
dynamicEntityResolution.resolutionsPerAuthority[0].values.push({
'value': {
'name': updatedEntities[entityName].value,
'id': entityName
}
});
}
});
if (dynamicEntityMatches.length > 0) {
this.emit(':delegate', dynamicEntityResolution);
} else {
this.emit(':delegate');
}
}
};
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
在这个示例中,定义了一个名为“MyDynamicEntityIntent”的Intent,它处理用户请求中的动态实体。有一个名为“AMAZON.FallbackIntent”的Intent,其中定义了可接受的动态实体列表。对于每个更新的实体值,它都会检查该值是否在预定义的动态实体列表中,并且解析实体。随后在Lambda函数中注册处理程序,并执行技能代码。
现在,在Alexa技能中可以正确地使用动态实体示例了!
上一篇:Alexa无法发出某些句子
下一篇:Alexa小部件快捷图标设计