This repository has been archived on 2024-01-17. You can view files and clone it, but cannot push or open issues or pull requests.
dlbot4/tiktok/main.go

130 lines
2.8 KiB
Go
Raw Normal View History

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
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
}
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, "."))
}
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."))
}
continue
2022-08-10 18:41:51 +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, "."))
continue
2022-08-10 18:41:51 +00:00
}
log.Println(lookup)
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)
}
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)
}
}