From f57370f33abf7e2d61d39f4867c9c95f9fc6cd8c Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 2 Mar 2019 22:38:44 +0100 Subject: Add support for URL in messageEntities (telegram). Fixes #735 (#736) --- bridge/telegram/handlers.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'bridge/telegram/handlers.go') diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index 93576fb4..fa8f2ffb 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -144,6 +144,9 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { // quote the previous message b.handleQuoting(&rmsg, message) + // handle entities (adding URLs) + b.handleEntities(&rmsg, message) + if rmsg.Text != "" || len(rmsg.Extra) > 0 { rmsg.Text = helper.RemoveEmptyNewLines(rmsg.Text) // channels don't have (always?) user information. see #410 @@ -353,3 +356,22 @@ func (b *Btelegram) handleQuote(message, quoteNick, quoteMessage string) string format = strings.Replace(format, "{QUOTEMESSAGE}", quoteMessage, -1) return format } + +// handleEntities handles messageEntities +func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Message) { + if message.Entities == nil { + return + } + // for now only do URL replacements + for _, e := range *message.Entities { + if e.Type == "text_link" { + url, err := e.ParseURL() + if err != nil { + b.Log.Errorf("entity text_link url parse failed: %s", err) + continue + } + link := rmsg.Text[e.Offset : e.Offset+e.Length] + rmsg.Text = strings.Replace(rmsg.Text, link, url.String(), 1) + } + } +} -- cgit v1.2.3