Getting the user's time zone in your skill

In this quick tutorial we will see how to get the user’s time zone using the Alexa Settings API and the code block in Voiceflow.

Let’s start by creating a new project and add a code block just after the home / start block.

In this code block, we will add these three lines of code:

deviceId = _system.device.deviceId;
apiAccessToken = _system.apiAccessToken;
apiEndpoint = _system.apiEndpoint;

Here we use the _system variable to get the device ID, a token to authenticate us and the API endpoint.
These three pieces of information will be necessary to be able to make a call to the Alexa Settings API.

As we will use the deviceId, apiAccessToken and apiEndpoint variables outside the code block, we will now create them in the Voiceflow variables section.

Once these variables are created, we will add an Integrations block just after our code block.

In the Integration block, select API as integration and I want to Make a GET Request.

In With settings -> URL, add the following line:

{apiEndpoint}/v2/devices/{deviceId}/settings/System.timeZone

You NEED to WRITE this line to enable the variables (in blue).

In the Headers section, add a pair to authenticate the request.

Authorization: Bearer {apiAccessToken}

Before mapping the result of our API call, we will create a new userTimezone variable

Return to the Integration block, and complete the configuration by mapping the response to the userTimezone variable.

For this last part of the tutorial, we will use the user’s time zone that we have obtained thanks to our Integration block to generate a corresponding date and time.

Let’s add the last two variables of this tutorial in the Variables section of Voiceflow, theTime and theDate.

In the meantime, add a Code block and a Speak block just after the Integration block.

Here is the code we will use to obtain the date and time:

theTime = new Date().toLocaleTimeString(locale, {hour: '2-digit', minute: '2-digit', timeZone: userTimezone});
theDate = new Date().toLocaleDateString(locale, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZone: userTimezone});

Here we use the Voiceflow’s locale variable to configure our two functions .toLocaleTimeString and .toLocaleDateString.
This way, you will get a 12h format with AM/PM if the locale variable is equal to en-US or 24h format if the locale is equal to fr-FR for example.
Same thing with the use of timeZone: userTimezone which allows us to obtain the time and date corresponding to the user’s time zone.

Finally, in the speak block, let’s add our two variables, theDate and theTime.

Well done, you can now test your skill with the ADC (Alexa Developer Console) simulator or directly on a device.

And the good news is, and I quote Bill Mccarthy who found and tested this:

“if you do this, even though the text you get says ‘May 15, 2019’, Alexa adds the ‘th’ after 15. ‘She’ says ‘15th’ without us having to change the number from cardinal to ordinal!!”

1 Like