package channels import ( "git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/message" "git.dragon-labs.de/alphyron/group_helper/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "log" "time" ) func CheckForLeavingUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) { for { select { case <-ticker.C: groups, err := groupHelperService.ListGroups() if err != nil { log.Println("Error while getting all groups in the LeavingChecking routine") log.Println(err) continue } for _, group := range groups { time.Sleep(500 * time.Millisecond) config := tgbotapi.ChatConfig{ ChatID: group.GroupID, } teleSize, _ := bot.GetChatMembersCount(config) if teleSize == group.Size { continue } if group.Size < teleSize { group.Size = teleSize _, err = groupHelperService.UpdateGroup(group) log.Println(err) continue } if teleSize < group.Size { checkGroupUser(group, groupHelperService, bot) groupHelperService.UpdateGroup(group) } } case <-quitChannel: ticker.Stop() return } } } func checkGroupUser(group *models.Group, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI) { rate := time.Second / 5 throttle := time.Tick(rate) users, err := groupHelperService.ListGroupUsers(group) log.Println("users") if err != nil { log.Println("Error while getting the users for group while checking for leaved user") log.Println(err) return } log.Println("no err") log.Println(len(users)) for _, user := range users { <-throttle // rate limit our Service.Method RPCs userConfig := tgbotapi.ChatConfigWithUser{ ChatID: group.GroupID, UserID: int(user.UserID), } log.Println(user.UserID) member, _ := bot.GetChatMember(userConfig) log.Println(member.User.FirstName) log.Println(member.HasLeft()) if !member.HasLeft() { continue } log.Println("User left") leaveMessage := group.UserLeaveMessage leaveMessage = message.ReplaceMessage(leaveMessage, member.User) msg := tgbotapi.NewMessage(group.GroupID, leaveMessage) msg.ParseMode = "Markdown" bot.Send(msg) _, err = groupHelperService.UserLeaveGroup(user, group) if err != nil { log.Println(err) } group.Size = group.Size - 1 groupHelperService.UpdateGroup(group) if err != nil { log.Println(err) } } }