How to get all message history from Hipchat for a room via the API?

烂漫一生 提交于 2019-12-03 17:09:28

I got this working but it was a big pain.

Start by sending a query with the current time, in UTC, but without including the time zone, as the start date:

https://internal-hipchat-server/v2/room/2/history?reverse=false&date=2015-06-25T20:42:18.658439&max-results=1000&auth_token=XXX

This is very fiddly:

  • If you specify just the current date, without a timezone, as documented in the API, it is interpreted as midnight last night and you only get messages from yesterday or older.
  • If you try specifying tomorrow’s date instead, the response is 400 Bad Request This day has not yet come to pass.
  • If you specify the time as 2015-06-25T20:42:18.658439+00:00, which is the format that times come in HipChat API responses, HipChat’s parser seems to fail and interpret it as midnight last night.

When you get the response back, take the oldest items.date property, strip the timezone, and resubmit the above URL with an updated date parameter:

https://internal-hipchat-server/v2/room/2/history?reverse=false&date=2015-06-17T19:56:34.533182&max-results=1000&auth_token=XXX

Be sure to include the microseconds, in case a notification posted multiple messages to the same room in the same second.

This will get you the next page of messages. Keep doing this until you get fewer than max-results messages back.

There is a start-index parameter I tried passing before I got the above working, and it will give you a few pages of results, with responses lacking a links.next property, but it won’t give you the full history. On a chatroom with 9166 messages in the history according to statistics.messages_sent, it only returned 3217 messages. So don’t use it. You can use statistics.messages_sent as a sanity check for whether you get all messages.

Oh yeah, and the last_active property in the /v2/room call cannot be trusted because it doesn’t update when notification messages are posted to the room.

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