Clean up: remove unused packages, switch to slog, tidy Makefile
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -16,6 +16,10 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
||||
AddSource: true,
|
||||
Level: slog.LevelInfo,
|
||||
})))
|
||||
godotenv.Load()
|
||||
|
||||
token := os.Getenv("BOT_TOKEN")
|
||||
@@ -25,29 +29,34 @@ func main() {
|
||||
|
||||
botAPI, err := tgbotapi.NewBotAPIWithClient(token, tgbotapi.APIEndpoint, httpClient)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
slog.Error("failed to create bot API", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
dbStore, err := db.NewStore(dbPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
slog.Error("failed to open database", "path", dbPath, "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
allowedUsers := parseAllowedUsers(os.Getenv("BOT_ALLOWED_USERS"))
|
||||
|
||||
handler := bot.NewHandler(botAPI, dbStore, allowedUsers)
|
||||
|
||||
log.Printf("Bot started. Allowed users: %d", len(allowedUsers))
|
||||
slog.Info("bot started",
|
||||
"bot_user", botAPI.Self.UserName,
|
||||
"allowed_users", len(allowedUsers),
|
||||
"polling", true,
|
||||
)
|
||||
|
||||
webhookURL := os.Getenv("BOT_WEBHOOK_URL")
|
||||
if webhookURL != "" {
|
||||
startWebhook(botAPI, handler, webhookURL)
|
||||
} else {
|
||||
if _, err := botAPI.Request(tgbotapi.DeleteWebhookConfig{}); err != nil {
|
||||
log.Printf("DeleteWebhook: %v", err)
|
||||
slog.Error("delete webhook", "error", err)
|
||||
}
|
||||
|
||||
// Daily report scheduler
|
||||
go dailyReportScheduler(handler, dbStore)
|
||||
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
@@ -74,6 +83,8 @@ func parseAllowedUsers(raw string) map[int64]bool {
|
||||
id, err := strconv.ParseInt(s, 10, 64)
|
||||
if err == nil {
|
||||
m[id] = true
|
||||
} else {
|
||||
slog.Warn("invalid user ID in BOT_ALLOWED_USERS", "value", s)
|
||||
}
|
||||
}
|
||||
return m
|
||||
@@ -86,16 +97,20 @@ func dailyReportScheduler(h *bot.Handler, store *db.Store) {
|
||||
if now.After(next) {
|
||||
next = next.AddDate(0, 0, 1)
|
||||
}
|
||||
slog.Info("daily report scheduler", "next_run", next)
|
||||
time.Sleep(time.Until(next))
|
||||
|
||||
chatIDStr, err := store.GetSetting("chat_id")
|
||||
if err != nil {
|
||||
slog.Error("daily report: chat_id not found")
|
||||
continue
|
||||
}
|
||||
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
|
||||
if err != nil {
|
||||
slog.Error("daily report: invalid chat_id", "value", chatIDStr)
|
||||
continue
|
||||
}
|
||||
slog.Info("sending daily report", "chat_id", chatID)
|
||||
h.SendDailyReport(chatID)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user