Responders no saben sobre Telegram

This commit is contained in:
Cat /dev/Nulo 2023-01-04 13:53:23 -03:00
parent 8f5ecef858
commit 92a46e38fb
10 changed files with 59 additions and 72 deletions

View file

@ -1,5 +1,3 @@
module nulo.in/dlbot/common module nulo.in/dlbot/common
go 1.19 go 1.19
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect

View file

@ -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=

View file

@ -2,34 +2,20 @@ package common
import ( import (
"net/url" "net/url"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type Result uint8
type Responder interface { 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 ( const (
NotValid Result = iota OK Error = iota
NotValid
HadError 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())
}

View file

@ -4,7 +4,4 @@ go 1.19
replace nulo.in/dlbot/common => ../common replace nulo.in/dlbot/common => ../common
require ( require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
)

View file

@ -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=

View file

@ -7,8 +7,6 @@ import (
"strings" "strings"
"nulo.in/dlbot/common" "nulo.in/dlbot/common"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type Instagram struct { type Instagram struct {
@ -17,25 +15,22 @@ type Instagram struct {
var Responder *Instagram = &Instagram{} 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" { if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" {
return common.NotValid return nil, common.NotValid
} }
if strings.Index(url.Path, "/reel/") != 0 { 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()) lookup, err := r.lookup(url.String())
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return common.HadError return nil, common.HadError
} }
log.Println(lookup)
res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup.VideoUrl)) return &common.Uploadable{
res.ReplyToMessageID = update.Message.MessageID Url: lookup.VideoUrl,
res.Caption = "@" + lookup.Author Caption: "@" + lookup.Author,
bot.Send(res) }, common.OK
return common.Uploaded
} }

46
main.go
View file

@ -41,35 +41,45 @@ func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update)
url, err := url.Parse(urlString) url, err := url.Parse(urlString)
if err != nil { if err != nil {
if explicit { 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 continue
} }
var result common.Result log.Printf("Downloading %s", url.String())
var uploadable *common.Uploadable
var érror common.Error
for _, responder := range config.Responders { for _, responder := range config.Responders {
result = responder.Respond(bot, update, url) uploadable, érror = responder.Respond(url)
if result != common.NotValid { if érror != common.NotValid {
break break
} }
} }
if explicit && result == common.NotValid { if uploadable != nil {
bot.Send(common.RespondWithMany(msg, "La URL ", urlString, " no es compatible con este bot.")) 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 continue
} }
if result == common.HadError || result == common.Uploaded { if érror == common.HadError || érror == common.OK {
hasDownloadables = true hasDownloadables = true
} }
if result == common.HadError { if érror == common.HadError {
bot.Send(common.RespondWithMany(update.Message, "Hubo un error al descargar ", urlString, ".")) bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", urlString, "."))
continue continue
} }
} }
if !hasDownloadables && explicit { 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) 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())
}

View file

@ -2,8 +2,6 @@ module nulo.in/dlbot/tiktok/v4
go 1.18 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
require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 // indirect
replace nulo.in/dlbot/common => ../common replace nulo.in/dlbot/common => ../common

View file

@ -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=

View file

@ -6,8 +6,6 @@ import (
"net/url" "net/url"
"nulo.in/dlbot/common" "nulo.in/dlbot/common"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
// Gracias a https://github.com/Xenzi-XN1/Tiktok-Download // Gracias a https://github.com/Xenzi-XN1/Tiktok-Download
@ -19,25 +17,20 @@ type TikTok struct {
var Responder *TikTok = &TikTok{} 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" { if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
return common.NotValid return nil, common.NotValid
} }
urlString := url.String() urlString := url.String()
// tikmate no entiende tiktok.com // tikmate no entiende tiktok.com
url.Host = "vm.tiktok.com" url.Host = "vm.tiktok.com"
log.Printf("Downloading %s", urlString)
lookup, err := r.lookup(urlString) lookup, err := r.lookup(urlString)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return common.HadError return nil, common.HadError
} }
log.Println(lookup)
res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup)) return &common.Uploadable{Url: lookup}, common.OK
res.ReplyToMessageID = update.Message.MessageID
bot.Send(res)
return common.Uploaded
} }