diff options
author | Alexander <emu.sas@gmail.com> | 2022-03-25 23:58:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-25 21:58:52 +0100 |
commit | 5d9604cd15513dce2244f0a4d2f8e67a6ee22455 (patch) | |
tree | a5b6eb2ad20e9d5e248ded9514a171bb3a1cc260 | |
parent | cc36ebf1c9191a6107637b26bd3dbc802f91eb4a (diff) | |
download | matterbridge-msglm-5d9604cd15513dce2244f0a4d2f8e67a6ee22455.tar.gz matterbridge-msglm-5d9604cd15513dce2244f0a4d2f8e67a6ee22455.tar.bz2 matterbridge-msglm-5d9604cd15513dce2244f0a4d2f8e67a6ee22455.zip |
Preserve threading from telegram replies (telegram) (#1776)
* Preserve threading from telegram replies
* Add fallback for unthreaded telegram message
* Fix linter issue
-rw-r--r-- | bridge/telegram/handlers.go | 5 | ||||
-rw-r--r-- | bridge/telegram/telegram.go | 21 | ||||
-rw-r--r-- | matterbridge.toml.sample | 6 |
3 files changed, 28 insertions, 4 deletions
diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index d9b104a3..6267c12b 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -199,6 +199,11 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { rmsg.ID = strconv.Itoa(message.MessageID) rmsg.Channel = strconv.FormatInt(message.Chat.ID, 10) + // preserve threading from telegram reply + if message.ReplyToMessage != nil { + rmsg.ParentID = strconv.Itoa(message.ReplyToMessage.MessageID) + } + // handle entities (adding URLs) b.handleEntities(&rmsg, message) diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index d696e9bc..1b648309 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -1,6 +1,7 @@ package btelegram import ( + "fmt" "html" "log" "strconv" @@ -108,10 +109,16 @@ func (b *Btelegram) Send(msg config.Message) (string, error) { return b.handleDelete(&msg, chatid) } + // Handle prefix hint for unthreaded messages. + if msg.ParentNotFound() { + msg.ParentID = "" + msg.Text = fmt.Sprintf("[reply]: %s", msg.Text) + } + // Upload a file if it exists if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { - if _, msgErr := b.sendMessage(chatid, rmsg.Username, rmsg.Text); msgErr != nil { + if _, msgErr := b.sendMessage(chatid, rmsg.Username, rmsg.Text, msg.ParentID); msgErr != nil { b.Log.Errorf("sendMessage failed: %s", msgErr) } } @@ -131,7 +138,7 @@ func (b *Btelegram) Send(msg config.Message) (string, error) { // Ignore empty text field needs for prevent double messages from whatsapp to telegram // when sending media with text caption if msg.Text != "" { - return b.sendMessage(chatid, msg.Username, msg.Text) + return b.sendMessage(chatid, msg.Username, msg.Text, msg.ParentID) } return "", nil @@ -145,10 +152,16 @@ func (b *Btelegram) getFileDirectURL(id string) string { return res } -func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) { +func (b *Btelegram) sendMessage(chatid int64, username, text, parentID string) (string, error) { m := tgbotapi.NewMessage(chatid, "") m.Text, m.ParseMode = TGGetParseMode(b, username, text) - + if parentID != "" { + rmid, err := strconv.Atoi(parentID) + if err != nil { + return "", err + } + m.ReplyToMessageID = rmid + } m.DisableWebPagePreview = b.GetBool("DisableWebPagePreview") res, err := b.c.Send(m) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 656949fc..54768a1e 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1147,6 +1147,12 @@ StripNick=false #OPTIONAL (default false) ShowTopicChange=false +#Opportunistically preserve threaded replies between Telegram groups. +#This only works if the parent message is still in the cache. +#Cache is flushed between restarts. +#OPTIONAL (default false) +PreserveThreading=false + ################################################################### #rocketchat section ################################################################### |