Lambda 函数不适用于 Alexa 技能?

Lambda function not working with Alexa skill intend?

我尝试将名为 "PageviewsIntent" 的自定义意图与我的 lambda 函数连接起来。可悲的是这不起作用?

我的想法是先这样连接

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

并将其添加到请求处理程序

 .addRequestHandlers(
    PageviewsHandler,
    StartHandler,

但它不起作用。调用工作正常。如果我将它调用到 StartHandler,getGA() 函数将起作用。

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();

我仍然没有解决问题,但我现在测试了 lambda。似乎没有问题。如果我这样测试

我得到了正确的结果

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

https://developer.amazon.com/alexa/console/ask/build中我是这样配置的

有没有可能是测试工具没有用?

测试界面如下所示:

在你的 lambda 中,你的 'can handle' 反映了

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

但您的 Intent 名称实际上是:

PageviewsIntent

您提供屏幕截图的测试事件不会调用您共享的代码。请仔细检查您的 Intent 名称和 canHandle 是否匹配。

如果您在 Alexa 开发者控制台中更改了 Intent 名称,则需要确保保存并构建您的 Alexa 技能。此外,确保您的技能指向正确版本的 lambda 以消除任何混淆。

您的问题是由 StartHandler 中不正确的会话处理引起的。默认情况下,当响应构建器中仅使用 speak() 方法时,它会关闭。您应该通过在欢迎消息中添加 .reprompt() 来保持会话打开:

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

或通过显式添加 .withShouldEndSession(false)

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

给你的回复生成器。您可以在 Alexa Developer Blog

上找到有关请求处理的更多信息