mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-03-02 00:14:34 +00:00
Added tests, revised logging Removed dependency on env file Try reformatting unit file Comments clarification Added readme Added readme
71 lines
1.6 KiB
Go
Executable File
71 lines
1.6 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
)
|
|
|
|
func main() {
|
|
// Initialize custom loggers
|
|
initLoggers()
|
|
|
|
// Log the start of the application
|
|
InfoLogger.Println("Starting Telegram Bot Application")
|
|
|
|
// Initialize database
|
|
db, err := initDB()
|
|
if err != nil {
|
|
ErrorLogger.Fatalf("Error initializing database: %v", err)
|
|
}
|
|
|
|
// Load all bot configurations
|
|
configs, err := loadAllConfigs("config")
|
|
if err != nil {
|
|
ErrorLogger.Fatalf("Error loading configurations: %v", err)
|
|
}
|
|
|
|
// Create a WaitGroup to manage goroutines
|
|
var wg sync.WaitGroup
|
|
|
|
// Set up context with cancellation
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer cancel()
|
|
|
|
// Initialize and start each bot
|
|
for _, config := range configs {
|
|
wg.Add(1)
|
|
go func(cfg BotConfig) {
|
|
defer wg.Done()
|
|
|
|
// Create Bot instance without TelegramClient initially
|
|
realClock := RealClock{}
|
|
bot, err := NewBot(db, cfg, realClock, nil)
|
|
if err != nil {
|
|
ErrorLogger.Printf("Error creating bot %s: %v", cfg.ID, err)
|
|
return
|
|
}
|
|
|
|
// Initialize TelegramClient with the bot's handleUpdate method
|
|
tgClient, err := initTelegramBot(cfg.TelegramToken, bot.handleUpdate)
|
|
if err != nil {
|
|
ErrorLogger.Printf("Error initializing Telegram client for bot %s: %v", cfg.ID, err)
|
|
return
|
|
}
|
|
|
|
// Assign the TelegramClient to the bot
|
|
bot.tgBot = tgClient
|
|
|
|
// Start the bot
|
|
InfoLogger.Printf("Starting bot %s...", cfg.ID)
|
|
bot.Start(ctx)
|
|
}(config)
|
|
}
|
|
|
|
// Wait for all bots to finish
|
|
wg.Wait()
|
|
|
|
InfoLogger.Println("All bots have stopped. Exiting application.")
|
|
}
|