6

您好,我已使用此https://docs.rocket.chat/api/rest-api/methods/channels/history通过 rest API 获取未读消息。

示例调用

  1. ( https://rcserver.rocket.chat/api/v1/im.history?roomId=ByehQjC44FwMeiLbX?&unreads=true )

  2. ( https://rcserver.rocket.chat/api/v1/im.history?roomId=ByehQjC44FwMeiLbX?&unreads=”+ true)

代码

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Constants.CONST_SITEURL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Auth-Token", authToken);
client.DefaultRequestHeaders.Add("X-User-Id", userRcId);
HttpResponseMessage msgHistory = client.GetAsync(Constants.CONST_CHATHISTORY + userDetail.RC_RoomID + "&count=20&unreads=true").Result;
     if (msgHistory.IsSuccessStatusCode)
         {
           using (HttpContent content = msgHistory.Content)
                   {
                      var result = content.ReadAsStringAsync();
                      value = JObject.Parse(result.Result);
                    }
           directChatWindow = JsonConvert.DeserializeObject<DirectChatWindowBO>(value.ToString());
         }

我尝试了上面的链接和代码,但它不会在结果中给出任何未读属性

示例结果

{
"messages": [
 {
  "_id": "7e6691fc-16sdfd3-ecbfsd8-317a-4076bb307e5dfsfd-4564",
  "rid": "CBsDHB7M8fsdfsdfN8G4X2BjsBDt5khnkenENacLN",
  "msg": "hittti",
  "ts": "2017-08-16T11:08:21.011Z",
  "u": {
    "_id": "CBsDHsdadsaB7M8N8G4X2Bj",
    "username": "xyz",
    "name": "xyz21"
  },
  "mentions": [],
  "channels": [],
  "_updatedAt": "2017-08-16T11:08:21.013Z"
},
{
  "_id": "eaf75056-bcxcvxcv40c-4a68-0128-c40503289d60",
  "rid": "CBsDHxcvB7M8cvxvxcvN8G4X2BjsBDt5kxcvhnkenENacLN",
  "msg": "hi",
  "ts": "2017-08-16T11:07:53.579Z",
  "u": {
    "_id": "CBsDHB7M8N8G4X2Bj",
    "username": "Abc",
    "name": "Abc123 "
  },
  "mentions": [],
  "channels": [],
  "_updatedAt": "2017-08-16T11:07:53.583Z"
}]
}

请帮帮我。谢谢你。

4

2 回答 2

9

作为 Rocket.Chat REST API 的维护者,您实际上让我们注意到了一个已经存在很长时间的错误(自从我们从咖啡脚本转换以来)。我已经提交了一个修复此问题的拉取请求,但是要让未读内容出现,它确实需要更改您使用im.history端点的方式。

要让未读数据出现,您还必须传入查询参数,oldest该参数是一个可以成功转换为 JavaScript Date 对象的字符串,Date.parse() 详情请参阅文档

示例查询 url 如下所示:

http://localhost:3000/api/v1/channels.history?roomName=general&unreads=true&oldest=2017-01-01

然后包含未读信息的成功响应将如下所示:

{
    "messages": [
        {
            "_id": "pwiJmc7ZfEwebMEKP",
            "alias": "",
            "msg": "hello ;) ;)",
            "attachments": null,
            "parseUrls": true,
            "bot": null,
            "groupable": false,
            "ts": "2017-08-18T08:27:26.746Z",
            "u": {
                "_id": "HL2hEQSGask47a82K",
                "username": "graywolf336",
                "name": "graywolf336"
            },
            "rid": "GENERAL",
            "mentions": [],
            "channels": [],
            "_updatedAt": "2017-08-18T08:27:26.749Z"
        },
        {
            "_id": "YRch8iRsur7L6WF5B",
            "alias": "",
            "msg": "hello world",
            "attachments": null,
            "parseUrls": true,
            "bot": null,
            "groupable": false,
            "ts": "2017-08-18T08:21:50.072Z",
            "u": {
                "_id": "HL2hEQSGask47a82K",
                "username": "graywolf336",
                "name": "graywolf336"
            },
            "rid": "GENERAL",
            "mentions": [],
            "channels": [],
            "_updatedAt": "2017-08-18T08:21:50.073Z"
        }
    ],
    "firstUnread": {
        "_id": "3gJZbwLia6tuznPTk",
        "t": "uj",
        "rid": "GENERAL",
        "ts": "2017-07-31T22:53:20.579Z",
        "msg": "graywolf336",
        "u": {
            "_id": "HL2hEQSGask47a82K",
            "username": "graywolf336"
        },
        "groupable": false,
        "_updatedAt": "2017-07-31T22:53:20.579Z"
    },
    "unreadNotLoaded": 259,
    "success": true
}

自 2017 年 8 月 8 日起,在合并拉取请求之前,这将不起作用。但是,在合并之后,这将与开发版本一起使用,一旦0.59Rocket.Chat 版本发布,您就可以更新您的服务器并使用它。

希望这会有所帮助,如果您有任何问题,请告诉我,我会更新我的答案。

免责声明:我是 Rocket.Chat 的员工,我维护 REST API 代码。

于 2017-08-18T09:05:51.597 回答
0

您可以在 php 中使用它

$unread = 0;

    $lists = apicall('im.list'); 
    foreach($lists->ims as $list){ 
        $data = apicall('im.counters?username='.$list->_id);

        $unread += $data->unreads;

    }

    $lists = apicall('channels.list'); 
    foreach($lists->channels as $list){ 
        $data = apicall('channels.counters?roomId='.$list->_id);

        $unread += $data->unreads;

    }

    echo $unread;



function apicall($url){
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://shsdatabase.rocket.chat/api/v1/'.$url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'X-Auth-Token: '.$_SESSION['rocket_token'],
        'X-User-Id: '.$_SESSION['rocket_user_id']
      ),
    ));
    
    $response = json_decode(curl_exec($curl));
    
    curl_close($curl);
    
    return $response;

}
于 2021-04-30T14:16:49.617 回答