Add BOT_ALLOWED_USERS, fix SetColWidth params, drop fmt import, save chat_id on start
This commit is contained in:
@@ -4,23 +4,23 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"worktimeBot/internal/bot"
|
||||
"worktimeBot/internal/db"
|
||||
"worktimeBot/pkg/i18n"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load .env file if it exists (ignored when running inside Docker with env vars)
|
||||
_ = godotenv.Load()
|
||||
godotenv.Load()
|
||||
|
||||
token := os.Getenv("BOT_TOKEN")
|
||||
dbPath := getEnvDefault("DB_PATH", "db.sqlite3")
|
||||
|
||||
// Default HTTP client respects HTTP_PROXY env var automatically
|
||||
httpClient := http.DefaultClient
|
||||
|
||||
botAPI, err := tgbotapi.NewBotAPIWithClient(token, tgbotapi.APIEndpoint, httpClient)
|
||||
@@ -33,21 +33,21 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
translator, err := i18n.NewTranslator("en")
|
||||
if err != nil {
|
||||
log.Fatal("i18n: ", err)
|
||||
}
|
||||
allowedUsers := parseAllowedUsers(os.Getenv("BOT_ALLOWED_USERS"))
|
||||
|
||||
handler := bot.NewHandler(botAPI, dbStore, translator)
|
||||
handler := bot.NewHandler(botAPI, dbStore, allowedUsers)
|
||||
|
||||
webhookURL := os.Getenv("BOT_WEBHOOK_URL")
|
||||
if webhookURL != "" {
|
||||
startWebhook(botAPI, handler, webhookURL)
|
||||
} else {
|
||||
// Fallback to long‑polling — remove any stale webhook first
|
||||
if _, err := botAPI.Request(tgbotapi.DeleteWebhookConfig{}); err != nil {
|
||||
log.Printf("DeleteWebhook: %v", err)
|
||||
}
|
||||
|
||||
// Daily report scheduler
|
||||
go dailyReportScheduler(handler, dbStore)
|
||||
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 30
|
||||
updates := botAPI.GetUpdatesChan(u)
|
||||
@@ -62,6 +62,42 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func parseAllowedUsers(raw string) map[int64]bool {
|
||||
m := make(map[int64]bool)
|
||||
for _, s := range strings.Split(raw, ",") {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
id, err := strconv.ParseInt(s, 10, 64)
|
||||
if err == nil {
|
||||
m[id] = true
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func dailyReportScheduler(h *bot.Handler, store *db.Store) {
|
||||
for {
|
||||
now := time.Now()
|
||||
next := time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
|
||||
if now.After(next) {
|
||||
next = next.AddDate(0, 0, 1)
|
||||
}
|
||||
time.Sleep(time.Until(next))
|
||||
|
||||
chatIDStr, err := store.GetSetting("chat_id")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
h.SendDailyReport(chatID)
|
||||
}
|
||||
}
|
||||
|
||||
func getEnvDefault(key, defaultValue string) string {
|
||||
if val := os.Getenv(key); val != "" {
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user