Amazon Lex没有直接支持接受点的插槽类型,但可以使用自定义插槽类型和格式验证器实现类似的功能。下面是一个示例:
前提条件: 使用AWS Lambda创建一个函数,并将其与Amazon Lex的Lambda解析器集成。在函数中,使用以下代码创建一个新的自定义插槽类型和格式验证器。
const AWS = require('aws-sdk');
const lexmodelbuildingservice = new AWS.LexModelBuildingService();
exports.handler = (event, context, callback) => {
const slotTypeParams = {
description: "Custom slot type for accepting dots",
name: "CustomDotSlotType",
enumerationValues: [
{value: "."}
]
};
lexmodelbuildingservice.putSlotType(slotTypeParams, function(err, data) {
if (err) {
console.log(err, err.stack);
callback(err);
} else {
console.log(data);
const intent = event.currentIntent;
const slots = intent.slots;
const slotValue = slots.CustomDotSlot;
const matched = slotValue.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/); // format validation
if (matched) {
callback(null, {
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": `Your IP address is ${matched[0]}`
}
}
});
} else {
callback(null, {
"dialogAction": {
"type": "ElicitSlot",
"intentName": intent.name,
"slots": slots,
"slotToElicit": "CustomDotSlot",
"message": {
"contentType": "PlainText",
"content": "Please enter a valid IP address with dots in between"
}
}
});
}
}
});
};
以上代码创建一个名为“CustomDotSlotType”的自定义插槽类型,并在“putSlotType” API调用中将点添加为枚举值。此外,代码还包括一个格式验证器来确保用户输入的值具有正确的格式(例如,“192.168.0.1”)。
接下来,在Amazon Lex的“插槽类型” > “自定义插槽类型”页面中,将新创建的插槽类型添加到您的Bot中。