node.jsalexa-skills-kitamazon-echo

Lambda function not working with Alexa skill intend?


I try to connect a custom intent called "PageviewsIntent" with my lambda function. Sadly this does not work?

My idea was to connect first like this

return request.type === 'IntentRequest'
        && request.intent.name === 'PageviewsIntent';

and to add it to the Request Handlers

 .addRequestHandlers(
    PageviewsHandler,
    StartHandler,

But its not working. The invocation works fine. The getGA() function works to if I call it to the StartHandler.

const Alexa = require('ask-sdk');
const { google } = require('googleapis')

const jwt = new google.auth.JWT(
  XXXXX,
  null,
  XXXXX,
  scopes
)

const gaQuery = {
  auth: jwt,
  ids: 'ga:' + view_id,
  'start-date': '1daysAgo',
  'end-date': '1daysAgo',
  metrics: 'ga:pageviews'
}

const getGA = async () => {
  try {
    jwt.authorize()
    const result = await google.analytics('v3').data.ga.get(gaQuery)
    return result.data.totalsForAllResults['ga:pageviews'];
  } catch (error) {
    throw error
  }
}

const PageviewsHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
        && request.intent.name === 'Pageviews';
  },
  async handle(handlerInput) {
    try {
      const gadata = await getGA()
      const speechOutput = GET_FACT_MESSAGE + " Bla " + gadata;

      return handlerInput.responseBuilder
        .speak(speechOutput)
        .withSimpleCard(SKILL_NAME, speechOutput)
        .getResponse();

    } catch (error) {
      console.error(error);
    }
  },
};

const StartHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'LaunchRequest';
  },
  handle(handlerInput) {
    try {
      const speechOutput = GET_FACT_MESSAGE;

      return handlerInput.responseBuilder
        .speak(speechOutput)
        .withSimpleCard(SKILL_NAME, speechOutput)
        .getResponse();

    } catch (error) {
      console.error(error);
    }
  },
};

const HelpHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'AMAZON.HelpIntent';
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(HELP_MESSAGE)
      .reprompt(HELP_REPROMPT)
      .getResponse();
  },
};

const ExitHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && (request.intent.name === 'AMAZON.CancelIntent'
        || request.intent.name === 'AMAZON.StopIntent');
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak(STOP_MESSAGE)
      .getResponse();
  },
};

const SessionEndedRequestHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'SessionEndedRequest';
  },
  handle(handlerInput) {
    console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);

    return handlerInput.responseBuilder.getResponse();
  },
};

const ErrorHandler = {
  canHandle() {
    return true;
  },
  handle(handlerInput, error) {
    console.log(`Error handled: ${error.message}`);

    return handlerInput.responseBuilder
      .speak('Sorry, an error occurred.')
      .reprompt('Sorry, an error occurred.')
      .getResponse();
  },
};

const SKILL_NAME = 'Blick Google Analytics';
const GET_FACT_MESSAGE = 'Hallo zu Blick Google Analytics';
const HELP_MESSAGE = 'Bla Bla Hilfe';
const HELP_REPROMPT = 'Bla Bla Hilfe';
const STOP_MESSAGE = 'Ade!';

const skillBuilder = Alexa.SkillBuilders.standard();

exports.handler = skillBuilder
  .addRequestHandlers(
    PageviewsHandler,
    StartHandler,
    HelpHandler,
    ExitHandler,
    SessionEndedRequestHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();

I still don't made it to solve the problem but I tested now the lambda. there seems to be no problem. If I test like this

enter image description here

I get a correct result

  "outputSpeech": {
      "type": "SSML",
      "ssml": "<speak>Bla 5207767</speak>"

In https://developer.amazon.com/alexa/console/ask/build i configured it like this

enter image description here

Is it possible that the testing tool is not working?

Test interface looks like this:

enter image description here


Solution

  • Your problem is caused by incorrect session handling in StartHandler. By default, it is closed when there is only a speak() method used in the response builder. You should keep session open, by adding .reprompt() to your welcome message:

    return handlerInput.responseBuilder
            .speak(speechOutput)
            .reprompt(speechOutput)
            .withSimpleCard(SKILL_NAME, speechOutput)
            .getResponse();
    

    or by explicit adding .withShouldEndSession(false)

    return handlerInput.responseBuilder
            .speak(speechOutput)
            .withSimpleCard(SKILL_NAME, speechOutput)
            .withShouldEndSession(false)
            .getResponse();
    

    to your response builder. You can find more about request handling on Alexa Developer Blog