How to get the time zone or local time of Alexa device

99封情书 提交于 2019-12-23 14:47:48

问题


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?


回答1:


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);
            })



回答2:


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".




回答3:


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);
    }
}


来源:https://stackoverflow.com/questions/50714782/how-to-get-the-time-zone-or-local-time-of-alexa-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!