diff --git a/common/go.mod b/common/go.mod index 8210290..21e9524 100644 --- a/common/go.mod +++ b/common/go.mod @@ -1,5 +1,3 @@ module nulo.in/dlbot/common go 1.19 - -require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect diff --git a/common/go.sum b/common/go.sum index db8e45c..e69de29 100644 --- a/common/go.sum +++ b/common/go.sum @@ -1,2 +0,0 @@ -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/common/main.go b/common/main.go index 21c473f..77ea85b 100644 --- a/common/main.go +++ b/common/main.go @@ -2,34 +2,20 @@ package common import ( "net/url" - "strings" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) -type Result uint8 type Responder interface { - Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) Result + Respond(url *url.URL) (*Uploadable, Error) } +type Uploadable struct { + Url string + Caption string +} + +type Error uint8 const ( - NotValid Result = iota + OK Error = iota + NotValid HadError - Uploaded ) - -func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig { - res := tgbotapi.NewMessage(msg.Chat.ID, str) - res.ReplyToMessageID = msg.MessageID - res.DisableWebPagePreview = true - res.ParseMode = "markdown" - return res -} - -func RespondWithMany(msg *tgbotapi.Message, s ...string) tgbotapi.MessageConfig { - var res strings.Builder - for _, v := range s { - res.WriteString(v) - } - return respondWith(msg, res.String()) -} diff --git a/instagram/go.mod b/instagram/go.mod index 2e59c67..d800e50 100644 --- a/instagram/go.mod +++ b/instagram/go.mod @@ -4,7 +4,4 @@ go 1.19 replace nulo.in/dlbot/common => ../common -require ( - github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 - nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 -) +require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 diff --git a/instagram/go.sum b/instagram/go.sum index db8e45c..e69de29 100644 --- a/instagram/go.sum +++ b/instagram/go.sum @@ -1,2 +0,0 @@ -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/instagram/main.go b/instagram/main.go index c37b71f..fb9157d 100644 --- a/instagram/main.go +++ b/instagram/main.go @@ -7,8 +7,6 @@ import ( "strings" "nulo.in/dlbot/common" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) type Instagram struct { @@ -17,25 +15,22 @@ type Instagram struct { var Responder *Instagram = &Instagram{} -func (r *Instagram) Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { +func (r *Instagram) Respond(url *url.URL) (*common.Uploadable, common.Error) { if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" { - return common.NotValid + return nil, common.NotValid } if strings.Index(url.Path, "/reel/") != 0 { - return common.NotValid + return nil, common.NotValid } - log.Printf("Downloading %s", url.String()) lookup, err := r.lookup(url.String()) if err != nil { log.Println(err) - return common.HadError + return nil, common.HadError } - log.Println(lookup) - res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup.VideoUrl)) - res.ReplyToMessageID = update.Message.MessageID - res.Caption = "@" + lookup.Author - bot.Send(res) - return common.Uploaded + return &common.Uploadable{ + Url: lookup.VideoUrl, + Caption: "@" + lookup.Author, + }, common.OK } diff --git a/main.go b/main.go index 644442f..2352e34 100644 --- a/main.go +++ b/main.go @@ -41,35 +41,45 @@ func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) url, err := url.Parse(urlString) if err != nil { if explicit { - bot.Send(common.RespondWithMany(msg, "No se pudo detectar la URL ", urlString, ".")) + bot.Send(respondWithMany(msg, "No se pudo detectar la URL ", urlString, ".")) } continue } - var result common.Result + log.Printf("Downloading %s", url.String()) + + var uploadable *common.Uploadable + var érror common.Error for _, responder := range config.Responders { - result = responder.Respond(bot, update, url) - if result != common.NotValid { + uploadable, érror = responder.Respond(url) + if érror != common.NotValid { break } } - if explicit && result == common.NotValid { - bot.Send(common.RespondWithMany(msg, "La URL ", urlString, " no es compatible con este bot.")) + if uploadable != nil { + res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(uploadable.Url)) + res.ReplyToMessageID = update.Message.MessageID + res.Caption = uploadable.Caption + bot.Send(res) + } + + if explicit && érror == common.NotValid { + bot.Send(respondWithMany(msg, "La URL ", urlString, " no es compatible con este bot.")) continue } - if result == common.HadError || result == common.Uploaded { + if érror == common.HadError || érror == common.OK { hasDownloadables = true } - if result == common.HadError { - bot.Send(common.RespondWithMany(update.Message, "Hubo un error al descargar ", urlString, ".")) + if érror == common.HadError { + bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", urlString, ".")) continue } } if !hasDownloadables && explicit { - bot.Send(common.RespondWithMany(msg, "No encontré URLs descargables en ese mensaje.")) + bot.Send(respondWithMany(msg, "No encontré URLs descargables en ese mensaje.")) } } @@ -113,3 +123,19 @@ func main() { go config.handleMessage(bot, update) } } + +func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig { + res := tgbotapi.NewMessage(msg.Chat.ID, str) + res.ReplyToMessageID = msg.MessageID + res.DisableWebPagePreview = true + res.ParseMode = "markdown" + return res +} + +func respondWithMany(msg *tgbotapi.Message, s ...string) tgbotapi.MessageConfig { + var res strings.Builder + for _, v := range s { + res.WriteString(v) + } + return respondWith(msg, res.String()) +} diff --git a/tiktok/go.mod b/tiktok/go.mod index 9d33f45..c016ab9 100644 --- a/tiktok/go.mod +++ b/tiktok/go.mod @@ -2,8 +2,6 @@ module nulo.in/dlbot/tiktok/v4 go 1.18 -require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 - -require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 // indirect +require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 replace nulo.in/dlbot/common => ../common diff --git a/tiktok/go.sum b/tiktok/go.sum index db8e45c..e69de29 100644 --- a/tiktok/go.sum +++ b/tiktok/go.sum @@ -1,2 +0,0 @@ -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/tiktok/main.go b/tiktok/main.go index 381c7a5..f2f1c79 100644 --- a/tiktok/main.go +++ b/tiktok/main.go @@ -6,8 +6,6 @@ import ( "net/url" "nulo.in/dlbot/common" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) // Gracias a https://github.com/Xenzi-XN1/Tiktok-Download @@ -19,25 +17,20 @@ type TikTok struct { var Responder *TikTok = &TikTok{} -func (r *TikTok) Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { +func (r *TikTok) Respond(url *url.URL) (*common.Uploadable, common.Error) { if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" { - return common.NotValid + return nil, common.NotValid } urlString := url.String() // tikmate no entiende tiktok.com url.Host = "vm.tiktok.com" - log.Printf("Downloading %s", urlString) lookup, err := r.lookup(urlString) if err != nil { log.Println(err) - return common.HadError + return nil, common.HadError } - log.Println(lookup) - res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup)) - res.ReplyToMessageID = update.Message.MessageID - bot.Send(res) - return common.Uploaded + return &common.Uploadable{Url: lookup}, common.OK }