Search code examples
alexa-skills-kit

How to do something as soon as Skill is created?


In my Alexa Skill i set dynamic entities in 'addRequestInterceptors' as the entities must be available as soon as possible, the issue is that if the user says 'Alexa, open SKILLNAME' the entities are initialized correctly, but if the user instead says 'Alexa, ask SKILLNAME where is DYNAMICENTITY' at this point the skill fails as the DYNAMICENTITY is not yet set.

So my question is, how can i set my dynamic entities as soon as possible like when the skill is created? I've tryed the LaunchIntent but as in the case above it's avoided if the user make the question inside the invocation..

So LaunchIntent and addRequestInterceptors aren't good to do it..

Here is my code which i should run as soon as possible:

const LoadListaPuntiVendita = {
    async process(handlerInput) {
        const {attributesManager, requestEnvelope} = handlerInput;
        const sessionAttributes = attributesManager.getSessionAttributes();
        const attributes = await attributesManager.getPersistentAttributes() || {};
        const piva =  attributes.hasOwnProperty('piva') ? attributes.piva : null;
        sessionAttributes["piva"] = piva;
        if (!piva || piva === null) {
            return;
        }
        
        let negozi = sessionAttributes['negozi']
        if (!negozi) {
            const response = await logic.fetchNegozi(piva);
            sessionAttributes['negozi'] = response;
            addDynamicEntities(handlerInput.responseBuilder, 'PuntoVenditaType', response);
            }
        }
};

Solution

  • Here's one possible bug on first glance. This will only run as expected after you've had at least one exchange in which "piva" had been saved and set as a persistent value for the customer.

    If you're running this the moment the skill is initialized, it still must be run after the persistence adapter has been initialized and retrieved the value of "piva" from your store AND there must be have been a value stored for "piva" or the code returns early without setting the entity.