diff options
author | Dan Walmsley <dan@walms.co.uk> | 2021-12-07 20:26:28 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-07 21:26:28 +0100 |
commit | b275efaeff94b3a747a6c312829f0b107d831f72 (patch) | |
tree | b5b2c08b50499d9a3e678e01faf58cf09c8a66cd /bridge/telegram/handlers.go | |
parent | 80d303345669d96b40de1d4998ec9928cb67b27d (diff) | |
download | matterbridge-msglm-b275efaeff94b3a747a6c312829f0b107d831f72.tar.gz matterbridge-msglm-b275efaeff94b3a747a6c312829f0b107d831f72.tar.bz2 matterbridge-msglm-b275efaeff94b3a747a6c312829f0b107d831f72.zip |
Add support for code blocks in telegram (#1650)
* handle code blocks in telegram.
* support multi-line code blocks.
* remove import.
* handle code blocks in middle of normal text.
* support multiple code blocks in same message.
Diffstat (limited to 'bridge/telegram/handlers.go')
-rw-r--r-- | bridge/telegram/handlers.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index 84881f7f..7f40ddc9 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -435,6 +435,9 @@ func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Messa if message.Entities == nil { return } + + var indexMovedBy = 0 + // for now only do URL replacements for _, e := range *message.Entities { if e.Type == "text_link" { @@ -451,5 +454,17 @@ func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Messa link := utf16.Decode(utfEncodedString[e.Offset : e.Offset+e.Length]) rmsg.Text = strings.Replace(rmsg.Text, string(link), url.String(), 1) } + + if e.Type == "code" { + var 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 == "pre" { + var offset = e.Offset + indexMovedBy + rmsg.Text = rmsg.Text[:offset] + "```\n" + rmsg.Text[offset:offset + e.Length] + "\n```" + rmsg.Text[offset + e.Length :] + indexMovedBy += 8 + } } } |