如何获取Alexa设备的时区或本地时间

Ana*_*ngh 5 timezone datetime alexa node.js alexa-skills-kit

我想在 Alexa 设备的设置或本地日期时间中设置“时区” 。有没有可用的API?或者是否有任何选项可以使用他的邮政编码获取用户的日期时间?

任何帮助将是非常可观的?

Sai*_*lah 5

现在可以使用Alexa Settings API获取用户的时区和其他相关数据。另请参阅相关博文,了解有关此 API 版本的更多信息。

您将感兴趣的端点如下:

GET /v2/devices/{deviceId}/settings/System.timeZone
Run Code Online (Sandbox Code Playgroud)

您只需要提供用户的设备 ID,它是接收到的意图的一部分。响应将包含时区名称,例如“欧洲/伦敦”。


Ato*_*m23 4

是的,您可以使用原生 Alexa API。这是您正在寻找的完美解决方案。您需要的是设备 ID 和 API 访问令牌。此外,还有一些工具,如axios ( npm i axios) 和zip-to-country-code (npm i zipcode-to-timezone) 更多信息,请参阅此处通过地址信息增强您的技能另外,在实现此代码之前,请确保转到 Alexa开发门户并打开权限 请参见下图。干杯!在此输入图像描述

            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);
            })
Run Code Online (Sandbox Code Playgroud)