How to get the time zone or local time of Alexa device
Asked Answered
O

3

5

I want to get the "time zone" set in the settings or local date time of the Alexa device. Is there any API available for it? Or is there any option to get the date-time of the user using his postal code?

Any help will be highly appreciable?

Orfinger answered 6/6, 2018 at 7:39 Comment(4)
which language are you using for your lambda function?Losel
There is a UserVoice item on this. Have you seen it?Kranz
Gone thru this, it didn't work. My lambda is in node js.Orfinger
Possible duplicate of How do I get a user's date/time or timezone information for an Alexa Skill?Riggs
H
4

Yes, there is a native Alexa API that you can use. Here is a perfect solution for what you're looking for. What you will need is a device ID and an API access token. Also, few tools like axios ( npm i axios) and zip-to-country-code (npm i zipcode-to-timezone) more info here Enhance Your Skill With Address Information Also, before you implement this code make sure to go to Alexa dev portal and turn on permissions See image below. Cheers! enter image description here

            const apiAccessToken = this.event.context.System.apiAccessToken;
            const deviceId = this.event.context.System.device.deviceId;
            let countryCode = '';
            let postalCode = '';

            axios.get(`https://api.amazonalexa.com/v1/devices/${deviceId}/settings/address/countryAndPostalCode`, {
              headers: { 'Authorization': `Bearer ${apiAccessToken}` }
            })
            .then((response) => {
                countryCode = response.data.countryCode;
                postalCode = response.data.postalCode;
                const tz = ziptz.lookup( postalCode );
                const currDate = new moment();
                const userDatetime = currDate.tz(tz).format('YYYY-MM-DD HH:mm');
                console.log('Local Timezone Date/Time::::::: ', userDatetime);
            })
Hickie answered 6/8, 2018 at 15:28 Comment(0)
F
5

It is now possible to get a user's timezone and other related data using the Alexa Settings API. Also see the related blogpost for more information on this API release.

The endpoint you'll be interested in is the following:

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

You simply need to provide the user's device ID, which is part of the received intent. The response will contain a timezone name, for instance "Europe/London".

Flotage answered 19/10, 2018 at 18:35 Comment(0)
H
4

Yes, there is a native Alexa API that you can use. Here is a perfect solution for what you're looking for. What you will need is a device ID and an API access token. Also, few tools like axios ( npm i axios) and zip-to-country-code (npm i zipcode-to-timezone) more info here Enhance Your Skill With Address Information Also, before you implement this code make sure to go to Alexa dev portal and turn on permissions See image below. Cheers! enter image description here

            const apiAccessToken = this.event.context.System.apiAccessToken;
            const deviceId = this.event.context.System.device.deviceId;
            let countryCode = '';
            let postalCode = '';

            axios.get(`https://api.amazonalexa.com/v1/devices/${deviceId}/settings/address/countryAndPostalCode`, {
              headers: { 'Authorization': `Bearer ${apiAccessToken}` }
            })
            .then((response) => {
                countryCode = response.data.countryCode;
                postalCode = response.data.postalCode;
                const tz = ziptz.lookup( postalCode );
                const currDate = new moment();
                const userDatetime = currDate.tz(tz).format('YYYY-MM-DD HH:mm');
                console.log('Local Timezone Date/Time::::::: ', userDatetime);
            })
Hickie answered 6/8, 2018 at 15:28 Comment(0)
W
4

If you are using ASK sdk v2. There's a better way to get the timezone.

const getCurrentDate = async (handlerInput) => {
    const serviceClientFactory = handlerInput.serviceClientFactory;
    const deviceId = handlerInput.requestEnvelope.context.System.device.deviceId;

    try {
        const upsServiceClient = serviceClientFactory.getUpsServiceClient();
        return userTimeZone = await upsServiceClient.getSystemTimeZone(deviceId);   
    } catch (error) {
        if (error.name !== 'ServiceError') {
            return handlerInput.responseBuilder.speak("There was a problem connecting to the service.").getResponse();
        }
        console.log('error', error.message);
    }
}
Wot answered 6/9, 2019 at 2:59 Comment(1)
^ This works easily. You can also get it from the Cake Time example that Amazon provides. Don't forget to add this line at the top of your exports.handler: .withApiClient(new Alexa.DefaultApiClient())Switchman

© 2022 - 2024 — McMap. All rights reserved.