Search code examples
htmlalexa-skills-kitask-sdk

Reduce the size of an base64 string in HTML payload


I am making use of the Amazon python ask-sdk package. It makes use of images similar to HTML, where you can use an image URL or a base64 string like this:

{
  "type": "Image",
  "source": <image_url> or "data:image/jpeg;base64,<base64_string>",
  "width": "30%",
  "height": "100%"
}

The issue is the images that have to be used are generated in real-time, so there is no option to store them as there will be way too many generated and stored as the number of requests increases. However, using base64 images makes the payload much larger than what can be processed on Alexa's end. So I was wondering if there is a more efficient way to send these images in the payload.

Since I am no expert in HTML, I'm not aware of the other format in which a file can be sent, or how to create them. I tried looking for byte arrays or hexstrings but had no success.

So any help is appreciated! Thanks in advance.


Solution

  • Since the images won't fit into a payload Alexa is willing to process, you'll need to send them out of band.

    It means that Alexa will issue a separate request to the image URL once it will have parsed the payload.

    It also means that the image will need to get stored somewhere in the meantime while Alexa parses the payload and requests the image.

    The easiest way is to store the images on S3, and set up the bucket to expire its objects after 1 day (the minimum value for this setting in S3). This will incur some storage costs, but this is easiest development-wise, because after you put an object to S3, you can easily generate a URL for Alexa to access it, complete with a temporary signature which can be set to expire within seconds.

    If the service that generates the images is stateful (i.e. runs as a long-lived process), you could add Flask or similar web server to it, and store the images in a hash map in memory, deleting them after they have been read.

    If the service is stateless (a bunch of lambdas behind an API gateway or similar), you can use an Elasticache instance for the purpose of temporary storage. The cheapest instance is included in the Free tier, but even if you pay for it, it would be something around $20 per month.