How to get unread message via rest api in Rocket.chat

爷,独闯天下 提交于 2019-12-05 12:34:48

As maintainer of the Rocket.Chat REST API, you actually brought to our attention a bug which has been present for a long time now (since we converted from coffeescript). I have submitted a pull request which fixes this issue, however to get the unreads to appear it does require changing how you use the im.history endpoint.

To get the unreads to appear you must also pass in the query parameter of oldest which is a string that can successfully be converted to a JavaScript Date object, see the Date.parse() documentation for details.

An example query url would look like:

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

Then a successful response which includes the unread information will look like the following:

{
    "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
}

As of August 8, 2017, this will not work until the pull request is merged. However after it is merged then this will work with development builds and once version 0.59 of Rocket.Chat is released then you can update your server and make usage of it.

Hopefully this helps, let me know if you have any questions and I will update my answer.

Disclaimer: I am an employee of Rocket.Chat and I do maintain the REST API code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!