Implement routine manager and interface

This commit is contained in:
Alphyron 2020-05-09 19:07:02 +02:00
parent f48595c0f4
commit a69b46f714
2 changed files with 44 additions and 0 deletions

37
telegram/manager.go Normal file
View File

@ -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)
}

7
telegram/routine.go Normal file
View File

@ -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
}