首页 > 解决方案 > Microsoft Teams:获取用户的时区?

问题描述

我正在为 MS Teams 开发一个机器人,我希望了解用户的时区,以便在适当的时间(例如,不在半夜)传递消息。

我没有在机器人框架 REST API 中找到合适的东西。虽然我们收到的消息包含一个“clientInfo.country”属性,这是一个开始,但绝对不足以按照我们的意愿对消息进行计时。

标签: botframeworkmicrosoft-graph-apimicrosoft-teams

解决方案


来自@Savageman回答

答案是:有一个 localTimestamp 属性可以用来获取时间偏移量,这对我的需要来说已经足够了。

我们可以通过将of和in"NOT Receive the timezone"映射到来解决这个问题。utcOffsetlocalTimestampcountryentitiestimezone

我编写了一个 javascript 代码来获取时区,例如在 Teams 消息中"Asia/shanghai"使用"localTimestamp": "2019-08-06T18:23:44.259+08:00"and "country": "CN"from 。Session

更多细节在我的 github readme.md中。

let moment = require("moment-timezone");
let ct = require("countries-and-timezones");

let partOfSampleSession = {
    "message": {
        "entities": [
            {
                "country": "CN",
                "locale": "zh-CN",
                "platform": "Web",
                "type": "clientInfo"
            }
        ],
        "localTimestamp": "2019-08-06T18:23:44.259+08:00"
    }
}

function getTimezoneFromSession(session) {
    // Get the name of country, such as "CN", "JP", "US"
    let country = session.message.entities[0].country;

    // Get the localTimestamp from message in session, such as "2019-08-06T18:23:44.259+08:00"
    let localTimestamp = session.message.localTimestamp;

    // Caculate the utfOffset of "localTimestamp", such as "480" by "2019-08-06T18:23:44.259+08:00"
    let utcOffsetOfLocalTime = moment().utcOffset(localTimestamp).utcOffset();

    // Mapping country to an object array which contains utcOffsets and it's corresponding timezones
    // One element from mxTimezones is {"utcOffset": "480", "name": "Asia/Shanghai"}
    let mxTimezones = ct.getTimezonesForCountry(country);

    // get the same timezone as localtime utcOffset from timezones in a country
    let timezone = "";
    mxTimezones.forEach(mxTimezone => {
        if (mxTimezone.utcOffset == utcOffsetOfLocalTime) {
            timezone = mxTimezone.name;
        }
    });
    return timezone;
}
let timezone = getTimezoneFromSession(partOfSampleSession);
// timezone = "Asia/Shanghai"
console.log(timezone);


// example of ct.getTimezonesForCountry("US")
// mxTimezones = [
//      {
//          "name": "America/New_York",
//          "utcOffset": "-300",
//      },
//      {
//          "name": "America/Los_Angeles",
//          "utcOffset": "-480",
//      }
//      ...
//      27 elements
//      ...
// ]

推荐阅读