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?
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".
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!
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);
})
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With