question

Shane S. Volpe avatar image
Shane S. Volpe asked

Python APL lazy loading (dynamicIndexList)

I've been trying to get Lazy loading working using the Python SDK. I've referenced the following documentation (java script):

https://developer.amazon.com/en-US/blogs/alexa/alexa-skills-kit/2020/04/how-to-create-lazy-loading-lists-in-your-alexa-skill

I think where I'm having a problem is generating the correct response to the "Alexa.Presentation.APL.LoadIndexListData" request.

Here is the example from the above Java

    return handlerInput.responseBuilder
      .addDirective({
        type: "Alexa.Presentation.APL.SendIndexListData",
        token: TOKEN,
        correlationToken: requestObject.correlationToken,
        listId: requestObject.listId,
        startIndex: requestObject.startIndex,
        minimumInclusiveIndex: 0,
        maximumExclusiveIndex: colors.length,
        items: getColorsFromIndex(requestObject.startIndex, requestObject.count, colors)
      }).getResponse();

Here is my attempt to re-write it in python, where recipeSummeryData is my set of new data:

        return (
                handler_input.response_builder
                    .add_directive(
                    object_type="Alexa.Presentation.APL.SendIndexListData",
                    token="myRecipes",
                    correlation_token=requestObject.correlation_token,
                    document=_load_apl_document("./documents/launchDocument.json"),
                    list_id=requestObject.list_id,
                    startIndex=requestObject.start_index,
                    minimumInclusiveIndex=0,
                    maximumExclusiveIndex=len(recipeSummeryData),
                    items=recipeSummeryData
                    )
                .response
                )

When I run the above code it keeps failing saying all the variables I'm setting (object_type, token, correlation_token, list_ied, startIndex, etc...) don't exist. I tried using the naming convention of those variables shown in the javascript version too (correlationToken instead of correlation_token as an example). I'm assuming I'm messing up the response format in some manner. I've listed my complete LoadIndexListData handler below for completeness.

class LoadIndexListDataRequestHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        return is_request_type("Alexa.Presentation.APL.LoadIndexListData")(handler_input)
        
    def handle(self, handler_input):
        access_token = handler_input.request_envelope.context.system.user.access_token
        
        credentials = AccessTokenCredentials(access_token, 'alexa-skill/1.0')        
        requestObject = handler_input.request_envelope.request
        self.recipeDat = recipes(credentials)
        
        recipeSummeryData = self.recipeDat.GetRecipesDescFromFolder(requestObject.start_index, requestObject.start_index+ 5 - 1)
                
        handler_input.response_builder.add_directive(t
        
        return (
                handler_input.response_builder
                    .add_directive(
                    object_type="Alexa.Presentation.APL.SendIndexListData",
                    token="myRecipes",
                    correlation_token=requestObject.correlation_token,
                    document=_load_apl_document("./documents/launchDocument.json"),
                    list_id=requestObject.list_id,
                    startIndex=requestObject.start_index,
                    minimumInclusiveIndex=0,
                    maximumExclusiveIndex=len(recipeSummeryData),
                    items=recipeSummeryData
                    )
                .response
alexaaplalexa skills
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Shane S. Volpe avatar image
Shane S. Volpe answered

I've managed to figure out the right syntax to use for the sendIndexListDirective in python. It as follows:

        return (
                handler_input.response_builder
                    .add_directive(
                        SendIndexListDataDirective(
                            correlation_token = requestObject.correlation_token,
                            list_id = requestObject.list_id,
                            start_index = requestObject.start_index,
                            minimum_inclusive_index = 0,
                            items = recipeSummeryData
                        )
                    )
                .response
                )
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Shane S. Volpe avatar image
Shane S. Volpe answered

I just found a weird behavior. It only seems to call the LoadIndexListData handler 3 times and then it stops calling it even though there is more data to send. What would cause this? I read the following documentation:

SendIndexListData directive

And it states: maximumExclusiveIndex: Absence of this property indicates that the maximum index is not yet known and Alexa continues requesting more items as the user scrolls forwards

So I assumed if I left off maximumExclusiveIndex then it would keep sending the LoadIndexListData directive for more data but, again, it stops after 3 requests.

1 comment
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

It turns out this behavior of only loading 3 requests is only present on the simulator. On the device it loads all the data.
0 Likes 0 ·