From 755d704d5ae54b895231e406875c51673bf189fa Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 9 May 2020 19:36:15 +0200 Subject: [PATCH] :sparkles: Create Constructor and implement the command routine --- main.go | 22 ++++++++++++---------- telegram/routines/command.go | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 telegram/routines/command.go diff --git a/main.go b/main.go index 6083160..19832ec 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "git.dragon-labs.de/alphyron/group_helper/config" "git.dragon-labs.de/alphyron/group_helper/logic" "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" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" @@ -54,19 +56,19 @@ func main() { service := logic.NewGroupHelperService(groupHelperRepo) cm := InitialCommandManager(bot, service) + rm := InitialRoutineManager(bot, cm) - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 - updates, _ := bot.GetUpdatesChan(u) + rm.StartUpdates() +} - for update := range updates { - _, err := cm.ExecuteUpdate(&update) - - if err != nil { - log.Printf("ERROR - Command error") - log.Println(err) - } +func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager) *telegram.RoutineManager { + rm := &telegram.RoutineManager{ + Bot: bot, } + + rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) + + return rm } func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperService) *cli.CommandManager { diff --git a/telegram/routines/command.go b/telegram/routines/command.go new file mode 100644 index 0000000..0d517c0 --- /dev/null +++ b/telegram/routines/command.go @@ -0,0 +1,21 @@ +package routines + +import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/telegram" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +type commandRoutine struct { + CommandManager *cli.CommandManager +} + +func NewCommandRoutine(commandManager *cli.CommandManager) telegram.Routine { + return &commandRoutine{CommandManager: commandManager} +} + +func (cr commandRoutine) Update(update *tgbotapi.Update) error { + _, err := cr.CommandManager.ExecuteUpdate(update) + + return err +}