From a69b46f714fb868c8ac64c630be0b5d80b01721e Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 9 May 2020 19:07:02 +0200 Subject: [PATCH] :sparkles: Implement routine manager and interface --- telegram/manager.go | 37 +++++++++++++++++++++++++++++++++++++ telegram/routine.go | 7 +++++++ 2 files changed, 44 insertions(+) create mode 100644 telegram/manager.go create mode 100644 telegram/routine.go diff --git a/telegram/manager.go b/telegram/manager.go new file mode 100644 index 0000000..0f6839d --- /dev/null +++ b/telegram/manager.go @@ -0,0 +1,37 @@ +package telegram + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "log" +) + +type RoutineManager struct { + Routine []Routine + Bot *tgbotapi.BotAPI +} + +func (rm RoutineManager) StartUpdates() { + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + updates, _ := rm.Bot.GetUpdatesChan(u) + + for update := range updates { + + for _, routine := range rm.Routine { + err := routine.Update(&update) + + if err != nil { + log.Printf("ERROR - Routine error") + log.Println(err) + } + } + } +} + +func (rm *RoutineManager) RegisterRoutine(routine Routine) { + if rm.Routine == nil { + rm.Routine = make([]Routine, 0) + } + + rm.Routine = append(rm.Routine, routine) +} diff --git a/telegram/routine.go b/telegram/routine.go new file mode 100644 index 0000000..8dcec97 --- /dev/null +++ b/telegram/routine.go @@ -0,0 +1,7 @@ +package telegram + +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + +type Routine interface { + Update(update *tgbotapi.Update) error +}