botframework - Microsoft Teams:获取用户的时区?
问题描述
我正在为 MS Teams 开发一个机器人,我希望了解用户的时区,以便在适当的时间(例如,不在半夜)传递消息。
我没有在机器人框架 REST API 中找到合适的东西。虽然我们收到的消息包含一个“clientInfo.country”属性,这是一个开始,但绝对不足以按照我们的意愿对消息进行计时。
解决方案
来自@Savageman的回答
答案是:有一个 localTimestamp 属性可以用来获取时间偏移量,这对我的需要来说已经足够了。
我们可以通过将of和in"NOT Receive the timezone"
映射到来解决这个问题。utcOffset
localTimestamp
country
entities
timezone
我编写了一个 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
// ...
// ]
推荐阅读
- docker - Docker 容器中缺少 CV2
- github - 如何在 github 页面中添加代码翻转器(类似标签)
- azure-active-directory - PublicClientApplication 构建器在部署到 IIS 后不工作
- android - 将 Android 文件系统上的文件作为已签名的系统应用程序移动
- java - 应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,它们就可用
- postgresql - 如何指定 postgresql 转换为 UUID 的错误行为
- android - ViewCompat.setOnApplyWindowInsetsListener() 改变系统导航栏的背景
- javascript - 使用类名使用 svg 文件
- arm - Pyinstaller 在构建引导加载程序时找不到目标架构
- database - 修改TDengine表结构