我尝试将名为“ 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中,我这样配置
测试工具可能无法正常工作吗?
测试界面如下:
答案 0 :(得分: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
上找到有关请求处理的更多信息答案 1 :(得分:0)
在您的lambda中,您的“可以处理”反映了
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'Pageviews';
}
但是您的Intent名称实际上是:
PageviewsIntent
您提供的屏幕快照测试事件不会调用您共享的代码。请仔细检查您的Intent名称和canHandle是否匹配。
如果您在Alexa开发人员控制台中更改了Intent名称,则需要确保保存并建立了Alexa技能。另外,请确保您的技能指向了正确的lambda版本,以消除任何混淆。