我正尝试使用以下代码将“技能购买”添加到我的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();
});
},
};
运行代码时,出现以下错误消息:
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)"
]
}
我使用了Alexa SDK网站上的代码,可以在这里找到https://ask-sdk-for-nodejs.readthedocs.io/en/latest/Calling-Alexa-Service-APIs.html#getinskillproducts。有人可以告诉我出了什么问题或如何解决吗?预先感谢。
答案 0 :(得分:3)
如果您共享您的出口部分,我也许可以提供进一步的帮助。如果您正在使用带有以下代码的自定义alexa技能生成器:
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers()
或
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers()
然后您只需要添加
.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();`
我从以下问题中找到了该解决方案:https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/356