From ad1beafeb3e59df8fb759fd89fcdbfc7b2cfef4f Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Sun, 8 Dec 2019 18:19:35 +0200 Subject: [PATCH] Add /history command --- telegram/commands.go | 64 +++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/telegram/commands.go b/telegram/commands.go index f4e0c62..12bf187 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -40,15 +40,15 @@ var chatCommands = map[string]command{ "channel": command{"title description", "create new channel «title» with «description»"}, "secret": command{"", "create secretchat with current user"}, "search": command{"string [limit]", "search in current chat"}, - //"history": command{"[limit]", "get last [limit] messages from current chat"}, - "block": command{"", "blacklist current user"}, - "unblock": command{"", "unblacklist current user"}, - "invite": command{"id or @username", "add user to current chat"}, - "kick": command{"id or @username", "remove user to current chat"}, - "ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"}, - "leave": command{"", "leave current chat"}, - "close": command{"", "close current secret chat"}, - "delete": command{"", "delete current chat from chat list"}, + "history": command{"[limit]", "get last [limit] messages from current chat"}, + "block": command{"", "blacklist current user"}, + "unblock": command{"", "unblacklist current user"}, + "invite": command{"id or @username", "add user to current chat"}, + "kick": command{"id or @username", "remove user to current chat"}, + "ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"}, + "leave": command{"", "leave current chat"}, + "close": command{"", "close current secret chat"}, + "delete": command{"", "delete current chat from chat list"}, //"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"}, } @@ -122,6 +122,17 @@ func (c *Client) unsubscribe(chatID int64) { ) } +func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) { + for i := len(messages) - 1; i >= 0; i-- { + gateway.SendMessage( + c.jid, + strconv.FormatInt(chatID, 10), + c.formatMessage(0, 0, false, messages[i]), + c.xmpp, + ) + } +} + func (c *Client) usernameOrIdToId(username string) (int32, error) { userID, err := strconv.ParseInt(username, 10, 32) // couldn't parse the id, try to lookup as a username @@ -599,24 +610,35 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatId: chatID, - Query: query, - Limit: limit, - SenderUserId: c.me.Id, - Filter: &client.SearchMessagesFilterEmpty{}, + ChatId: chatID, + Query: query, + Limit: limit, + Filter: &client.SearchMessagesFilterEmpty{}, }) if err != nil { return err.Error(), true } - for i := len(messages.Messages) - 1; i >= 0; i-- { - gateway.SendMessage( - c.jid, - strconv.FormatInt(chatID, 10), - c.formatMessage(0, 0, false, messages.Messages[i]), - c.xmpp, - ) + c.sendMessagesReverse(chatID, messages.Messages) + // get latest entries from history + case "history": + var limit int32 = 10 + if len(args) > 0 { + newLimit, err := strconv.ParseInt(args[0], 10, 32) + if err == nil { + limit = int32(newLimit) + } } + + messages, err := c.client.GetChatHistory(&client.GetChatHistoryRequest{ + ChatId: chatID, + Limit: limit, + }) + if err != nil { + return err.Error(), true + } + + c.sendMessagesReverse(chatID, messages.Messages) case "help": return helpString(helpTypeChat), true default: