データを取得するためにRESTAPIを呼び出すスキルを構築しようとしています。HelloWorldサンプルを使用して、ニーズに合わせて変更しています。リクエストノード(node.js)を使用してリクエストを発行しています。
しかし、私の地獄のために、私はそれを機能させることができません。ログで関数が呼び出され、正しい結果が返されていることがわかりますが、Alexaに送信された応答は空です!! 私が欠けているものはありますか?
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
console.log("HelloWorldIntentHandler 1: ");
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {
console.log("HelloWorldIntentHandler 2");
var speechText = 'Hello World';
Request.get(url, function(error, response, body) {
console.log("I'm here")
var data = JSON.parse(body)
var result = data.records.totalNum
if (result > 0) {
speechText = "There are " + result + " matches";
} else {
speechText = "ERROR";
}
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
});
},
};
ログのエラーは
Error handled: speechOutput.trim is not a function
Requestの代わりにAxiosを使用してこれを機能させることができました。
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
console.log("HelloWorldIntentHandler 1: ");
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
async handle(handlerInput) {
console.log("HelloWorldIntentHandler 2");
var speechText = 'default';
try {
const response = await Axios.get(url);
var result = response.data.totalRecs;
if (result > 0) {
speechText = "There are " + result + " matches";
} else {
speechText = "ERROR";
}
console.log("text=" + speechText);
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
} catch (error) {
console.log(error);
}
},
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加