如何通过API从Hipchat获取房间的所有消息历史记录? [英] How to get all message history from Hipchat for a room via the API?
问题描述
我今天有点用 Hipchat API (v2),遇到了一个奇怪的问题在那儿,我无法真正了解一个房间的所有历史记录.例如,当我查询一个特定日期时,它似乎只会检索给定日期的历史记录的一小部分.我本来打算简单地遍历一个房间的所有日期,以一种我可以使用的格式提取历史记录,但最终还是碰到了这个问题,现在不确定是否真的有可能完全提取历史记录. >
我意识到这有点笨拙.它以字符串的形式提取JSON,然后我必须将其形成为哈希,所以我知道我做的还不够好,但这是我快速完成的工作,只是为了测试
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
显然,对此的扩展是只是在所需范围内诅咒日期(使用:client['ROOM_NAME'].history(:date => '2010-11-19', :timezone => 'PST')
),但同样,我只得到了房间历史的一小部分.为了使它按预期工作,我是否缺少其他一些参数?
我已经开始工作了,但这是一个很大的痛苦.
首先以UTC发送带有 current time 的查询,但不包括时区作为开始日期:
请确保包括微秒,以防通知在同一秒内将多条消息发布到同一房间.
这将为您提供下一页消息.继续这样做,直到收到少于max-results
条消息为止.
在完成上述工作之前,我尝试传递了一个start-index
参数,它会给您几页结果,并且响应缺少links.next
属性,但不会为您提供完整的历史记录.在根据statistics.messages_sent
的历史记录中有9166条消息的聊天室中,它仅返回3217条消息.所以不要使用它.您可以使用statistics.messages_sent
来检查是否收到所有邮件.
是的,/v2/room
调用中的last_active
属性不能被信任,因为通知消息被发布到会议室时它不会更新.
I was using the Hipchat API (v2) a bit today and ran into an odd issue where I was not able to really pull up all of the history for a room. It seemed as though when I queried a specific date, for example, it would only retrieve a fraction of the history for that date given. I had had plans to simply iterate across all of the dates for a Room to extract the history in a format that I could use, but ended up hitting this and am now unsure if it is really possible to pull out the history fully.
I realize that this is a bit clunky. It is pulling the JSON as a string and then I have to form it into a hash so I know I'm not doing this as good as it could be done, but here is roughly what I quickly did just to test out the history
method 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 then the extension to that was to just curse through the dates in the desired range (using: client['ROOM_NAME'].history(:date => '2010-11-19', :timezone => 'PST')
), but again, I was only getting a fraction of the history for the room. Are there some additional parameters that I'm missing for this to make it work as expected?
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:
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:
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.
这篇关于如何通过API从Hipchat获取房间的所有消息历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!