all repos — escarbot @ e68eb04533a659848407ac2be2de0ff3fcd7d603

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

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 parseText(text string, entities []tgbotapi.MessageEntity) []string {
 49	var rawLinks string
 50	for _, e := range entities {
 51		if e.Type == "text_link" {
 52			rawLinks += e.URL + "\n"
 53		}
 54
 55		if e.Type == "url" {
 56			rawLinks += text[e.Offset:e.Length] + "\n"
 57		}
 58	}
 59
 60	links := []string{}
 61	for _, replacer := range replacers {
 62		foundMatches := replacer.Regex.FindStringSubmatch(rawLinks)
 63		if len(foundMatches) == 0 {
 64			continue
 65		}
 66		captureGroups := foundMatches[1:]
 67
 68		var formatArgs []interface{}
 69		for _, match := range captureGroups {
 70			if match != "" {
 71				formatArgs = append(formatArgs, match)
 72			}
 73		}
 74
 75		formatted := fmt.Sprintf(replacer.Format, formatArgs...)
 76		links = append(links, formatted)
 77	}
 78	return links
 79}
 80
 81func getUserMention(user tgbotapi.User) string {
 82	var name string
 83	if user.UserName == "" {
 84		name = user.FirstName + user.LastName
 85	} else {
 86		name = "@" + user.UserName
 87	}
 88	return fmt.Sprintf("[%s](tg://user?id=%d)", name, user.ID)
 89}
 90
 91func handleLinks(bot *tgbotapi.BotAPI, message *tgbotapi.Message) {
 92	links := []string{}
 93
 94	if len(message.Entities) > 0 {
 95		textLinks := parseText(message.Text, message.Entities)
 96		links = append(links, textLinks...)
 97	}
 98
 99	if len(message.CaptionEntities) > 0 {
100		captionLinks := parseText(message.Caption, message.CaptionEntities)
101		links = append(links, captionLinks...)
102	}
103
104	if len(links) == 0 {
105		return
106	}
107
108	user := getUserMention(*message.From)
109
110	for _, link := range links {
111		text := fmt.Sprintf(linkMessage, link, user)
112		msg := tgbotapi.NewMessage(message.Chat.ID, text)
113		msg.MessageThreadID = message.MessageThreadID
114		msg.ParseMode = parseMode
115		bot.Send(msg)
116	}
117
118}