diff options
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | main.go | 25 |
3 files changed, 25 insertions, 3 deletions
@@ -14,4 +14,5 @@ require ( github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect golang.org/x/text v0.14.0 // indirect + mvdan.cc/xurls/v2 v2.5.0 ) @@ -63,3 +63,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +mvdan.cc/xurls/v2 v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8= +mvdan.cc/xurls/v2 v2.5.0/go.mod h1:yQgaGQ1rFtJUzkmKiHYSSfuQxqfYmd//X6PxvholpeE= @@ -8,6 +8,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/kkdai/youtube/v2" + "mvdan.cc/xurls/v2" ) func main() { @@ -30,20 +31,33 @@ func main() { if update.Message != nil { // If we got a message message := update.Message - link, err := url.Parse(message.Text) + xRelaxed := xurls.Relaxed() + linkString := xRelaxed.FindString(message.Text) + if linkString == "" { + msg := tgbotapi.NewMessage(message.Chat.ID, "Сообщение должно содержать ссылку") + log.Println(err) + msg.ReplyToMessageID = message.MessageID + bot.Send(msg) + continue + } + link, err := url.Parse(linkString) if err != nil { + msg := tgbotapi.NewMessage(message.Chat.ID, "Сообщение должно содержать корректную ссылку на youtube") + log.Println(err) + msg.ReplyToMessageID = message.MessageID + bot.Send(msg) continue } go func(message *tgbotapi.Message) { - if link.Host == "youtube.com" && link.Query().Get("v") != "" { + if strings.HasSuffix(link.Host, "youtube.com") && link.Query().Get("v") != "" { if err := download(bot, message, link.Query().Get("v")); err != nil { msg := tgbotapi.NewMessage(message.Chat.ID, "Ошибка: "+err.Error()) log.Println(err) msg.ReplyToMessageID = message.MessageID bot.Send(msg) } - } else if link.Host == "youtu.be" && link.Path != "" { + } else if strings.HasSuffix(link.Host, "youtu.be") && link.Path != "" { pathParts := strings.SplitN(link.Path, "?", 2) if err := download(bot, message, pathParts[0]); err != nil { msg := tgbotapi.NewMessage(message.Chat.ID, "Ошибка: "+err.Error()) @@ -51,6 +65,11 @@ func main() { msg.ReplyToMessageID = message.MessageID bot.Send(msg) } + } else { + msg := tgbotapi.NewMessage(message.Chat.ID, "Сообщение должно содержать корректную ссылку на youtube") + log.Println(err) + msg.ReplyToMessageID = message.MessageID + bot.Send(msg) } }(message) } |