From 92191d0b7442351800969513792697ead0be4766 Mon Sep 17 00:00:00 2001 From: Nulo Date: Sun, 13 Aug 2023 16:12:28 -0300 Subject: [PATCH] usar servidor de bot custom --- .woodpecker.yml | 14 ++++++++++ Taskfile.yml | 22 +++++----------- main.go | 17 +++++++++++- readme.md | 29 +++++++++++++++++++++ telegram-bot-api-container/Containerfile | 33 ++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 telegram-bot-api-container/Containerfile diff --git a/.woodpecker.yml b/.woodpecker.yml index d9856fc..cd19e62 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -13,3 +13,17 @@ pipeline: - echo $REGISTRY_SECRET | apko login --username Nulo --password-stdin gitea.nulo.in - apko publish --debug --arch x86_64 --repository-append "$(pwd)/packages" --keyring-append melange.rsa.pub apko.yaml gitea.nulo.in/nulo/dlbot4:latest secrets: [REGISTRY_SECRET] + + telegram-bot-api: + image: docker.io/woodpeckerci/plugin-docker-buildx + settings: + repo: gitea.nulo.in/Nulo/dlbot4/telegram-bot-api + context: ./telegram-bot-api-container + registry: https://gitea.nulo.in + username: Nulo + password: + from_secret: registry_secret + secrets: [REGISTRY_SECRET] + when: + branch: "antifascista" + event: "push" diff --git a/Taskfile.yml b/Taskfile.yml index 58a0afb..c830184 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -11,20 +11,12 @@ tasks: - ssh -p993 root@nulo.in sv stop dlbot - scp -P993 dlbot dlbot@nulo.in:/home/dlbot/bin/ - ssh -p993 root@nulo.in sv start dlbot - melange: - cmds: - - podman run --privileged --rm -it - -v "$PWD":/work:Z - -v "$PWD"/.melange-cache:/work/melange-cache:Z - cgr.dev/chainguard/melange build - packaging/melange.yml - --arch x86_64 --signing-key packaging/melange.rsa - apko: + ko: cmds: - podman run --rm -it - -v "$PWD":/work:Z - cgr.dev/chainguard/apko:latest publish - --debug --arch x86_64 - --repository-append "/work/packages" --keyring-append packaging/melange.rsa.pub - packaging/apko.yaml - gitea.nulo.in/nulo/dlbot:latest + -v ~/.docker/:/docker-config:Z,ro + -v $PWD:/work:Z + --workdir=/work + -e KO_DOCKER_REPO=gitea.nulo.in/nulo/dlbot4 + -e DOCKER_CONFIG=/docker-config + cgr.dev/chainguard/ko build --bare . diff --git a/main.go b/main.go index 614717c..bc21010 100644 --- a/main.go +++ b/main.go @@ -142,7 +142,15 @@ func main() { debug = true } - bot, err := tgbotapi.NewBotAPI(token) + 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) + } if err != nil { log.Panic(err) } @@ -151,6 +159,13 @@ func main() { log.Printf("Authorized on account %s", bot.Self.UserName) + if len(os.Args) > 1 && os.Args[1] == "logout" { + logout := tgbotapi.LogOutConfig{} + bot.Send(logout) + log.Println("Logged out.") + return + } + u := tgbotapi.NewUpdate(0) u.Timeout = 60 diff --git a/readme.md b/readme.md index 012d770..8e89c53 100644 --- a/readme.md +++ b/readme.md @@ -7,3 +7,32 @@ Un bot para Telegram que permite descargar videos de distintos lugares. ## TikTok Previamente este bot descargaba directo de TikTok a través de distintas APIs internas, usando programas externos como yt-dlp. Lamentablemente TikTok seguía parcheando estas APIs internas, que hacía mantener un sideproject molesto. Por suerte, encontré [TikMate](https://tikmate.app) que es un sitio que los descarga por vos. Asumo que tiene desarrollador(es?) que mantienen el sitio. Así, uso su API interna (muy simple) y listo. + +## Correr tu propio servidor de bot + +El servidor de bots de Telegram por defecto (`https://api.telegram.org`) tiene un limite de subida de 50MB, y tenés que usar tu [propio servidor](https://github.com/tdlib/telegram-bot-api) para poder subir hasta 2000MB. + +En ./telegram-bot-api-container hay un Containerfile para hostear este servidor. + +Cuando ya tengas tu propio servidor, empezá deslogeandote del oficial (no vas a poder volver a logearte por 10 minutos): + +``` +dlbot logout +``` + +Después, tenés que reiniciar dlbot con el endpoint especificado con este formato: `$endpoint/bot%s/%s"`. Un ejemplo en docker-compose: + +``` + dlbot: + image: gitea.nulo.in/nulo/dlbot4 + environment: + TELEGRAM_TOKEN: "${DLBOT_TELEGRAM_TOKEN}" + TELEGRAM_API_ENDPOINT: http://dlbot-telegram-bot-api:8081/bot%s/%s + links: + - dlbot-telegram-bot-api + dlbot-telegram-bot-api: + image: gitea.nulo.in/nulo/dlbot4/telegram-bot-api + entrypoint: ["telegram-bot-api", "--api-id=$DLBOT_TELEGRAM_API_ID", "--api-hash=$DLBOT_TELEGRAM_API_HASH", "--local"] +``` + +Podés ver como está hecho en producción [en la repo de infra](https://gitea.nulo.in/Nulo/infra/commit/1067c632d203f7b7304fabd7bc4e818eb9d90386). diff --git a/telegram-bot-api-container/Containerfile b/telegram-bot-api-container/Containerfile new file mode 100644 index 0000000..6a83f74 --- /dev/null +++ b/telegram-bot-api-container/Containerfile @@ -0,0 +1,33 @@ +FROM docker.io/alpine:3.18 AS build + +RUN apk add --no-cache \ + make cmake \ + g++ \ + gperf \ + linux-headers openssl-dev \ + zlib-dev \ + git + +RUN git clone --recursive https://github.com/tdlib/telegram-bot-api.git && \ + cd telegram-bot-api/ && \ + mkdir build && \ + cd build + +WORKDIR telegram-bot-api/build + +RUN cmake .. -DCMAKE_BUILD_TYPE=Release \ + && cmake --build . --target install + +FROM docker.io/alpine:3.18 +ENV TZ=America/Argentina/Buenos_Aires + +RUN apk add --no-cache \ + openssl \ + zlib \ + libstdc++ + +COPY --from=build /usr/local/bin/telegram-bot-api /usr/local/bin/telegram-bot-api + +ENTRYPOINT telegram-bot-api + +EXPOSE 8081