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

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

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

任何帮助将不胜感激?

是的,您可以使用原生 Alexa API。这是您正在寻找的完美解决方案。您需要的是设备 ID 和 API 访问令牌。此外,很少有像 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);
            })

现在可以使用 Alexa Settings API. Also see the related blogpost 获取用户的时区和其他相关数据,以获取有关此 API 版本的更多信息。

您感兴趣的端点如下:

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

您只需提供用户的设备 ID,这是接收到的意图的一部分。响应将包含时区名称,例如 "Europe/London".

如果您使用的是 ASK sdk v2。有一个更好的方法来获取时区。

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