2022-08-10 18:41:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gracias a https://github.com/Xenzi-XN1/Tiktok-Download
|
|
|
|
// por enseñarme tikmate.app
|
|
|
|
|
|
|
|
func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig {
|
|
|
|
res := tgbotapi.NewMessage(msg.Chat.ID, str)
|
|
|
|
res.ReplyToMessageID = msg.MessageID
|
|
|
|
res.DisableWebPagePreview = true
|
2022-08-27 22:50:55 +00:00
|
|
|
res.ParseMode = "markdown"
|
2022-08-10 18:41:51 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
|
|
|
var explicit bool
|
|
|
|
|
|
|
|
msg := update.Message
|
|
|
|
if strings.HasPrefix(msg.Text, "/dl") || msg.Chat.IsPrivate() {
|
|
|
|
explicit = true
|
|
|
|
}
|
|
|
|
|
|
|
|
searchMsg := msg
|
|
|
|
if msg.ReplyToMessage != nil && explicit {
|
|
|
|
searchMsg = msg.ReplyToMessage
|
|
|
|
}
|
|
|
|
|
2022-08-27 22:37:27 +00:00
|
|
|
hasDownloadables := false
|
2022-08-10 18:41:51 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(searchMsg.Entities); i++ {
|
|
|
|
e := searchMsg.Entities[i]
|
|
|
|
if e.Type != "url" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
urlString := searchMsg.Text[e.Offset : e.Offset+e.Length]
|
|
|
|
url, err := url.Parse(urlString)
|
|
|
|
if err != nil {
|
|
|
|
if explicit {
|
|
|
|
bot.Send(respondWithMany(msg, "No se pudo detectar la URL ", urlString, "."))
|
|
|
|
}
|
2022-08-27 22:38:04 +00:00
|
|
|
continue
|
2022-08-10 18:41:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:54:58 +00:00
|
|
|
if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
|
2022-08-10 18:41:51 +00:00
|
|
|
if explicit {
|
|
|
|
bot.Send(respondWithMany(msg, "La URL ", urlString, " no es de TikTok."))
|
|
|
|
}
|
2022-08-27 22:38:04 +00:00
|
|
|
continue
|
2022-08-10 18:41:51 +00:00
|
|
|
}
|
2022-08-27 22:37:27 +00:00
|
|
|
hasDownloadables = true
|
2022-11-06 19:54:58 +00:00
|
|
|
// tikmate no entiende tiktok.com
|
|
|
|
url.Host = "vm.tiktok.com"
|
2022-08-10 18:41:51 +00:00
|
|
|
|
|
|
|
log.Printf("Downloading %s", urlString)
|
2022-11-06 19:54:58 +00:00
|
|
|
lookup, err := Lookup(url.String())
|
2022-08-10 18:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, "."))
|
2022-08-27 22:38:04 +00:00
|
|
|
continue
|
2022-08-10 18:41:51 +00:00
|
|
|
}
|
|
|
|
log.Println(lookup)
|
2022-08-27 22:50:55 +00:00
|
|
|
if !lookup.Success {
|
|
|
|
if len(lookup.Message) > 0 {
|
|
|
|
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, ": `", lookup.Message, "`"))
|
|
|
|
} else {
|
|
|
|
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, "."))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-08-10 18:41:51 +00:00
|
|
|
|
|
|
|
res := tgbotapi.NewVideo(msg.Chat.ID, *lookup)
|
|
|
|
res.ReplyToMessageID = msg.MessageID
|
|
|
|
|
|
|
|
bot.Send(res)
|
|
|
|
}
|
2022-08-27 22:37:27 +00:00
|
|
|
if !hasDownloadables && explicit {
|
|
|
|
bot.Send(respondWithMany(msg, "No encontré URLs descargables en ese mensaje."))
|
|
|
|
}
|
2022-08-10 18:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
token := os.Getenv("TELEGRAM_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
log.Panic("No telegram token")
|
|
|
|
}
|
|
|
|
|
|
|
|
var debug bool
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
debug = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bot, err := tgbotapi.NewBotAPI(token)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.Debug = debug
|
|
|
|
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
|
|
|
|
for update := range updates {
|
|
|
|
if update.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go handleMessage(bot, update)
|
|
|
|
}
|
|
|
|
}
|