alexa/alexa-skills-kit-sdk-for-python

handler_input.request_envelope.request.timestamp returns a datetime.datetime object instead of a string.

cmdruid opened this issue · 2 comments

I'm submitting a...


[x ] Bug report  

Expected Behavior

According to the documentation, the timestamp should be in a string format, like all other timestamps. Please see link: https://alexa-skills-kit-python-sdk.readthedocs.io/en/latest/models/ask_sdk_model.html#ask_sdk_model.request.Request

Current Behavior

When I receive "handler_input.request_envelope.request.timestamp", I get a datetime.datetime object instead of a string. I don't know if it's a real datetime object because python breaks if I treat it as one.

Possible Solution

If this is a bug then I'm sure it's an easy fix. Unfortunately I don't have time to hunt it down right now but I will go back and look for it later.

Steps to Reproduce (for bugs)

You should be able to paste the code under "def handle" into a working skill template and get the same results. FYI I'm running a development server that communicates with Alexa API through Flask and HTTPS. I'm not hosting on the lambda service. I don't know if that makes a difference in the responses. When I have more time I will test that as well!

from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.skill_builder import CustomSkillBuilder
from ask_sdk_core.api_client import DefaultApiClient
from ask_sdk_core.utils import is_request_type

class LaunchRequestHandler(AbstractRequestHandler):
    """ Initial handler for skill launch. """
    def can_handle(self, handler_input):
        return is_request_type("LaunchRequest")(handler_input)

    def handle(self, handler_input):
        timestamp = handler_input.request_envelope.request.timestamp
        prompt = timestamp if type(timestamp) is str else "Your timestamp is not a string!"
        handler_input.response_builder.speak(prompt).ask("Is that correct?")
        return handler_input.response_builder.response

sb = CustomSkillBuilder(api_client=DefaultApiClient())
sb.add_request_handler(LaunchRequestHandler())

Context

If this behavior is intentional then my apologies. But it seems unintentional since all other timestamps in the response are strings. And it's much easier to work with strings.

Your Environment

  • ASK SDK for Python used: latest (v1.11.0)
  • Operating System and version: Ubuntu 18.04.3

Python version info

  • Python version used for development: 3.6

Hey @cmdruid , thanks for raising this. However, this seems to be a minor documentation issue and you can see in the Request class that it is actually a datetime object. We deserialize the value from date-time string to the python formatted date-time object so that it is easier to do date-time processing and logic with it.

I don't know if it's a real datetime object because python breaks if I treat it as one.

Not sure what you mean by this. What breaking behavior are you observing?

But it seems unintentional since all other timestamps in the response are strings. And it's much easier to work with strings.

Can you mention which response values are strings and not date-times? That might be a bug in the code. It is intentional to make all timestamps as python formatted date-time objects since it is idiomatic with the language and it is easier for SDK to convert them to ISO formatted strings as required by the service. Instead, if we make them strings, then it is left to developers to figure out the right format to send the value and it will only break during the skill run, which is not a good developer experience.

Hope this helps. Let us know if you have any other issues.

Closing this issue. Please reopen in case you still face problems.