如果您在Alexa技能中无法从意图中获取数据,可以尝试以下解决方法:
{
"intents": [
{
"name": "GetDataIntent",
"slots": [
{
"name": "data",
"type": "AMAZON.NUMBER",
"samples": [
"get data {data}"
]
}
],
"samples": [
"get data",
"retrieve data",
"fetch data"
]
}
]
}
handlerInput.requestEnvelope.request.intent.slots
对象来访问意图中的槽位数据。const GetDataIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' &&
Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetDataIntent'
);
},
handle(handlerInput) {
const dataSlotValue = handlerInput.requestEnvelope.request.intent.slots.data.value;
// 处理获取到的数据
// ...
const speakOutput = `您请求的数据是 ${dataSlotValue}`;
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
{
"types": [
{
"name": "MyCustomSlotType",
"values": [
{
"name": {
"value": "value1"
}
},
{
"name": {
"value": "value2"
}
},
{
"name": {
"value": "value3"
}
}
]
}
]
}
const https = require('https');
const options = {
hostname: 'api.example.com',
path: '/data',
method: 'GET'
};
const GetDataIntentHandler = {
canHandle(handlerInput) {
return (
Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' &&
Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetDataIntent'
);
},
async handle(handlerInput) {
const dataSlotValue = handlerInput.requestEnvelope.request.intent.slots.data.value;
// 与外部服务进行通信,获取数据
const data = await getDataFromExternalService(dataSlotValue);
// 处理获取到的数据
// ...
const speakOutput = `您请求的数据是 ${data}`;
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
function getDataFromExternalService(data) {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
}
通过上述解决方法,您可以尝试修复无法从Alexa意图中获取数据的问题,并根据您的技能需求进行相应的处理和解析。