Unable to process alexa intent

70 views Asked by At

My code is not running, can anybody help. Unable to speak out the text, can i return handler input response. The test function is a http call which may take tieme.

function test(url, number)
{
    return 5;
}

function speak(handlerInput) {
    return handlerInput.responseBuilder
        .getResponse();
}

const NumberFactIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'NumberFactIntent';
    },

    handle(handlerInput) {

    const theNumber = handlerInput.requestEnvelope.request.intent.slots.number.value;
    const repromptOutput = " Would you like another fact?";
    const URL = "http://numbersapi.com";

    test(URL, theNumber).then(function (data) {
             console.log("data is " + data);
             handlerInput.responseBuilder
            .speak("Test Data")
            .reprompt(repromptOutput) 

             return speak(handlerInput);
        }).catch(function (data) {

             console.log("error is " + data);
             handlerInput.responseBuilder
            .speak(`I wasn't able to find a fact for ${theNumber}` )
            .reprompt(repromptOutput)
             return speak(handlerInput);
        }); 
    }
};
2

There are 2 answers

2
R. Vait On

First of all your test function doesn't return a promise. I don't know if this is intentional and you just cut api calling code to make it simpler, but it should return a promise if you want to use then on it.

If it returns a promise in your full example, then what are you missing is adding a return before test. Also you should return handlerInput from inside of your promise. Code should look like this (i'll remove some of the code, that is not relevant):

const NumberFactIntentHandler = {
    canHandle(handlerInput) {},

    handle(handlerInput) {

    const repromptOutput = " Would you like another fact?";
    const URL = "http://numbersapi.com";

    return test(URL, theNumber).then(function (data) {
             return handlerInput.responseBuilder
                .speak("Test Data")
                .reprompt(repromptOutput) 
        }).catch(function (data) {
             return handlerInput.responseBuilder
                 .speak(`I wasn't able to find a fact for ${theNumber}` )
                 .reprompt(repromptOutput)
        }); 
    }
};

Now you might be wondering why do you need those return's. This is because JS functions implicitly returns undefined, so in this case you have to explicitly tell what should be returned from handle function. Same applies to inside of the promise.

0
Dhaval Parmar On

This Code might Help You!!

 //use request for http call
 function fun(url) {
      return new Promise((resolve, reject) => {
       request.get(url,(err, res, body) => {
       console.log("url-fetched");
       return resolve(body);
     });
   });
  }

   const NumberFactIntentHandler = {
      canHandle(handlerInput) {..
      },

 async handle(handlerInput) {

   const theNumber =handlerInput.requestEnvelope.request.intent.slots.number.value;
   const repromptOutput = " Would you like another fact?";
   const URL = "http://numbersapi.com";
   let data = await fun(url);

   return handlerInput.responseBuilder
    .speak(data)
    .reprompt('is there any thing i can do for you?')
    .withSimpleCard('Hello', speechText)
    .getResponse();
  };