2023-01-04 16:21:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-22 00:08:39 +00:00
|
|
|
"errors"
|
2023-05-09 23:40:54 +00:00
|
|
|
"io"
|
2023-01-04 16:21:00 +00:00
|
|
|
"log"
|
2023-05-09 23:40:54 +00:00
|
|
|
"net/http"
|
2023-05-09 23:59:23 +00:00
|
|
|
"net/url"
|
2023-01-04 16:21:00 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-05-09 23:59:23 +00:00
|
|
|
"unicode/utf16"
|
2023-01-04 16:21:00 +00:00
|
|
|
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
"nulo.in/dlbot/common"
|
|
|
|
"nulo.in/dlbot/instagram"
|
|
|
|
"nulo.in/dlbot/tiktok"
|
2023-12-29 02:29:28 +00:00
|
|
|
"nulo.in/dlbot/youtube"
|
2023-01-04 16:21:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-01-04 16:36:25 +00:00
|
|
|
Responders []common.Responder
|
2023-01-04 16:21:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 23:40:54 +00:00
|
|
|
type FileURL string
|
|
|
|
|
|
|
|
func (fu FileURL) NeedsUpload() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fu FileURL) UploadData() (string, io.Reader, error) {
|
|
|
|
res, err := http.Get(string(fu))
|
|
|
|
if err != nil {
|
2023-08-13 17:08:05 +00:00
|
|
|
return "", nil, errors.Join(errors.New("error while uploading FileURL"), err)
|
2023-05-09 23:40:54 +00:00
|
|
|
}
|
2023-05-17 02:33:40 +00:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2023-08-13 17:08:05 +00:00
|
|
|
return "", nil, errors.New("error while uploading FileURL: " + res.Status)
|
2023-05-17 02:33:40 +00:00
|
|
|
}
|
2023-05-09 23:40:54 +00:00
|
|
|
return "url.mp4", res.Body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fu FileURL) SendData() string {
|
|
|
|
panic("we")
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:21:00 +00:00
|
|
|
func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
|
|
|
var explicit bool
|
|
|
|
|
2023-06-22 00:08:04 +00:00
|
|
|
send := func(c tgbotapi.Chattable) {
|
|
|
|
_, err := bot.Send(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("No pude enviar un mensaje porque", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:21:00 +00:00
|
|
|
msg := update.Message
|
|
|
|
if strings.HasPrefix(msg.Text, "/dl") || msg.Chat.IsPrivate() {
|
|
|
|
explicit = true
|
|
|
|
}
|
|
|
|
|
|
|
|
searchMsg := msg
|
|
|
|
if msg.ReplyToMessage != nil && explicit {
|
|
|
|
searchMsg = msg.ReplyToMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
hasDownloadables := false
|
|
|
|
|
|
|
|
for i := 0; i < len(searchMsg.Entities); i++ {
|
|
|
|
e := searchMsg.Entities[i]
|
2023-05-09 23:59:23 +00:00
|
|
|
if !e.IsURL() {
|
2023-01-04 16:21:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-09 23:59:23 +00:00
|
|
|
// ugh https://github.com/go-telegram-bot-api/telegram-bot-api/issues/231
|
2023-05-15 11:16:14 +00:00
|
|
|
text := searchMsg.Text
|
2023-05-09 23:59:23 +00:00
|
|
|
utfEncodedString := utf16.Encode([]rune(text))
|
|
|
|
runeString := utf16.Decode(utfEncodedString[e.Offset : e.Offset+e.Length])
|
|
|
|
text = string(runeString)
|
|
|
|
|
|
|
|
url, err := url.Parse(text)
|
2023-01-04 16:21:00 +00:00
|
|
|
if err != nil {
|
|
|
|
if explicit {
|
2023-05-09 23:59:23 +00:00
|
|
|
bot.Send(respondWithMany(msg, "No se pudo detectar la URL %s.", text))
|
2023-01-04 16:21:00 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:53:23 +00:00
|
|
|
log.Printf("Downloading %s", url.String())
|
|
|
|
|
|
|
|
var uploadable *common.Uploadable
|
|
|
|
var érror common.Error
|
2023-01-04 16:36:25 +00:00
|
|
|
for _, responder := range config.Responders {
|
2023-01-04 16:53:23 +00:00
|
|
|
uploadable, érror = responder.Respond(url)
|
|
|
|
if érror != common.NotValid {
|
2023-01-04 16:21:00 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:53:23 +00:00
|
|
|
if uploadable != nil {
|
2023-12-29 02:11:52 +00:00
|
|
|
if uploadable.ImagesWithAudio != nil {
|
|
|
|
var files []interface{}
|
|
|
|
for _, u := range uploadable.ImagesWithAudio.ImageUrls {
|
|
|
|
files = append(files,
|
|
|
|
tgbotapi.NewInputMediaPhoto(tgbotapi.FileURL(u)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
log.Println(files)
|
|
|
|
mediaGroup := tgbotapi.NewMediaGroup(update.Message.Chat.ID, files)
|
|
|
|
mediaGroup.ReplyToMessageID = update.Message.MessageID
|
|
|
|
|
|
|
|
msgs, err := bot.SendMediaGroup(mediaGroup)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error subiendo", url.String(), err)
|
|
|
|
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
|
|
|
|
}
|
|
|
|
res := tgbotapi.NewAudio(update.Message.Chat.ID, FileURL(uploadable.AudioUrl))
|
|
|
|
res.ReplyToMessageID = msgs[0].MessageID
|
|
|
|
_, err = bot.Send(res)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error subiendo", url.String(), err)
|
|
|
|
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res := tgbotapi.NewVideo(update.Message.Chat.ID, FileURL(uploadable.VideoUrl))
|
|
|
|
res.ReplyToMessageID = update.Message.MessageID
|
|
|
|
res.Caption = uploadable.Caption
|
|
|
|
_, err := bot.Send(res)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error subiendo", url.String(), err)
|
|
|
|
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
|
|
|
|
}
|
|
|
|
|
2023-05-09 23:40:43 +00:00
|
|
|
}
|
2023-01-04 16:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if explicit && érror == common.NotValid {
|
2023-05-09 23:50:34 +00:00
|
|
|
bot.Send(respondWithMany(msg, "La URL ", url.String(), " no es compatible con este bot."))
|
2023-01-04 16:21:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:53:23 +00:00
|
|
|
if érror == common.HadError || érror == common.OK {
|
2023-01-04 16:21:00 +00:00
|
|
|
hasDownloadables = true
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:53:23 +00:00
|
|
|
if érror == common.HadError {
|
2023-06-22 00:08:04 +00:00
|
|
|
send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
|
2023-01-04 16:21:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasDownloadables && explicit {
|
2023-01-04 16:53:23 +00:00
|
|
|
bot.Send(respondWithMany(msg, "No encontré URLs descargables en ese mensaje."))
|
2023-01-04 16:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
config := Config{
|
2023-01-04 16:36:25 +00:00
|
|
|
Responders: []common.Responder{
|
|
|
|
instagram.Responder,
|
|
|
|
tiktok.Responder,
|
2023-12-29 02:29:28 +00:00
|
|
|
youtube.Responder,
|
2023-01-04 16:21:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
token := os.Getenv("TELEGRAM_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
log.Panic("No telegram token")
|
|
|
|
}
|
|
|
|
|
|
|
|
var debug bool
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
debug = true
|
|
|
|
}
|
|
|
|
|
2023-08-13 19:12:28 +00:00
|
|
|
apiEndpoint := os.Getenv("TELEGRAM_API_ENDPOINT")
|
|
|
|
var bot *tgbotapi.BotAPI
|
|
|
|
var err error
|
|
|
|
if apiEndpoint == "" {
|
|
|
|
bot, err = tgbotapi.NewBotAPI(token)
|
|
|
|
} else {
|
|
|
|
log.Printf("Setting endpoint to %s", apiEndpoint)
|
|
|
|
bot, err = tgbotapi.NewBotAPIWithAPIEndpoint(token, apiEndpoint)
|
|
|
|
}
|
2023-01-04 16:21:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.Debug = debug
|
|
|
|
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
|
2023-08-13 19:12:28 +00:00
|
|
|
if len(os.Args) > 1 && os.Args[1] == "logout" {
|
|
|
|
logout := tgbotapi.LogOutConfig{}
|
|
|
|
bot.Send(logout)
|
|
|
|
log.Println("Logged out.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:21:00 +00:00
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
|
|
|
|
for update := range updates {
|
|
|
|
if update.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go config.handleMessage(bot, update)
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 16:53:23 +00:00
|
|
|
|
|
|
|
func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig {
|
|
|
|
res := tgbotapi.NewMessage(msg.Chat.ID, str)
|
|
|
|
res.ReplyToMessageID = msg.MessageID
|
|
|
|
res.DisableWebPagePreview = true
|
|
|
|
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())
|
|
|
|
}
|