diff --git a/main.go b/main.go index db193a1..3ccc5eb 100644 --- a/main.go +++ b/main.go @@ -69,6 +69,7 @@ func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandMana rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) rm.RegisterRoutine(routines.NewDatabaseRoutine(service)) + rm.RegisterRoutine(routines.NewJoinCheckerRoutine()) return rm } diff --git a/telegram/manager.go b/telegram/manager.go index b738bfd..b88f57e 100644 --- a/telegram/manager.go +++ b/telegram/manager.go @@ -2,6 +2,7 @@ package telegram import ( "git.dragon-labs.de/alphyron/group_helper/logic" + "git.dragon-labs.de/alphyron/group_helper/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "log" ) @@ -19,11 +20,22 @@ func (rm RoutineManager) StartUpdates() { for update := range updates { - group, err := rm.GroupHelperService.GetGroupByID(update.Message.Chat.ID) + var group *models.Group + var err error + if update.Message != nil { + group, err = rm.GroupHelperService.GetGroupByID(update.Message.Chat.ID) - if err != nil { - log.Println(err) - continue + if err != nil { + log.Println(err) + continue + } + } else if update.CallbackQuery != nil { + group, err = rm.GroupHelperService.GetGroupByID(update.CallbackQuery.Message.Chat.ID) + + if err != nil { + log.Println(err) + continue + } } for _, routine := range rm.Routine { diff --git a/telegram/routines/joinchecker.go b/telegram/routines/joinchecker.go new file mode 100644 index 0000000..de713e4 --- /dev/null +++ b/telegram/routines/joinchecker.go @@ -0,0 +1,68 @@ +package routines + +import ( + "git.dragon-labs.de/alphyron/group_helper/models" + "git.dragon-labs.de/alphyron/group_helper/telegram" + "git.dragon-labs.de/alphyron/group_helper/util" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "log" + "math/rand" + "strconv" + "time" +) + +type joinCheckerRoutine struct { +} + +func NewJoinCheckerRoutine() telegram.Routine { + return joinCheckerRoutine{} +} + +func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error { + + if update.Message == nil { // ignore any non-Message Updates + return nil + } + + if update.Message.Chat.IsPrivate() || update.Message.Chat.IsChannel() { + return nil + } + + if update.Message.NewChatMembers != nil { + for _, newUser := range *update.Message.NewChatMembers { + type check struct { + Task string + Solution bool + } + + length := 3 + + tasks := make([][]tgbotapi.InlineKeyboardButton, length) + + for i := 0; i < length; i++ { + tasks[i] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(util.GenerateCalculation(i == 0), "UserValidation: "+strconv.FormatBool(i == 0))) + } + + rand.Seed(time.Now().UnixNano()) + rand.Shuffle(len(tasks), func(i, j int) { tasks[i], tasks[j] = tasks[j], tasks[i] }) + + markup := tgbotapi.NewInlineKeyboardMarkup(tasks...) + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) + //TODO Replace placeholder + msg.ParseMode = "Markdown" + msg.ReplyMarkup = markup + + _, err := botAPI.Send(msg) + + if err != nil { + log.Println("Problem to send verified Messages to user") + log.Println(newUser.FirstName) + log.Println(err) + } + + //TODO Add to countdown + } + } + return nil +} diff --git a/util/calculator.go b/util/calculator.go new file mode 100644 index 0000000..9d3d709 --- /dev/null +++ b/util/calculator.go @@ -0,0 +1,19 @@ +package util + +import ( + "fmt" + "math/rand" +) + +func GenerateCalculation(correct bool) string { + firstNum := rand.Intn(100) + secondNum := rand.Intn(100) + + solution := firstNum + secondNum + + for !correct && solution == firstNum+secondNum { + solution = rand.Intn(150) + } + + return fmt.Sprintf("%d + %d = %d", firstNum, secondNum, solution) +}