I have made a "http.get" request from lambda function and am using the response for constructing the reply back to ASK.
In the below code - in switch case "GetName" context.succeed is never called while in switch case "GetNameParameter" context.succeed is successfully executed. Is the some problem due to "http.get" request of "GetName"? How to resolve this.
Whenever I add "http.get" request, the context.succeed method does not work and hence I cannot get alexa working.
Could anybody help in getting the problem solved.
Following is my code:
var http = require('http');
exports.handler = (event, context, callback) => {
// TODO implement
if(event.session.new){
console.log("NEW SESSION");
}
switch(event.request.type){
case "LaunchRequest":
console.log("LAUNCH REQUEST");
context.succeed(
generateResponse(
buildSpeechletResponse("Welcome", true),
{}
)
);
break;
case "IntentRequest":
console.log("IntentRequest");
switch(event.request.intent.name){
case "GetName":
var endpoint = //endpoint
var body = ""
http.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
var data = JSON.parse(body)
var name = data.name
console.log("Name: "+name);
//Not getting executed
context.succeed(
generateResponse(
buildSpeechletResponse(`Name is ${data}`, true),
{}
)
)
})
})
break;
case "GetNameParameter":
context.succeed(
generateResponse(
buildSpeechletResponse("OK, Name is "+event.request.intent.slots.Name.value, true),
{}
)
);
break;
}
break;
case "SessionEndedRequest":
// Session Ended Request
console.log(`SESSION ENDED REQUEST`);
break;
default:
context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);
}
callback(null, 'Hello from Lambda');
};
buildSpeechletResponse = (outputText, shouldEndSession) => {
return {
outputSpeech: {
type: "PlainText",
text: outputText
},
shouldEndSession: shouldEndSession
};
};
generateResponse = (speechletResponse, sessionAttributes) => {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
};
I got it, callback was causing the problem.
Commented "callback(null, 'Hello from Lambda');" and problem was solved.