diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-05-18 17:18:47 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-05-18 17:18:47 +0300 |
commit | ae16fd2a0b48c3659a012accd0269323935dfd14 (patch) | |
tree | dbb5eba5aa29118b16cd5be2327027f96e1747eb | |
parent | b78779dad0c78479654941ecb43a3d0a30e83689 (diff) |
Implement robust history fetching
-rw-r--r-- | telegram/commands.go | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/telegram/commands.go b/telegram/commands.go index 17d212f..b2396b8 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -988,24 +988,32 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } } - var messages *client.Messages + var newMessages *client.Messages + var messages []*client.Message var err error - for _ = range make([]struct{}, 2) { - messages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{ - ChatId: chatID, - Limit: limit, + var fromId int64 + for _ = range make([]struct{}, limit) { // safety limit + if len(messages) > 0 { + fromId = messages[len(messages)-1].Id + } + + newMessages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{ + ChatId: chatID, + FromMessageId: fromId, + Limit: limit, }) if err != nil { return err.Error(), true } - // TDlib yields only the latest message on the first request - if !(len(messages.Messages) == 1 && limit > 1) { + messages = append(messages, newMessages.Messages...) + + if len(newMessages.Messages) == 0 || len(messages) >= int(limit) { break } } - c.sendMessagesReverse(chatID, messages.Messages) + c.sendMessagesReverse(chatID, messages) // chat members case "members": var query string |