all repos — escarbot @ e68eb04533a659848407ac2be2de0ff3fcd7d603

Earthbound Café's custom Telegram bot, with lots of cool utilities built-in.

parse message entities
Marco Andronaco andronacomarco@gmail.com
Sat, 26 Oct 2024 15:14:42 +0200
commit

e68eb04533a659848407ac2be2de0ff3fcd7d603

parent

661438f9080f348c334f56cc65014c5288474afb

1 files changed, 17 insertions(+), 8 deletions(-)

jump to
M telegram/replace.gotelegram/replace.go

@@ -45,12 +45,21 @@ Format: "https://vm.tnktok.com/%s/",

}, } -func parseText(message string) []string { - links := []string{} +func parseText(text string, entities []tgbotapi.MessageEntity) []string { + var rawLinks string + for _, e := range entities { + if e.Type == "text_link" { + rawLinks += e.URL + "\n" + } + + if e.Type == "url" { + rawLinks += text[e.Offset:e.Length] + "\n" + } + } + links := []string{} for _, replacer := range replacers { - foundMatches := replacer.Regex.FindStringSubmatch(message) - + foundMatches := replacer.Regex.FindStringSubmatch(rawLinks) if len(foundMatches) == 0 { continue }

@@ -82,13 +91,13 @@

func handleLinks(bot *tgbotapi.BotAPI, message *tgbotapi.Message) { links := []string{} - if message.Text != "" { - textLinks := parseText(message.Text) + if len(message.Entities) > 0 { + textLinks := parseText(message.Text, message.Entities) links = append(links, textLinks...) } - if message.Caption != "" { - captionLinks := parseText(message.Caption) + if len(message.CaptionEntities) > 0 { + captionLinks := parseText(message.Caption, message.CaptionEntities) links = append(links, captionLinks...) }