const GetInfoIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'GetInfoIntent');
},
handle(handlerInput) {
let data;
const request = require("request");
let options = { method: 'GET',
url : "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
qs:
{ action: 'query' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
let json = body;
let obj = JSON.parse(json);
data = obj;
});
const x = "Hello";
const speechOutput = data;
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
};
The response I am getting is Undefined
. Other APIs don't work either. Do I need to use a HTTP API? I have tried everything but nothing seems to be working.
I have the request dependency installed.
I just want Alexa to return the first paragraph of the address.
You are getting Undefined
as no value is assigned into the variable data . Because your function is returning before data is fetched from the url.
put the return statement in within the request block. Also extract the property that you want to return from obj
object.
const request = require("request");
const getData = function() {
return new Promise((resolve, reject) => {
let options = {
method: 'GET',
url : "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
qs: {
action: 'query'
}
};
request(options, function (error, response, body) {
if (error) reject(error);
resolve(JSON.parse(body)); // you may want to check parsing error.
});
});
}
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) ===
'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) ===
'HelloWorldIntent';
},
async handle(handlerInput) {
const response = await getData();
console.log(response);
return handlerInput.responseBuilder
.speak(response.query.pages["1928349"].extract)
.getResponse();
}
};
using your provided url following is the response you should expect:
{
"batchcomplete": "",
"query": {
"normalized": [
{
"from": "manulife",
"to": "Manulife"
}
],
"pages": {
"1928349": {
"pageid": 1928349,
"ns": 0,
"title": "Manulife",
"extract": "Manulife Financial Corporation (also known as Financière
Manuvie in Quebec) is a Canadian multinational insurance
company and financial services provider headquartered in
Toronto, Ontario, Canada. The company operates in Canada and
Asia as \"Manulife\" and in the United States primarily
through its John Hancock Financial division. As of December
2015, the company employed approximately 34,000 people and had
63,000 agents under contract, and has CA$935 billion in assets
under management and administration. Manulife services over 26
million customers worldwide.Manulife is the largest insurance
company in Canada and the 28th largest fund manager in the
world based on worldwide institutional assets under management
(AUM).Manulife Bank of Canada is a wholly owned subsidiary of
Manulife."
}
}
},
"limits": {
"extracts": 20
}
}
const GetInfoIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'GetInfoIntent');
},
handle(handlerInput) {
let data;
const request = require("request");
let options = { method: 'GET',
url : "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
qs:
{ action: 'query' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
let json = body;
let obj = JSON.parse(json);
return handlerInput.responseBuilder
.speak(obj.query.pages["1928349"].extract)
.getResponse();
});
const x = "Hello";
},
};
This is JSON Output { "body": { "version": "1.0", "response": {}, "userAgent": "ask-node/2.9.0 Node/v10.22.1 sample/wiki-app/v1.0", "sessionAttributes": {} } }
This is my error "There was a problem with the requested skill's response"
const GetInfoIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'GetInfoIntent');
},
handle(handlerInput) {
const speakOutput = 'reached intenthandler';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
This code works which means my getinfointenthandler does work.
const GetInfoIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'GetInfoIntent');
},
handle(handlerInput) {
let data;
const request = require("request");
let options = { method: 'GET',
url : "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
qs:
{ action: 'query' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
let json = body;
let obj = JSON.parse(json);
return handlerInput.responseBuilder
.speak(obj.query.pages["1928349"].extract)
.getResponse();
});
},
};
This code does not work. It stops after the launchrequest.
This is my JSON input
"request": {
"type": "SessionEndedRequest",
"requestId": "amzn1.echo-api.request.f26816fa-672b-4e35-9de2-126780cfb1ef",
"timestamp": "2020-10-05T20:52:16Z",
"locale": "en-US",
"reason": "ERROR",
"error": {
"type": "INVALID_RESPONSE",
"message": "SpeechletResponse was null"
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.