Microsoft Teams:获取用户的时区?
Microsoft Teams: get timezone of user?
我正在为 MS Teams 开发一个机器人,我想知道用户的时区,以便在适当的时间(例如,不是在半夜)传递消息。
我没有在机器人框架 REST 中找到合适的东西 API。虽然我们收到的消息中包含一个'clientInfo.country'属性,这是一个开始,但绝对不足以让消息按我们想要的时间显示。
在发给用户的每条消息中,都有一个 entities[]
集合,其中之一是用户区域设置的详细信息。例如(copied/pasted 来自 here):
"entities": [
{
"locale": "en-US",
"country": "US",
"platform": "Windows",
"timezone": "America/Los_Angeles",
"type": "clientInfo"
}
],
答案是:有一个localTimestamp
属性可以用来获取时间偏移量,这足以满足我的需要。
来自@Savageman 's
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.
我们可以通过将 localTimestamp
中的 utcOffset
和 entities
中的 country
映射到 timezone
来解决问题 "NOT Receive the timezone"
。
我写了一个 javascript 代码来获取诸如 "Asia/shanghai"
的时区,方法是使用 Teams 消息中 Session
的 "localTimestamp": "2019-08-06T18:23:44.259+08:00"
和 "country": "CN"
。
我的 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
// ...
// ]
我正在为 MS Teams 开发一个机器人,我想知道用户的时区,以便在适当的时间(例如,不是在半夜)传递消息。
我没有在机器人框架 REST 中找到合适的东西 API。虽然我们收到的消息中包含一个'clientInfo.country'属性,这是一个开始,但绝对不足以让消息按我们想要的时间显示。
在发给用户的每条消息中,都有一个 entities[]
集合,其中之一是用户区域设置的详细信息。例如(copied/pasted 来自 here):
"entities": [
{
"locale": "en-US",
"country": "US",
"platform": "Windows",
"timezone": "America/Los_Angeles",
"type": "clientInfo"
}
],
答案是:有一个localTimestamp
属性可以用来获取时间偏移量,这足以满足我的需要。
来自@Savageman 's
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.
我们可以通过将 localTimestamp
中的 utcOffset
和 entities
中的 country
映射到 timezone
来解决问题 "NOT Receive the timezone"
。
我写了一个 javascript 代码来获取诸如 "Asia/shanghai"
的时区,方法是使用 Teams 消息中 Session
的 "localTimestamp": "2019-08-06T18:23:44.259+08:00"
和 "country": "CN"
。
我的 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
// ...
// ]