Skip to content

Commit 5655616

Browse files
authored
Make use of Telegram buttons for original URLs (#8)
* Make use of telegram buttons for original url Fixes #5 * Add original link buttons
1 parent 42c8a54 commit 5655616

1 file changed

Lines changed: 38 additions & 11 deletions

File tree

sanitizetelebot.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,31 +75,44 @@ func main() {
7575
return
7676
}
7777

78-
sanitizedMsg, sanitized, isPhotoURL, photoURLs, err := sanitizeURL(m.Text)
78+
sanitizedMsg, sanitized, isPhotoURL, photoURLs, originalURLs, err := sanitizeURL(m.Text)
7979
if err != nil {
8080
log.Println(err)
8181
return
8282
}
8383

8484
if sanitized {
8585
// Create send options with reply if original was a reply
86-
sendOpts := &telebot.SendOptions{ParseMode: telebot.ModeMarkdown}
86+
sendOpts := &telebot.SendOptions{
87+
ParseMode: telebot.ModeMarkdown,
88+
}
8789
if m.IsReply() {
8890
sendOpts.ReplyTo = m.ReplyTo
8991
}
9092

93+
// Create URL buttons
94+
if len(originalURLs) > 0 {
95+
buttons := createURLButtons(originalURLs)
96+
sendOpts.ReplyMarkup = &telebot.ReplyMarkup{InlineKeyboard: buttons}
97+
}
98+
9199
if isPhotoURL && len(photoURLs) > 0 {
92100
// Create album of photos with caption on first photo
93101
album := make(telebot.Album, 0)
94102
for i, photoPath := range photoURLs {
95103
var photo *telebot.Photo
96104
if i == 0 {
97-
// Escape brackets in the URL to prevent Markdown parsing issues
98-
escapedURL := escapeMarkdown(m.Text)
99-
// Add caption to first photo
105+
// Get the message text in the default format
106+
messageText := ""
107+
escapedMsg := escapeMarkdown(m.Text)
108+
if m.FromGroup() && strings.Contains(m.Text, "anon") {
109+
messageText = strings.Replace(escapedMsg, "anon", "", 1)
110+
} else {
111+
messageText = "@" + username + " said: " + escapedMsg
112+
}
100113
photo = &telebot.Photo{
101114
File: telebot.FromDisk(photoPath),
102-
Caption: fmt.Sprintf("@%s said: [Original Link](%s)", username, escapedURL),
115+
Caption: messageText,
103116
}
104117
} else {
105118
photo = &telebot.Photo{File: telebot.FromDisk(photoPath)}
@@ -141,7 +154,7 @@ func main() {
141154
})
142155

143156
b.Handle(telebot.OnQuery, func(q *telebot.Query) {
144-
sanitizedMsg, sanitized, _, _, err := sanitizeURL(q.Text)
157+
sanitizedMsg, sanitized, _, _, _, err := sanitizeURL(q.Text)
145158
if err != nil {
146159
log.Println(err)
147160
return
@@ -174,13 +187,14 @@ func getUsername(sender *telebot.User) string {
174187
return sender.Username
175188
}
176189

177-
func sanitizeURL(text string) (string, bool, bool, []string, error) {
190+
func sanitizeURL(text string) (string, bool, bool, []string, []string, error) {
178191
// Split text into paragraphs first
179192
paragraphs := strings.Split(text, "\n")
180193
var sanitizedParagraphs []string
181194
var sanitized bool
182195
var isPhotoURL bool
183196
var photoURLs []string
197+
var originalURLs []string
184198

185199
for _, paragraph := range paragraphs {
186200
if paragraph == "" {
@@ -193,6 +207,7 @@ func sanitizeURL(text string) (string, bool, bool, []string, error) {
193207

194208
for _, word := range words {
195209
if containsURL(word) {
210+
originalURLs = append(originalURLs, word) // Store the original URL
196211
parsedURL, err := url.Parse(word)
197212
if err != nil {
198213
sanitizedWords = append(sanitizedWords, word)
@@ -202,11 +217,11 @@ func sanitizeURL(text string) (string, bool, bool, []string, error) {
202217
if parsedURL.Host == "vm.tiktok.com" || parsedURL.Host == "tiktok.com" {
203218
word, err = ExpandUrl(word)
204219
if err != nil {
205-
return "", false, false, nil, err
220+
return "", false, false, nil, nil, err
206221
}
207222
parsedURL, err = url.Parse(word)
208223
if err != nil {
209-
return "", false, false, nil, err
224+
return "", false, false, nil, nil, err
210225
}
211226
}
212227

@@ -452,7 +467,7 @@ func sanitizeURL(text string) (string, bool, bool, []string, error) {
452467
sanitizedParagraphs = append(sanitizedParagraphs, strings.Join(sanitizedWords, " "))
453468
}
454469

455-
return strings.Join(sanitizedParagraphs, "\n"), sanitized, isPhotoURL, photoURLs, nil
470+
return strings.Join(sanitizedParagraphs, "\n"), sanitized, isPhotoURL, photoURLs, originalURLs, nil
456471
}
457472

458473
func containsURL(text string) bool {
@@ -566,3 +581,15 @@ func fetchTikTokPhotos(photoURL string) ([]string, error) {
566581

567582
return localPaths, nil
568583
}
584+
585+
func createURLButtons(urls []string) [][]telebot.InlineButton {
586+
var rows [][]telebot.InlineButton
587+
for i, url := range urls {
588+
button := telebot.InlineButton{
589+
Text: fmt.Sprintf("Original Link #%d", i+1),
590+
URL: url,
591+
}
592+
rows = append(rows, []telebot.InlineButton{button})
593+
}
594+
return rows
595+
}

0 commit comments

Comments
 (0)