Search code examples
javascriptnode.jsfunctionreturnalexa-skills-kit

How to return handlerinput.responsebuilder inside a function using node js for Alexa Skill


I have been struck as to how to return handlerinput.responsebuilder to speak the value stored inside the variable of a function for my Alexa Skill. I am using ask-sdk-core node js for building my Alexa Skill. The function I am using is as below. Please help me in solving this.

function callback(out)
{
    var jsonurl=out;   
    console.log(jsonurl)

    return handlerInput.responseBuilder
                .speak(jsonurl)
                .getResponse();

}

Solution

  • I think you have not included handlerInput in callback function params. Try doing like below :

      handle(handlerInput) {
         var out = ""
    
            getJSON(function(data) {
                console.log(data)
                if (data !== "ERROR") {
                     var speechOutput = data
                       out = speechOutput;
                 }
             });
    
         function getJSON(callback) 
         {
                //As I don't know what result might be So I am giving some string value.
                  var result = 'Thank you !!';
                 if (result!=='') {
                     callback(result);
                  } else {
                      callback("ERROR");
                  }
         }
            return callback(out,handlerInput)
      }
    
    };
    
    function callback(out, handlerInput)
    {
    
      const jsonurl=out;
      console.log(jsonurl)
    
    
      return handlerInput.responseBuilder
             .speak(jsonurl)
             .getResponse();
    
    }
    

    The above one worked perfectly fine for me and I am getting the given result to the out variable. I think there might be a problem in the result you are getting from the URL can you check that one weather it is string or object.