【问题标题】:Alexa.getRequestType is not a function in AWS lambda alexa skillsAlexa.getRequestType 不是 AWS lambda Alexa 技能中的函数
【发布时间】:2020-02-19 01:43:22
【问题描述】:

我是 Alexa Skills 的新手,刚刚完成了 Alexa Skill Interaction Model 和 AWS 上的 Lambda 函数的连接。

我在 AWS lambda 开发者控制台上设置了一个简单的 node.js 程序,我正在使用他们的测试功能在 AWS 控制台上对其进行测试。但我不明白为什么这给了我一个错误。另外,我从示例存储库创建了这个项目 - serverlessrepo-alexa-skil-alexaskillskitnodejsfact

Response:
{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "SSML",
      "ssml": "<speak>Sorry, I had trouble doing what you asked. Please try again.</speak>"
    },
    "reprompt": {
      "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak>Sorry, I had trouble doing what you asked. Please try again.</speak>"
      }
    },
    "shouldEndSession": false
  },
  "userAgent": "ask-node/2.0.0 Node/v8.10.0",
  "sessionAttributes": {}
}

Request ID:
"18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1"

Function Logs:
START RequestId: 18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1 Version: $LATEST
2019-10-22T20:44:11.141Z    18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1    ~~~~ Error handled: TypeError: Alexa.getRequestType is not a function
    at Object.canHandle (/var/task/index.js:8:22)
    at DefaultRequestMapper.<anonymous> (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:74:61)
    at step (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:44:23)
    at Object.next (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:25:53)
    at /var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:19:71
    at new Promise (<anonymous>)
    at __awaiter (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:15:12)
    at DefaultRequestMapper.getRequestHandlerChain (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:63:16)
    at DefaultRequestDispatcher.<anonymous> (/var/task/node_modules/ask-sdk-core/lib/dispatcher/DefaultRequestDispatcher.js:104:60)
    at step (/var/task/node_modules/ask-sdk-core/lib/dispatcher/DefaultRequestDispatcher.js:44:23)
END RequestId: 18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1
REPORT RequestId: 18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1  Duration: 376.64 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 73 MB  Init Duration: 209.77 ms

代码如下:

/* eslint-disable  no-console */

const Alexa = require('ask-sdk');

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Hello! Welcome to Happy Expresso. Which Expresso type are you in the mood for?';
        const repromptText = 'I like Latte. What do you prefer?';    
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(repromptText)
            .getResponse();
    }
};

const CaptureCoffeeIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'CaptureCoffeeIntent';
    },
    handle(handlerInput) {
        const coffee = handlerInput.requestEnvelope.request.intent.slots.coffee.value;

        const speakOutput = `Thanks, I'll find a cafe nearby that has the best ${coffee}!`;
        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};
const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'You can say hello to me! How can I help?';

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.getResponse();
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        CaptureCoffeeIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();

看起来 Alexa.getRequestType 没有定义。在这个过程中我有什么遗漏吗?

谢谢!

【问题讨论】:

    标签: aws-lambda alexa-skills-kit


    【解决方案1】:

    我查看了代码,您似乎包含了 ask-sdk 包而不是 ask-sdk-core

    还要确保您使用的是这两个软件包的最新版本。这是来自示例 Hello World 存储库的 package.json file。您也可以使用code base here

    除此之外,我为 NodeJS 事实技能使用了无服务器包,它不使用 Alexa.getRequestType(handlerInput.requestEnvelope),而是像 handlerInput.requestEnvelope.request.type 一样处理请求。您能否确认您使用了哪个 Serverless 资源?

    【讨论】:

    • 感谢您指出无服务器软件包问题。就是这样!我从 Alexa 开发者控制台复制了我的 node.js 代码,结果发现这里的包不起作用。
    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2019-03-15
    • 2017-07-15
    • 2019-12-03
    相关资源
    最近更新 更多