telegram/replace.go (view raw)
1package telegram
2
3import (
4 "fmt"
5 "regexp"
6
7 tgbotapi "github.com/BiRabittoh/telegram-bot-api/v5"
8)
9
10type Replacer struct {
11 Regex *regexp.Regexp
12 Format string
13}
14
15const (
16 parseMode = "markdown"
17 linkMessage = "[🔗](%s) Da %s."
18 regexFlags = "(?i)(?m)"
19)
20
21var replacers = []Replacer{
22 {
23 Regex: regexp.MustCompile(regexFlags + `(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:(?:youtube(?:-nocookie)?\.com|youtu.be))(?:\/(?:[\w\-]+\?v=|embed\/|live\/|v\/|shorts\/)?)([\w\-]+)`),
24 Format: "https://y.outube.duckdns.org/%s",
25 },
26 {
27 Regex: regexp.MustCompile(regexFlags + `https?:\/\/(?:www\.)?twitter\.com\/(?:#!\/)?(.*)\/status(?:es)?\/([^\/\?\s]+)`),
28 Format: "https://fxtwitter.com/%s/status/%s",
29 },
30 {
31 Regex: regexp.MustCompile(regexFlags + `(?:https?:\/\/)?(?:www\.)?x\.com\/(?:#!\/)?(.*)\/status(?:es)?\/([^\/\?\s]+)`),
32 Format: "https://fixupx.com/%s/status/%s",
33 },
34 {
35 Regex: regexp.MustCompile(regexFlags + `(?:https?:\/\/)?(?:www\.)?instagram\.com\/(reels?|p)\/([\w\-]{11})[\/\?\w=&]*`),
36 Format: "https://ddinstagram.com/%s/%s",
37 },
38 {
39 Regex: regexp.MustCompile(regexFlags + `https?:\/\/(?:(?:www)|(?:vm))?\.?tiktok\.com\/@([\w.]+)\/(?:video)\/(\d{19,})`),
40 Format: "https://www.tnktok.com/@%s/video/%s",
41 },
42 {
43 Regex: regexp.MustCompile(regexFlags + `(?:https?:\/\/)?(?:(?:www)|(?:vm))?\.?tiktok\.com\/(?:t\/)?([\w]{9})\/?`),
44 Format: "https://vm.tnktok.com/%s/",
45 },
46}
47
48func isInSpoiler(entities []tgbotapi.MessageEntity, offset, length int) bool {
49 for _, s := range entities {
50 if s.Type != "spoiler" {
51 continue
52 }
53
54 if offset < s.Offset+s.Length && offset+length > s.Offset {
55 return true
56 }
57 }
58 return false
59}
60
61func parseText(text string, entities []tgbotapi.MessageEntity) (links []string) {
62 var rawLinks string
63
64 for _, e := range entities {
65 if e.Type == "text_link" {
66 if isInSpoiler(entities, e.Offset, len(e.URL)) {
67 continue
68 }
69 rawLinks += e.URL + "\n"
70 } else if e.Type == "url" {
71 if isInSpoiler(entities, e.Offset, e.Length) {
72 continue
73 }
74 rawLinks += text[e.Offset:e.Length] + "\n"
75 }
76 }
77
78 for _, replacer := range replacers {
79 foundMatches := replacer.Regex.FindStringSubmatch(rawLinks)
80 if len(foundMatches) == 0 {
81 continue
82 }
83 captureGroups := foundMatches[1:]
84
85 var formatArgs []interface{}
86 for _, match := range captureGroups {
87 if match != "" {
88 formatArgs = append(formatArgs, match)
89 }
90 }
91
92 formatted := fmt.Sprintf(replacer.Format, formatArgs...)
93 links = append(links, formatted)
94 }
95 return links
96}
97
98func getUserMention(user tgbotapi.User) string {
99 var name string
100 if user.UserName == "" {
101 name = user.FirstName + user.LastName
102 } else {
103 name = "@" + user.UserName
104 }
105 return fmt.Sprintf("[%s](tg://user?id=%d)", name, user.ID)
106}
107
108func handleLinks(bot *tgbotapi.BotAPI, message *tgbotapi.Message) {
109 links := []string{}
110
111 if len(message.Entities) > 0 {
112 textLinks := parseText(message.Text, message.Entities)
113 links = append(links, textLinks...)
114 }
115
116 if len(message.CaptionEntities) > 0 {
117 captionLinks := parseText(message.Caption, message.CaptionEntities)
118 links = append(links, captionLinks...)
119 }
120
121 if len(links) == 0 {
122 return
123 }
124
125 user := getUserMention(*message.From)
126
127 for _, link := range links {
128 text := fmt.Sprintf(linkMessage, link, user)
129 msg := tgbotapi.NewMessage(message.Chat.ID, text)
130 msg.MessageThreadID = message.MessageThreadID
131 msg.ParseMode = parseMode
132 bot.Send(msg)
133 }
134
135}