From ae16fd2a0b48c3659a012accd0269323935dfd14 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Wed, 18 May 2022 10:18:47 -0400 Subject: [PATCH] Implement robust history fetching --- telegram/commands.go | 24 ++++++++++++++++-------- 1 file 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