diff --git a/.env.example b/.env.example index 81ebafc..d2406de 100644 --- a/.env.example +++ b/.env.example @@ -1,15 +1,23 @@ -BOT_API_URL=https://api.telegram.org -BOT_TOKEN=12345678:ABC-DEF1234ghIkl-zyx57W2P0s - +# database DB_HOST=localhost DB_PORT=3306 DB_NAME=govd DB_USER=govd DB_PASSWORD=password -REPO_URL=https://github.com/govdbot/govd +# gotgbot +BOT_API_URL=https://api.telegram.org +BOT_TOKEN=12345678:ABC-DEF1234ghIkl-zyx57W2P0s +CONCURRENT_UPDATES=-1 +LOG_ERRORS=0 + +# downloads +DOWNLOADS_DIR=downloads # proxy HTTP_PROXY= HTTPS_PROXY= -NO_PROXY= \ No newline at end of file +NO_PROXY= + +# misc +REPO_URL=https://github.com/govdbot/govd \ No newline at end of file diff --git a/bot/main.go b/bot/main.go index 4df32e9..a720aa4 100644 --- a/bot/main.go +++ b/bot/main.go @@ -3,6 +3,7 @@ package bot import ( "log" "os" + "strconv" "time" botHandlers "govd/bot/handlers" @@ -33,12 +34,29 @@ func Start() { if err != nil { log.Fatalf("failed to create bot: %v", err) } + concurrentUpdates, err := strconv.Atoi(os.Getenv("CONCURRENT_UPDATES")) + if err != nil { + log.Println("failed to parse CONCURRENT_UPDATES env, using 50") + concurrentUpdates = 50 + } + logDispatcherErrors, err := strconv.ParseBool(os.Getenv("LOG_DISPATCHER_ERRORS")) + if err != nil { + log.Println("failed to parse LOG_ERRORS env, using false") + logDispatcherErrors = false + } dispatcher := ext.NewDispatcher(&ext.DispatcherOpts{ Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction { - // log.Println("an error occurred while handling update:", err.Error()) + if logDispatcherErrors { + log.Printf("an error occurred while handling update: %v", err) + } return ext.DispatcherActionNoop }, - MaxRoutines: 500, + Panic: func(b *gotgbot.Bot, ctx *ext.Context, r interface{}) { + if logDispatcherErrors { + log.Printf("panic occurred while handling update: %v", r) + } + }, + MaxRoutines: concurrentUpdates, }) updater := ext.NewUpdater(dispatcher, nil) registerHandlers(dispatcher) diff --git a/util/download.go b/util/download.go index 8fc6d1b..644a72e 100644 --- a/util/download.go +++ b/util/download.go @@ -21,11 +21,15 @@ import ( ) func DefaultConfig() *models.DownloadConfig { + downloadsDir := os.Getenv("DOWNLOADS_DIR") + if downloadsDir == "" { + downloadsDir = "downloads" + } return &models.DownloadConfig{ ChunkSize: 10 * 1024 * 1024, // 10MB Concurrency: 4, Timeout: 30 * time.Second, - DownloadDir: "downloads", + DownloadDir: downloadsDir, RetryAttempts: 3, RetryDelay: 2 * time.Second, Remux: true,