diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-01-08 13:59:57 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-01-08 13:59:57 +0300 |
commit | 9f04ed51bd26923afec70f086bdeba934fec32ba (patch) | |
tree | d3160232bbe0b64002b325c992c21194867a9989 /telegram/utils.go | |
parent | ee6653c0c6c09a5a38d1c1ef2be5318edfbe2e14 (diff) |
Make /s replace the whole message; fix replies and whitespace corruption
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 108 |
1 files changed, 48 insertions, 60 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index bddffc8..7cc6256 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -25,7 +25,7 @@ import ( var errOffline = errors.New("TDlib instance is offline") var spaceRegex = regexp.MustCompile(`\s+`) -var replyRegex = regexp.MustCompile("> ?([0-9]{10,})") +var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n") const newlineChar string = "\n" @@ -483,83 +483,68 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str } // ProcessOutgoingMessage executes commands or sends messages to mapped chats -func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) { - if messageID == 0 && strings.HasPrefix(text, "/") { - // try to execute a command +func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string) client.InputMessageContent { + if !c.Online() { + // we're offline + return nil + } + + if returnJid != "" && strings.HasPrefix(text, "/") { + // try to execute commands response, isCommand := c.ProcessChatCommand(chatID, text) if response != "" { gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp) } // do not send on success if isCommand { - return + return nil } } - if !c.Online() { - // we're offline - return - } - - log.Warnf("Send message to chat %v", chatID) + log.Warnf("Sending message to chat %v", chatID) - if messageID != 0 { - formattedText := &client.FormattedText{ - Text: text, - } + // quotations + var reply int64 + replySlice := replyRegex.FindStringSubmatch(text) + if len(replySlice) > 1 { + reply, _ = strconv.ParseInt(replySlice[1], 10, 64) + } - // compile our message - message := &client.InputMessageText{ - Text: formattedText, - } - - c.client.EditMessageText(&client.EditMessageTextRequest{ - ChatID: chatID, - MessageID: messageID, - InputMessageContent: message, - }) - } else { - // quotations - var reply int64 - replySlice := replyRegex.FindStringSubmatch(text) - if len(replySlice) > 1 { - reply, _ = strconv.ParseInt(replySlice[1], 10, 64) + // attach a file + var file *client.InputFileRemote + if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { + file = &client.InputFileRemote{ + ID: text, } + } - // attach a file - var file *client.InputFileRemote - if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { - file = &client.InputFileRemote{ - ID: text, - } + // remove first line from text + if file != nil || reply != 0 { + newlinePos := strings.Index(text, newlineChar) + if newlinePos != -1 { + text = text[newlinePos+1:] } + } - // remove first line from text - if file != nil || reply != 0 { - newlinePos := strings.Index(text, newlineChar) - if newlinePos != -1 { - text = text[newlinePos+1:] - } - } + formattedText := &client.FormattedText{ + Text: text, + } - formattedText := &client.FormattedText{ - Text: text, + var message client.InputMessageContent + if file != nil { + // we can try to send a document + message = &client.InputMessageDocument{ + Document: file, + Caption: formattedText, } - - var message client.InputMessageContent - if file != nil { - // we can try to send a document - message = &client.InputMessageDocument{ - Document: file, - Caption: formattedText, - } - } else { - // compile our message - message = &client.InputMessageText{ - Text: formattedText, - } + } else { + // compile our message + message = &client.InputMessageText{ + Text: formattedText, } + } + if chatID != 0 { _, err := c.client.SendMessage(&client.SendMessageRequest{ ChatID: chatID, ReplyToMessageID: reply, @@ -569,10 +554,13 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int gateway.SendMessage( returnJid, strconv.FormatInt(chatID, 10), - fmt.Sprintf("Message not sent: %s", err.Error()), + fmt.Sprintf("Not sent: %s", err.Error()), c.xmpp, ) } + return nil + } else { + return message } } |