From 9d31a390a8bf5c554eb0e934a2bfe2fa8feca8eb Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 3 Dec 2019 18:48:41 +0200 Subject: [PATCH] Send messages to Telegram --- telegram/commands.go | 4 +-- telegram/utils.go | 66 +++++++++++++++++++++++++++++++++++++++++++- xmpp/handlers.go | 2 +- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/telegram/commands.go b/telegram/commands.go index 102a2c4..606e28d 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -141,8 +141,8 @@ func (c *Client) ProcessChatCommand(cmdline string) (string, bool) { cmd, _ := parseCommand(cmdline) switch cmd { case "help": - return helpString(helpTypeChat), false + return helpString(helpTypeChat), true } - return "", true + return "", false } diff --git a/telegram/utils.go b/telegram/utils.go index d98cfb0..de70152 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -23,6 +23,7 @@ import ( var errOffline = errors.New("TDlib instance is offline") var spaceRegex = regexp.MustCompile(`\s+`) +var replyRegex = regexp.MustCompile("> ?([0-9]{10,})") const newlineChar string = "\n" @@ -406,14 +407,77 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str } // ProcessOutgoingMessage executes commands or sends messages to mapped chats -func (c *Client) ProcessOutgoingMessage(chatID int, text string, messageID int, returnJid string) { +func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) { if strings.HasPrefix(text, "/") { + // try to execute a command response, isCommand := c.ProcessChatCommand(text) if response != "" { gateway.SendMessage(returnJid, strconv.Itoa(int(chatID)), response, c.xmpp) } + // do not send on success if isCommand { return } } + + if !c.online { + // we're offline + return + } + + log.Warnf("Send message to chat %v", chatID) + + // 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 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:] + } + } + 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, + } + } else { + // compile our message + message = &client.InputMessageText{ + Text: formattedText, + } + } + + if messageID != 0 { + c.client.EditMessageText(&client.EditMessageTextRequest{ + ChatId: chatID, + MessageId: messageID, + InputMessageContent: message, + }) + } else { + c.client.SendMessage(&client.SendMessageRequest{ + ChatId: chatID, + ReplyToMessageId: reply, + InputMessageContent: message, + }) + } } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 861ecea..5635f10 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -64,7 +64,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { toParts := strings.Split(msg.To, "@") toID := toParts[0] if len(toParts) > 1 { - toIDInt, err := strconv.Atoi(toID) + toIDInt, err := strconv.ParseInt(toID, 10, 64) if err == nil { session.ProcessOutgoingMessage(toIDInt, msg.Body, 0, msg.From) return