Skip to content

Commit

Permalink
Preserve threading from telegram replies (telegram) (#1776)
Browse files Browse the repository at this point in the history
* Preserve threading from telegram replies

* Add fallback for unthreaded telegram message

* Fix linter issue
  • Loading branch information
sas1024 authored Mar 25, 2022
1 parent cc36ebf commit 5d9604c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions bridge/telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 17 additions & 4 deletions bridge/telegram/telegram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package btelegram

import (
"fmt"
"html"
"log"
"strconv"
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
###################################################################
Expand Down

0 comments on commit 5d9604c

Please sign in to comment.