alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 736 forks source link

alexa-skills-kit-sdk-for-nodejs with external service #163

Closed amdev28 closed 6 years ago

amdev28 commented 7 years ago

I am able to invoke the lambda expression using Alexa sdk, but unable to call the external service from the lambda expression. I am not getting any error or response. I'm not sure what's wrong with this. While testing the lambda expression I'm only able to get the logs until 'Do the POST call', not after that. That means something wrong in https.request. The same logic tested with node.js is able to get the response correctly. Any thoughts?

var https = require('https');
    var responseString = ''; 

    var postheaders = {
        'Content-Type' : 'application/json'//,
        //'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
    };

    var optionspost = {
        host : '{host name}',
        port : 443,
        path : '{path name}',
        method : 'POST',
        headers : postheaders
    };

    console.info('Options prepared:');
    //console.info(optionspost);
    console.info('Do the POST call');

    // do the POST call
    var reqPost = https.request(optionspost, function(res) {

        console.log("statusCode: ", res.statusCode);
        // uncomment it for header details
        // console.log("headers: ", res.headers);

        res.on('data', function(d) {
            console.info('POST result:\n');
            process.stdout.write(d);
            responseString += d;
            console.info('\n\nPOST completed');
        });

        res.on('error', function(e) {
            console.error('error');
            console.error(e);
        });

        res.on('end', function () {
            console.info('end');       
            var responseObject = JSON.parse(responseString);

            if (responseObject.error) {
                console.info("NOAA error: " + responseObject.error.message);        
            } else {
                console.info(responseString);             
            }
         });
    }).on('error', function(e) {
        console.error('Communications err' + e);
        console.error(e);
    }); 

reqPost.end();
reqPost.on('error', function(e) {
    console.error(e);
}).on('error', function (e) {
        console.log("Communications error: " + e.message);        
});
ghost commented 7 years ago

Hi @amdev28

Could you post code for the handler which is executing this function. It's possible that the function is terminating prior to the HTTP call returning. It's also possible that your lambda function is timing out, have you checked the cloudwatch logs? look for a timeout exceeded error message.

tianrenz commented 6 years ago

I'm closing this issue now. Please feel free to reopen the issue if problem persists. Thanks