Skip to content

Instantly share code, notes, and snippets.

@t04glovern
Created December 10, 2018 14:21
Show Gist options
  • Save t04glovern/8f7b2fa0baed3555a90ed7996fd555c2 to your computer and use it in GitHub Desktop.
Save t04glovern/8f7b2fa0baed3555a90ed7996fd555c2 to your computer and use it in GitHub Desktop.
getUserTimezone
function getUserTimezone(event, callback) {
if (event.context.System.apiAccessToken) {
// Invoke the entitlement API to load timezone
const options = {
host: 'api.amazonalexa.com',
path: '/v2/devices/' + event.context.System.device.deviceId + '/settings/System.timeZone',
method: 'GET',
timeout: 1000,
headers: {
'Content-Type': 'application/json',
'Accept-Language': event.request.locale,
'Authorization': 'bearer ' + event.context.System.apiAccessToken,
},
};
const req = https.get(options, (res) => {
let returnData = '';
res.setEncoding('utf8');
if (res.statusCode != 200) {
console.log('deviceTimezone returned status code ' + res.statusCode);
callback();
} else {
res.on('data', (chunk) => {
returnData += chunk;
});
res.on('end', () => {
// Strip quotes
const timezone = returnData.replace(/['"]+/g, '');
callback(moment.tz.zone(timezone) ? timezone : undefined);
});
}
});
req.on('error', (err) => {
console.log('Error calling user settings API: ' + err.message);
callback();
});
} else {
// No API token - no user timezone
callback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment