Files
worktimeBot/cmd/bot/main.go
db123 4a5778afe5 feat: add inline keyboard, /start command, and callback handlers
- Show main menu with Clock In/Out, Report, Export, Remote, Day Off buttons
- Each action result includes a 'Back to Menu' button
- Handle callback queries alongside text commands
- Delete stale webhook before switching to polling
- Log i18n initialization error instead of silently ignoring it
2026-06-23 21:42:52 +03:30

71 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"log"
"net/http"
"os"
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()
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)
if err != nil {
log.Fatal(err)
}
dbStore, err := db.NewStore(dbPath)
if err != nil {
log.Fatal(err)
}
translator, err := i18n.NewTranslator("en")
if err != nil {
log.Fatal("i18n: ", err)
}
handler := bot.NewHandler(botAPI, dbStore, translator)
webhookURL := os.Getenv("BOT_WEBHOOK_URL")
if webhookURL != "" {
startWebhook(botAPI, handler, webhookURL)
} else {
// Fallback to longpolling — remove any stale webhook first
if _, err := botAPI.Request(tgbotapi.DeleteWebhookConfig{}); err != nil {
log.Printf("DeleteWebhook: %v", err)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 30
updates := botAPI.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil {
handler.HandleMessage(update)
}
if update.CallbackQuery != nil {
handler.HandleCallback(update)
}
}
}
}
func getEnvDefault(key, defaultValue string) string {
if val := os.Getenv(key); val != "" {
return val
}
return defaultValue
}