group_helper/main.go

117 lines
3.6 KiB
Go
Raw Normal View History

2020-04-11 18:39:54 +00:00
package main
import (
"git.dragon-labs.de/alphyron/group_helper/channels"
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/cli/commands"
"git.dragon-labs.de/alphyron/group_helper/config"
"git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/repository"
"git.dragon-labs.de/alphyron/group_helper/telegram"
"git.dragon-labs.de/alphyron/group_helper/telegram/routines"
"log"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
2020-04-11 18:39:54 +00:00
func main() {
dbConfig := config.DatabaseConfig{}
err := dbConfig.LoadConfig()
if err != nil {
log.Println("Problem while loading database environment variables")
log.Fatal(err)
}
2020-04-11 21:01:14 +00:00
tgConfig := config.TelegramConfig{}
err = tgConfig.LoadConfig()
2020-04-11 21:01:14 +00:00
if err != nil {
log.Println("Problem while loading telegram environment variables")
log.Fatal(err)
}
bot, err := tgbotapi.NewBotAPI(tgConfig.TelegramKey)
if err != nil {
log.Println("Problem while authenticate at telegram")
log.Fatal(err)
}
bot.Debug = tgConfig.TelegramDebug
log.Printf("Authorized on account %s", bot.Self.UserName)
groupHelperRepo, err := repository.NewUserHelperRepository()
if err != nil {
log.Println("Problem while connection to the Database")
log.Fatal(err)
}
err = groupHelperRepo.InitRepository()
if err != nil {
log.Println("Problem while initial the Database")
log.Fatal(err)
}
service := logic.NewGroupHelperService(groupHelperRepo)
userData := &obj.VerifyData{
Users: make([]*obj.VerifyUser, 0),
}
2024-10-28 20:39:17 +00:00
cm := InitialCommandManager(bot, service, userData)
rm := InitialRoutineManager(bot, cm, service, userData)
verifyTicker := time.NewTicker(1 * time.Minute)
leaveTicker := time.NewTicker(5 * time.Minute)
quit := make(chan struct{})
go channels.CheckUnverifiedUser(verifyTicker, quit, service, bot, userData)
go channels.CheckForLeavingUser(leaveTicker, quit, service, bot)
rm.StartUpdates()
}
func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService, userData *obj.VerifyData) *telegram.RoutineManager {
rm := &telegram.RoutineManager{
Bot: bot,
GroupHelperService: service,
}
rm.RegisterRoutine(routines.NewCommandRoutine(commandManager))
rm.RegisterRoutine(routines.NewDatabaseRoutine(service))
rm.RegisterRoutine(routines.NewJoinCheckerRoutine(userData))
rm.RegisterRoutine(routines.NewVerifierRoutine(service, userData))
rm.RegisterRoutine(routines.NewJoinWritingCheckerRoutine(userData))
return rm
}
2024-10-28 20:39:17 +00:00
func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperService, verifyData *obj.VerifyData) *cli.CommandManager {
cm := &cli.CommandManager{
Commands: make([]cli.Command, 0),
Bot: bot,
}
cm.RegisterCommand(commands.NewInfoCommand())
2020-04-18 19:32:33 +00:00
cm.RegisterCommand(commands.NewHelpCommand(cm))
cm.RegisterCommand(commands.NewJoinMessage(service))
cm.RegisterCommand(commands.NewLeaveMessage(service))
cm.RegisterCommand(commands.NewKickMessage(service))
cm.RegisterCommand(commands.NewVerifiedMessage(service))
cm.RegisterCommand(commands.NewToggleOnlineCheckCommand(service))
cm.RegisterCommand(commands.NewToggleWritingForbidCommand(service))
cm.RegisterCommand(commands.NewChatInfoCommand())
cm.RegisterCommand(commands.NewAdminGroupCommand(service))
cm.RegisterCommand(commands.NewBroadcastCommand())
cm.RegisterCommand(commands.NewRules(service))
cm.RegisterCommand(commands.NewSetRules(service))
2024-10-28 20:39:17 +00:00
cm.RegisterCommand(commands.NewWhitelistCommand(verifyData))
cm.RegisterCommand(commands.NewListVerifyCommand(verifyData))
return cm
2020-04-11 18:39:54 +00:00
}