question

newuser-02ca8f03-8510-43c6-8ccc-33875181e8b4 avatar image

Problem with calling web service and speech output

Hello,


I have a my own web service which is returning response as string. I have the following code


const HelloWorldIntentHandler = {

canHandle(handlerInput) {

return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'

&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';

},

handle(handlerInput) {

var speakOutput;

request('http://8ccdee84.ngrok.io', function (error, response, body) {

console.log('error:', error); // Print the error if one occurred

console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received

console.log('body:', body); // Print the body

speakOutput = body;


});

return handlerInput.responseBuilder

.speak(speakOutput)

.reprompt(speakOutput)

.getResponse();

}

};


The 'body' element in the response has the string data and i could see the string value get printed in the console log(Bold Highlighted above). However the speakOutput(underlined with italic) on .speak is empty and seeing the following response.


{

"body": {

"version": "1.0",

"response": {

"outputSpeech": {

"type": "SSML",

"ssml": "<speak></speak>"

},

"reprompt": {

"outputSpeech": {

"type": "SSML",

"ssml": "<speak></speak>"

}

},

"shouldEndSession": false,

"type": "_DEFAULT_RESPONSE"

},

"sessionAttributes": {},

"userAgent": "ask-node/2.7.0 Node/v8.10.0"

}

}


Anyone help me what is the issue in the code and please explain me why i'm getting the string value in the console and not on the speak output.

alexa skills kitalexaapi
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Andy Whitworth avatar image
Andy Whitworth answered

This is the async nature of node.

Move your return statement into the callback function, at the end.

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.