Microsoft Teams: get timezone of user?

Viewed 1643

I'm developing a bot for MS Teams and I'm looking to know the timezone of a user, to deliver messages at an appropriate time (e.g. not in the middle of the night).

I didn't found something appropriate in the bot framework REST API. Although messages we receive include a 'clientInfo.country' property, which is a start, but definitely not enough to time messages as we would like.

3 Answers

On every message to a user, there is an entities[] collection, one of which is details of the user's locale. For example (copied/pasted from here):

"entities": [
  { 
    "locale": "en-US",
    "country": "US",
    "platform": "Windows",
    "timezone": "America/Los_Angeles",
    "type": "clientInfo"
  }
],

And the answer is: there’s a localTimestamp property that can be used to get the time offset, which is good enough for what I need.

From @Savageman 's answer

And the answer is: there’s a localTimestamp property that can be used to get the time offset, which is good enough for what I need.

We can solve the issue "NOT Receive the timezone" by mapping the utcOffset of localTimestamp and country in entities to timezone.

I wrote a javascript code to get timezone such as "Asia/shanghai" by using the "localTimestamp": "2019-08-06T18:23:44.259+08:00" and "country": "CN" from Session in Teams message.

More details in my 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
//      ...
// ]
Related