Lambda函数不能与Alexa技能打算一起使用?

问题描述 投票:0回答:2

我尝试将名为“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。似乎没有问题。如果我这样测试

enter image description here

我得到了正确的结果

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

https://developer.amazon.com/alexa/console/ask/build我配置它像这样

enter image description here

测试工具可能无法正常工作吗?

测试界面如下所示:

enter image description here

node.js alexa amazon-echo
2个回答
1
投票

您的问题是由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上找到有关请求处理的更多信息


0
投票

在你的lambda中,你的“可以处理”反映出来

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

但你的意图名称实际上是:

PageviewsIntent

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

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

© www.soinside.com 2019 - 2024. All rights reserved.