0

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

Tobi
  • 1,702
  • 2
  • 23
  • 42

2 Answers2

1

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

slawciu
  • 775
  • 4
  • 15
  • I now have Certification feedback from Amazon: After the skill completes a task, the session remains open with no prompt to the user. The skill must close the session after fulfilling requests if it does not prompt the user for any input. I guess thats related to this??? – Tobi Mar 13 '19 at 12:09
0

In your lambda, your 'can handle' reflects

canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
        && request.intent.name === 'Pageviews';
  }

But your Intent name is actually:

PageviewsIntent

The test event you provided a screenshot would not have invoked the code you shared. Please double check that your Intent name and canHandle match.

If you changed your Intent name in the Alexa Developer Console, you will need to make sure that you save and build your Alexa skill. Also, make sure that your skill is pointed to the correct version of your lambda to eliminate any confusion.

rocketlobster
  • 690
  • 7
  • 18
  • Thanks for the Feedback. The Intent is called "PageviewsIntent" already, but that didn't solved the problem. The innvoation works, so I guess lambda is connected correct. – Tobi Feb 25 '19 at 15:43