5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 10:30:26 +00:00

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.
This commit is contained in:
Dan Walmsley 2021-12-07 20:26:28 +00:00 committed by GitHub
parent 80d3033456
commit b275efaeff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -435,6 +435,9 @@ func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Messa
if message.Entities == nil { if message.Entities == nil {
return return
} }
var indexMovedBy = 0
// for now only do URL replacements // for now only do URL replacements
for _, e := range *message.Entities { for _, e := range *message.Entities {
if e.Type == "text_link" { 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]) link := utf16.Decode(utfEncodedString[e.Offset : e.Offset+e.Length])
rmsg.Text = strings.Replace(rmsg.Text, string(link), url.String(), 1) 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
}
} }
} }