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

I have been using the Hipchat API (v2) for a bit today and ran into an odd problem where I was unable to really pull up all the stories for a room. It seemed that when I gave a specific date, for example, it would only fetch part of the history for the specified date. I had plans to just iterate over all the dates for a room to extract the history in a format I could use, but ended up getting into this and now I'm not sure if it is really possible to deduce the history entirely.

I realize this is a little awkward. It pulls the JSON as a string, and then I have to form it into a hash, so I know I am not doing this as well as it could be, but here is roughly what I quickly did to test history

for the API:

api_token = "MY_TOKEN"

client = HipChat::Client.new(api_token, :api_version => 'v2')
history = client['ROOM_NAME'].history

history = JSON.parse(history)

history.each do |key, history|
  if history.is_a? Array
    history.each do |message|
      if message.is_a? Hash
        puts "#{message['from']['name']}: #{message['message']}"
      end
    end
  end
end

      

Obviously, the extension for this was to just curse the dates in the desired range (using:) client['ROOM_NAME'].history(:date => '2010-11-19', :timezone => 'PST')

, but again, I was only getting a part of the history for the room. Are there any additional options I am missing for this to make it work as expected?

+3


source to share


1 answer


I got this job, but it was a big pain.

Start by sending a request with the current time , in UTC, but excluding 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

It is very uncomfortable:

  • If you only provide the current date, no time zone, as described in the API, it is interpreted as midnight last night and you only get messages from yesterday and older.
  • If you try to put a date for tomorrow, the answer 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 comes in the HipChat API responses, the HipChats parser seems to fail and interprets it as midnight last night.

When you get a response back, grab the oldest property items.date

, split the timezone, and resubmit the above url with the updated parameter date

:



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 microseconds if the notification sent multiple messages to the same room in one second.

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

.

There is a parameter start-index

that I tried to skip before I got the above working and it will give you multiple pages of results and the answers won't have properties links.next

, but it won't give you the full story. Chatting with 9166 messages in history according to statistics.messages_sent

it only 3217 messages returned. So don't use it. You can use it statistics.messages_sent

as a health check to get all messages.

Oh yes, and the property last_active

in the call /v2/room

cannot be trusted because it is not updated when notifications are sent to the room.

+6


source







All Articles