aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2023-06-05 11:22:13 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2023-06-05 11:22:13 +0300
commit7215d11d7973b9896c6223938649c75165fa3ae7 (patch)
tree0aa0bf028cc899a283eb55654ee7618eba167681 /telegram/utils.go
parenta5c90340ad2da16a68ddcbfa3d9573f0664067ca (diff)
XEP-0308 message editing
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go39
1 files changed, 22 insertions, 17 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index dd63248..ecce6da 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -991,14 +991,14 @@ func (c *Client) PrepareOutgoingMessageContent(text string) client.InputMessageC
return c.prepareOutgoingMessageContent(text, nil)
}
-// ProcessOutgoingMessage executes commands or sends messages to mapped chats
-func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, id string, replyId int64) {
+// ProcessOutgoingMessage executes commands or sends messages to mapped chats, returns message id
+func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, replyId int64, replaceId int64) int64 {
if !c.Online() {
// we're offline
- return
+ return 0
}
- if strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!") {
+ if replaceId == 0 && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) {
// try to execute commands
response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" {
@@ -1006,7 +1006,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
}
// do not send on success
if isCommand {
- return
+ return 0
}
}
@@ -1014,7 +1014,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
// quotations
var reply int64
- if replyId == 0 {
+ if replaceId == 0 && replyId == 0 {
replySlice := replyRegex.FindStringSubmatch(text)
if len(replySlice) > 1 {
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
@@ -1069,24 +1069,29 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
content := c.prepareOutgoingMessageContent(text, file)
+ if replaceId != 0 {
+ tgMessage, err := c.client.EditMessageText(&client.EditMessageTextRequest{
+ ChatId: chatID,
+ MessageId: replaceId,
+ InputMessageContent: content,
+ })
+ if err != nil {
+ c.returnError(returnJid, chatID, "Not edited", err)
+ return 0
+ }
+ return tgMessage.Id
+ }
+
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
ChatId: chatID,
ReplyToMessageId: reply,
InputMessageContent: content,
})
if err != nil {
- gateway.SendTextMessage(
- returnJid,
- strconv.FormatInt(chatID, 10),
- fmt.Sprintf("Not sent: %s", err.Error()),
- c.xmpp,
- )
- } else {
- err = gateway.IdsDB.Set(c.Session.Login, c.jid, tgMessage.ChatId, tgMessage.Id, id)
- if err != nil {
- log.Errorf("Failed to save ids %v/%v %v", tgMessage.ChatId, tgMessage.Id, id)
- }
+ c.returnError(returnJid, chatID, "Not sent", err)
+ return 0
}
+ return tgMessage.Id
}
func (c *Client) returnMessage(returnJid string, chatID int64, text string) {