Td: Call api TdApi.GetChatHistory,The number of history messages returned is incorrect

Created on 30 Mar 2018  Â·  16Comments  Â·  Source: tdlib/td

When I The first call the api TdApi.GetChatHistory,like this:

   TdApi.GetChatHistory getChatHistory = new TdApi.GetChatHistory(chatId, fromId, 0, LOAD_MESSAGES_COUNT, false);//at this time,the fromId is 0,LOAD_MESSAGES_COUNT is 10

 Client.send(getChatHistory);

There is only one message in the result, but there are many messages in the chat at this time。

Most helpful comment

If single message is returned, then in next request from_message_id should be set to ID of returned message, so that message isn't returned again.

All 16 comments

From GetChatHistory method documentation "For optimal performance the number of returned messages is chosen by the library." If you need more messages, you need to repeat the call with adjusted fromMessageId.

thank you

btw what is the rationale against returning the number of messages as per requested?

from what I've noticed getChatHistory first returns an arbitrary number of results, but subsequent calls for the same chat_id, with the same parameters return the expected number of results.

this behaviour is very odd imo.

Because it should be possible to view chat history without available network connection. It would be very odd, if you can't view locally available messages if there is less than limit of them.

I'm not sure I understand what you mean, this is the case even with only_local set to false.
It feels odd that I need to make the same request 2 or 3 times before actually getting the result.

Also, how am I supposed to know whether the reason I get less than the expected number of messages is because the api just decided not to give them or whether there are not enough messages in the chat?

So let's say I want to get the last 10 messages in a group, what sequence of calls should I make to fetch them?

If only_local is set to true, you would also need to always wait for completion of database request, which also may be slow.

You shouldn't make the same request. Instead, you should adjust from_message_id on every subsequent request. It should look like this

from_message_id = 0;
left = 10;
while (left > 0) {
  messages = await getChatHistory(chat_id, from_message_id, 0, left, false);
  if (empty(messages)) {
    // there is no more messages
    break;
  }
  from_message_id = messages[-1].message_id;
  left -= size(messages);
  result += messages;
}
// result contains last 10 messages

That's similar to what I'm doing but I've seen sometimes it returns a single result, which might result in making the same request a couple times because from_message_id wouldn't be updated in that case

If single message is returned, then in next request from_message_id should be set to ID of returned message, so that message isn't returned again.

HI Aleksy , do you happen to have a full example for the snippet above?

All I have is an example for calling the API like so :+1:

new TdApi.GetChatHistory(chatId, fromId, 0, LOAD_MESSAGES_COUNT, false);

what does "await getChatHistory" look lke ?

@stanleyG123 The full example depends on a language the code is written in. You can try to find some examples there.

So where can I find the example above ( any language) ? I am interested in getChatHistory() and how it aggregates messages together into a collection.

@stanleyG123 It's up to your application, how the results of getChatHistory will be processed. https://core.telegram.org/tdlib/getting-started describes among other things, how results of requests are received.

@levlam I was just wondering if it's possible that the getChatHistory would return 0 messages even though there are more messages, with onlyLocal set to false? If it can happen then is there a way to check that there are more messages and I need to make another request to get it, or is it unnecessary to make another request for a set of messages as TDLib would still return 0 messages?

@chairus If getChatHistory with only_local equal to false returned no new messages then there is no reason to repeat the request with the same parameters.

@levlam Are you saying that it may happen that getChatHistory might return 0 messages even though there are more messages? Is that a bug?

@chairus I'm saying that if getChatHistory returned 0 messages, then you must assume that there are no more messages.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

devxpy picture devxpy  Â·  5Comments

Invision70 picture Invision70  Â·  3Comments

mossaudi picture mossaudi  Â·  3Comments

omkarnathsingh picture omkarnathsingh  Â·  4Comments

joshchernoff picture joshchernoff  Â·  5Comments