Я использую AMAZON.RepeatIntent, чтобы повторить намерение о высказывании пользователя.Однако в обработчике намерений AMAZON.RepeatIntent я обновляю атрибуты сеанса, которые не обновляются, как если бы сеанс перешел в состояние до вызова AMAZON.RepeatIntent.Я также отслеживаю историю намерений через перехватчик запросов, который правильно сохраняет AMAZON.RepeatIntent до вызова обработчика, а затем после того, как вызов будет удален.
Я делегирую правильное намерение, которое хочу повторить, но нетспособ узнать (без атрибутов сеанса, которые не работают правильно), если намерение было вызвано в первый раз или это повторное намерение.
Это обработчик AMAZON.RepeatIntent
const utils = require('../../utils/utils');
const Paginator = require('../../classes/Paginator');
const RepeatIntent_Handler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'AMAZON.RepeatIntent';
},
handle(handlerInput) {
const { responseBuilder, attributesManager } = handlerInput;
const lastCustomIntent = utils.getCustomIntentsOnly(handlerInput, true);
utils.stringifyLog('RepeatIntent_Handler lastCustomIntent', lastCustomIntent);
if (!lastCustomIntent) {
return responseBuilder
.speak('No custom intents to repeat.')
.getResponse()
}
const intent = {
name: lastCustomIntent.IntentRequest,
confirmationStatus: 'NONE'
}
if (lastCustomIntent.slots) {
intent.slots = lastCustomIntent.slots;
}
utils.stringifyLog('RepeatIntent_Handler lastCustomIntent after', lastCustomIntent);
if(lastCustomIntent.IntentRequest === 'GetCategoriesIntent' || lastCustomIntent.IntentRequest === 'GetEntitiesIntent') {
const sessionAttrs = attributesManager.getSessionAttributes();
sessionAttrs.paginator.currentPage = Paginator.getPreviousPage(sessionAttrs.paginator);
attributesManager.setSessionAttributes(sessionAttrs);
}
return responseBuilder
.speak('Okay I\'ll repeat that for you.')
.addDelegateDirective(intent)
.withShouldEndSession(false)
.getResponse();
}
};
Это перехватчик запросов
const RequestHistoryInterceptor = {
process(handlerInput) {
const thisRequest = handlerInput.requestEnvelope.request;
const sessionAttrs = handlerInput.attributesManager.getSessionAttributes();
const history = sessionAttrs.history || [];
let intent = {};
if (thisRequest.type === 'IntentRequest') {
intent = {
IntentRequest: thisRequest.intent.name
};
const intentSlots = thisRequest.intent.slots;
if (intentSlots) {
const slots = {};
for (const slot in intentSlots) {
if (intentSlots.hasOwnProperty(slot)) {
slots[slot] = {...intentSlots[slot]};
}
}
intent = {
IntentRequest: thisRequest.intent.name,
slots: Object.keys(slots).length > 0 ? slots : null
};
}
} else {
intent = {
IntentRequest: thisRequest.type
};
}
if (history.length > maxHistorySize - 1) {
history.shift();
}
history.push(intent);
sessionAttrs.history = history;
handlerInput.attributesManager.setSessionAttributes(sessionAttrs);
}
};