Initial scaffold: Go module, SQLite schema, config loader, Telegram bot with webhook/polling modes

This commit is contained in:
2026-06-23 20:22:46 +03:30
parent c803239193
commit 4146e35f35
9 changed files with 600 additions and 0 deletions

73
cmd/bot/main.go Normal file
View File

@@ -0,0 +1,73 @@
package main
import (
"log"
"net/http"
"net/url"
"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")
proxyURL := os.Getenv("HTTP_PROXY")
dbPath := getEnvDefault("DB_PATH", "db.sqlite3")
// Build HTTP client with optional proxy
var httpClient *http.Client
if proxyURL != "" {
parsed, err := url.Parse(proxyURL)
if err != nil {
log.Fatal(err)
}
tr := &http.Transport{Proxy: http.ProxyURL(parsed)}
httpClient = &http.Client{Transport: tr}
} else {
httpClient = http.DefaultClient
}
botAPI, err := tgbotapi.NewBotAPIWithClient(token, "", httpClient)
if err != nil {
log.Fatal(err)
}
dbStore, err := db.NewStore(dbPath)
if err != nil {
log.Fatal(err)
}
translator, _ := i18n.NewTranslator("en")
handler := bot.NewHandler(botAPI, dbStore, translator)
webhookURL := os.Getenv("BOT_WEBHOOK_URL")
if webhookURL != "" {
startWebhook(botAPI, handler, webhookURL)
} else {
// Fallback to longpolling
u := tgbotapi.NewUpdate(0)
u.Timeout = 30
updates := botAPI.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil {
handler.HandleMessage(update)
}
}
}
}
func getEnvDefault(key, defaultValue string) string {
if val := os.Getenv(key); val != "" {
return val
}
return defaultValue
}

61
cmd/bot/webhook.go Normal file
View File

@@ -0,0 +1,61 @@
package main
import (
"log"
"net/http"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"worktimeBot/internal/bot"
)
// startWebhook configures the Telegram webhook and starts the HTTP(S) listener.
func startWebhook(botAPI *tgbotapi.BotAPI, handler *bot.Handler, webhookURL string) {
// Delete any previously set webhook
if _, err := botAPI.Request(tgbotapi.DeleteWebhookConfig{}); err != nil {
log.Fatal("DeleteWebhook:", err)
}
// Set the new webhook
wh, err := tgbotapi.NewWebhook(webhookURL)
if err != nil {
log.Fatal("NewWebhook:", err)
}
if _, err := botAPI.Request(wh); err != nil {
log.Fatal("SetWebhook:", err)
}
// Register the webhook endpoint and get the updates channel
updates := botAPI.ListenForWebhook("/webhook")
// Determine where to listen
listenAddr := getEnvDefault("BOT_LISTEN", ":8080")
tlsCert := os.Getenv("BOT_TLS_CERT")
tlsKey := os.Getenv("BOT_TLS_KEY")
if tlsCert != "" && tlsKey != "" {
log.Printf("Starting HTTPS webhook on %s", listenAddr)
go func() {
if err := http.ListenAndServeTLS(listenAddr, tlsCert, tlsKey, nil); err != nil {
log.Fatal("HTTPS server:", err)
}
}()
} else {
log.Printf("Starting HTTP webhook on %s (TLS handled by reverse proxy)", listenAddr)
go func() {
if err := http.ListenAndServe(listenAddr, nil); err != nil {
log.Fatal("HTTP server:", err)
}
}()
}
log.Printf("Webhook registered at %s", webhookURL)
// Process incoming updates
for update := range updates {
if update.Message != nil {
handler.HandleMessage(update)
}
}
}