Telegram bot for downloading media from any site using yt-dlp. Supports audio/video/both, quality selection, language picker, container format selection, playlist pagination, progress updates, per-user quotas, user preferences, download history, cancel at any step, polling and webhook modes, proxy support, and SQLite persistence.
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
|
|
tgbot "github.com/go-telegram/bot"
|
|
|
|
"uptodownBot/internal/handler"
|
|
)
|
|
|
|
func startWebhook(ctx context.Context, b *tgbot.Bot, h *handler.Handler, webhookURL string) {
|
|
if _, err := b.DeleteWebhook(ctx, &tgbot.DeleteWebhookParams{}); err != nil {
|
|
slog.Warn("failed to delete webhook", "error", err)
|
|
}
|
|
|
|
if _, err := b.SetWebhook(ctx, &tgbot.SetWebhookParams{URL: webhookURL}); err != nil {
|
|
slog.Error("set webhook", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
listenAddr := getEnvDefault("BOT_LISTEN", ":8080")
|
|
tlsCert := os.Getenv("BOT_TLS_CERT")
|
|
tlsKey := os.Getenv("BOT_TLS_KEY")
|
|
|
|
if tlsCert != "" && tlsKey != "" {
|
|
slog.Info("starting HTTPS webhook", "addr", listenAddr)
|
|
go func() {
|
|
if err := http.ListenAndServeTLS(listenAddr, tlsCert, tlsKey, b.WebhookHandler()); err != nil {
|
|
slog.Error("HTTPS server", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
} else {
|
|
slog.Info("starting HTTP webhook (TLS by reverse proxy)", "addr", listenAddr)
|
|
go func() {
|
|
if err := http.ListenAndServe(listenAddr, b.WebhookHandler()); err != nil {
|
|
slog.Error("HTTP server", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
}
|
|
|
|
slog.Info("webhook registered", "url", webhookURL)
|
|
b.StartWebhook(ctx)
|
|
}
|