From ccb5b1d0759ed1028aca994e6ab53c30435237f3 Mon Sep 17 00:00:00 2001 From: Jan Martin Reckel <57252256+reckel-jm@users.noreply.github.com> Date: Sat, 12 Mar 2022 00:19:02 +0100 Subject: [PATCH] Fix Telegram Problem (unforwarded formatting and skipping of linebreaks) (#1749) * Change bridge/telegram/handlers.go Comment out the removing of empty lines add support for bold, italic and striked telegram messages * Implement Telegram MessageEntities correctly * Apply gofmt Co-authored-by: Jan Martin Reckel Co-authored-by: Wim --- bridge/telegram/handlers.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index c5ed8c46..a27a6732 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -201,7 +201,8 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { b.handleEntities(&rmsg, message) if rmsg.Text != "" || len(rmsg.Extra) > 0 { - rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text) + // Comment the next line out due to avoid removing empty lines in Telegram + // rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text) // channels don't have (always?) user information. see #410 if message.From != nil { rmsg.Avatar = helper.GetAvatar(b.avatarMap, strconv.FormatInt(message.From.ID, 10), b.General) @@ -509,5 +510,21 @@ func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Messa rmsg.Text = rmsg.Text[:offset] + "```\n" + rmsg.Text[offset:offset+e.Length] + "\n```" + rmsg.Text[offset+e.Length:] indexMovedBy += 8 } + + if e.Type == "bold" { + offset := e.Offset + indexMovedBy + rmsg.Text = rmsg.Text[:offset] + "*" + rmsg.Text[offset:offset+e.Length] + "*" + rmsg.Text[offset+e.Length:] + indexMovedBy += 2 + } + if e.Type == "italic" { + offset := e.Offset + indexMovedBy + rmsg.Text = rmsg.Text[:offset] + "_" + rmsg.Text[offset:offset+e.Length] + "_" + rmsg.Text[offset+e.Length:] + indexMovedBy += 2 + } + if e.Type == "strike" { + offset := e.Offset + indexMovedBy + rmsg.Text = rmsg.Text[:offset] + "~" + rmsg.Text[offset:offset+e.Length] + "~" + rmsg.Text[offset+e.Length:] + indexMovedBy += 2 + } } }