diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-12-07 19:37:14 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-12-07 19:37:14 +0300 |
commit | 589876eef53ad2a64ffec9e1e47579b80465ac68 (patch) | |
tree | 6f66c86de87b4896bde046970070c4e569300531 /telegram/commands.go | |
parent | 3e791db2015d47661cc640e41eb6a23914c86ad4 (diff) |
Add replace command
Diffstat (limited to 'telegram/commands.go')
-rw-r--r-- | telegram/commands.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/telegram/commands.go b/telegram/commands.go index 5553580..3682535 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -3,6 +3,7 @@ package telegram import ( "fmt" "github.com/pkg/errors" + "regexp" "strconv" "strings" @@ -289,6 +290,50 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if err != nil { return err.Error(), true } + // edit last message + case "s": + if c.me == nil { + return "@me is not initialized", true + } + if len(args) < 2 { + return "Not enough arguments", true + } + regex, err := regexp.Compile(args[0]) + if err != nil { + return err.Error(), true + } + + messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ + ChatId: chatID, + Limit: 1, + SenderUserId: c.me.Id, + Filter: &client.SearchMessagesFilterEmpty{}, + }) + log.Debugf("%#v", client.SearchChatMessagesRequest{ + ChatId: chatID, + Limit: 1, + SenderUserId: c.me.Id, + Filter: &client.SearchMessagesFilterEmpty{}, + }) + if err != nil { + return err.Error(), true + } + if len(messages.Messages) == 0 { + return "No last message", true + } + + message := messages.Messages[0] + if message == nil { + return "Last message is empty", true + } + + messageText, ok := message.Content.(*client.MessageText) + if !ok { + return "Last message is not a text!", true + } + + text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " ")) + c.ProcessOutgoingMessage(chatID, text, message.Id, "") case "help": return helpString(helpTypeChat), true default: |