Alexa技能新的会话属性不会在AMAZON中持续存在。

时间:2019-07-11 12:21:04

标签: node.js amazon alexa alexa-skill alexa-intent

我正在使用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);
    }
};

0 个答案:

没有答案