Ani*_*gya 2 alexa node.js aws-lambda alexa-skills-kit
我正在尝试通过以下代码将“技能购买”添加到我的Alexa技能中:
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === "LaunchRequest";
},
handle(handlerInput){
console.log("In LaunchRequest");
const locale = handlerInput.requestEnvelope.request.locale;
const ms = handlerInput.serviceClientFactory.getMonetizationServiceClient();
return ms.getInSkillProducts(locale).then(function(result) {
// Code to handle result.inSkillProducts goes here
const totalProducts = result.inSkillProducts.length;
const purchasableProducts = result.inSkillProducts.filter(record => record.purchasable == 'PURCHASABLE');
const entitledProducts = result.inSkillProducts.filter(record => record.entitled == 'ENTITLED');
return handlerInput.responseBuilder
.speak('Found total ' + result.inSkillProducts.length + ' products of which ' + purchasableProducts.length + ' are purchasable and ' + entitledProducts.length + ' are entitled.')
.getResponse();
});
},
};
Run Code Online (Sandbox Code Playgroud)
运行代码时,出现以下错误消息:
Response:
{
"errorMessage": "Cannot read property 'getMonetizationServiceClient' of undefined",
"errorType": "TypeError",
"stackTrace": [
"Object.handle (/var/task/index.js:16:50)",
"GenericHandlerAdapter.<anonymous> (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:63:47)",
"step (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:44:23)",
"Object.next (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:25:53)",
"/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:19:71",
"new Promise (<anonymous>)",
"__awaiter (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:15:12)",
"GenericHandlerAdapter.execute (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/request/handler/GenericHandlerAdapter.js:61:16)",
"GenericRequestDispatcher.<anonymous> (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/GenericRequestDispatcher.js:173:58)",
"step (/var/task/node_modules/ask-sdk-runtime/dist/dispatcher/GenericRequestDispatcher.js:44:23)"
]
}
Run Code Online (Sandbox Code Playgroud)
我使用了Alexa SDK站点中的代码,可以在以下位置找到:https : //ask-sdk-for-nodejs.readthedocs.io/en/latest/Calling-Alexa-Service-APIs.html#getinskillproducts。有人可以告诉我哪里出了问题或如何解决?提前致谢。
如果您共享您的出口部分,我也许可以提供进一步的帮助。如果您正在使用带有以下代码的自定义alexa技能生成器:
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers()
Run Code Online (Sandbox Code Playgroud)
要么
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers()
Run Code Online (Sandbox Code Playgroud)
那么您只需要添加
.withApiClient(new Alexa.DefaultApiClient())
例如,您的出口可能如下所示:
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
CancelResponseHandler,
LaunchRequestHandler,
HelloWorldIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.withApiClient(new Alexa.DefaultApiClient())
.lambda();`
Run Code Online (Sandbox Code Playgroud)
我从以下问题中找到了该解决方案:https : //github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/356
| 归档时间: |
|
| 查看次数: |
332 次 |
| 最近记录: |