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

时间:2018-06-06 07:39:10

标签: node.js datetime timezone alexa alexa-skills-kit

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

任何帮助都会非常明显吗?

3 个答案:

答案 0 :(得分:3)

是的,您可以使用本机的Alexa API。这是您要寻找的完美解决方案。您将需要一个设备ID和一个API访问令牌。此外,axios(npm i axios)和zip-to-country-code(npm i邮政编码到时区)之类的工具很少,请点击此处Enhance Your Skill With Address Information。此外,在实施此代码之前,请确保先转到Alexa开发人员门户并打开权限,请参见下图。干杯! enter image description here

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

答案 1 :(得分:3)

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

您将感兴趣的端点如下:

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

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

答案 2 :(得分:1)

如果使用的是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);
    }
}