【问题标题】:Microsoft Teams: get timezone of user?Microsoft Teams:获取用户的时区?
【发布时间】:2020-11-06 21:55:35
【问题描述】:

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

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

【问题讨论】:

标签: botframework microsoft-graph-api microsoft-teams


【解决方案1】:

在给用户的每条消息中,都有一个entities[] 集合,其中之一是用户区域设置的详细信息。例如(从here复制/粘贴):

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

【讨论】:

  • 我确实收到了语言环境/国家/地区/平台/类型,但我没有收到时区...
  • 你能描述更多吗?您是如何接收消息的(或者等同于您在客户端上执行的操作以发送消息)以及来自哪个客户端?
  • 这是一款在Office商店发布的应用。我正在接收来自 webhook 的消息。我不是自己发送消息,而是从安装机器人的人那里接收消息。这是我收到的一个数据示例(没有时区):"entities":[{"locale":"nb-NO","country":"NO","platform":"iOS","type":"clientInfo"}] 这是另一个:"entities":[{"locale":"de-DE","country":"DE","platform":"Windows","type":" clientInfo"}] 我检查了其中的一些,但没有一个包含 timezone 属性。
  • Meta:Stack Overflow 不对代码块使用三个反引号。在代码前至少使用四个空格(并在块前使用换行符)。
  • 我们正在调查这个问题。目前,您还可以使用 localTimestamp 和时间戳值的偏移量。
【解决方案2】:

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

【讨论】:

  • localTimestamp 不是机器人运行的机器的时间吗?如果用户在+0400,bot服务器在-0800,localTimestamp的时区不是-0800,不是用户时区吗?
  • 我正在阅读用户消息,我不需要我自己的机器人的时间戳。我需要知道发布我正在阅读的消息的用户的时区。
  • 我知道这是我们正在寻找的用户时间戳。当我测试自己时,我使用 UTC+2 在机器 1 的 localhost 上运行我的机器人,然后我将机器 2 的时区更改为 -7 并向我的机器人发送消息。机器人使用 rawLocalTimestamp 和 localTimestamp 进行响应。 rawLocalTimestamp 似乎显示了正确的值,但是 localTimestamp 以 UTC+2 显示它。最重要的是,如果我的时区在团队开放时发生变化,我不会收到更新的时区,而是原始时区 (-7)。我在这里有什么遗漏吗?
【解决方案3】:

来自@Savagemananswer

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

我们可以通过将localTimestamp中的utcOffsetentities中的country映射到timezone来解决问题"NOT Receive the timezone"

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

更多细节在我的githubreadme.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
//      ...
// ]

【讨论】:

  • 这个解决方案的问题是,在用户向机器人发送内容之前,您无法获取时区。不幸的是,似乎没有更好的答案。我一直在寻找替代方案,可能会挂接到对话初始化事件或类似的东西,但我看到的有效负载都缺少用户的时区。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 2022-01-12
  • 2023-03-11
  • 2020-07-21
  • 1970-01-01
相关资源
最近更新 更多