Implement a scheduled checker to ban the persons if they not verified

This commit is contained in:
Alphyron 2020-06-08 22:12:36 +02:00
parent c702115fe6
commit 52bb59bc21
5 changed files with 165 additions and 15 deletions

View File

@ -3,7 +3,7 @@ FROM golang:1.13-stretch AS build-env
COPY . /src COPY . /src
WORKDIR /src WORKDIR /src
ENV GOPROXY=http://192.168.0.143:3000 #ENV GOPROXY=http://192.168.0.143:3000
ENV GO113MODULE=on ENV GO113MODULE=on
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go test ./... -cover -coverprofile=c.out #gosetup RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go test ./... -cover -coverprofile=c.out #gosetup

134
channels/verifiychecker.go Normal file
View File

@ -0,0 +1,134 @@
package channels
import (
"git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"strconv"
"sync"
"time"
)
func CheckUnverifiedUser(ticker *time.Ticker, quitChannel <-chan struct{}, groupHelperService logic.GroupHelperService, bot *tgbotapi.BotAPI, data *obj.VerifyData) {
var wg sync.WaitGroup
wg.Add(len(data.Users))
for {
select {
case <-ticker.C:
delete := make([]*obj.VerifyUser, 0)
groups := make(map[int64]*models.Group, 0)
users := make(map[int]*models.User, 0)
for _, user := range data.Users {
defer wg.Done()
user.LeftTime -= 1
if user.LeftTime <= 0 {
log.Println(len(data.Users))
if _, ok := groups[user.GroupID]; !ok {
group, err := groupHelperService.GetGroupByID(user.GroupID)
if err != nil {
log.Println("Error while getting the Group in the verifychecker for id " + strconv.FormatInt(user.GroupID, 10))
log.Println(err)
return
}
groups[user.GroupID] = group
}
if _, ok := users[user.UserID]; !ok {
user_db, err := groupHelperService.GetUserByID(int64(user.UserID))
if err != nil {
log.Println("Error while getting the User in the verifychecker for id " + strconv.Itoa(user.UserID))
log.Println(err)
return
}
users[user.UserID] = user_db
}
userconfig := tgbotapi.ChatConfigWithUser{
ChatID: user.GroupID,
UserID: user.UserID,
}
member, _ := bot.GetChatMember(userconfig)
delete = append(delete, user)
_, err := groupHelperService.UserLeaveGroup(users[user.UserID], groups[user.GroupID])
if err != nil {
log.Printf("Error while removing the user from group in db in the verifier (user/group) (%d/%d)", user.UserID, user.GroupID)
log.Println(err)
}
if member.HasLeft() {
bot.DeleteMessage(tgbotapi.DeleteMessageConfig{
ChatID: user.GroupID,
MessageID: user.MessageID,
})
return
}
kickMessage := groups[user.GroupID].UserKickMessage
//TODO Replace placeholder
msg := tgbotapi.NewMessage(user.GroupID, kickMessage)
msg.ParseMode = "Markdown"
_, err = bot.Send(msg)
if err != nil {
log.Println("problem while send a message in the verifier:")
log.Printf("Problem Error Message: %v", err)
}
kickConfig := tgbotapi.KickChatMemberConfig{
ChatMemberConfig: tgbotapi.ChatMemberConfig{
UserID: user.UserID,
ChatID: user.GroupID,
},
}
res, _ := bot.KickChatMember(kickConfig)
if !res.Ok {
msg := tgbotapi.NewMessage(user.GroupID, "I can't kick @"+member.User.UserName+"\nReason:\n"+res.Description)
msg.ParseMode = "Markdown"
_, err = bot.Send(msg)
if err != nil {
log.Println("problem while send a message in the verifier:")
log.Printf("Problem Error Message: %v", err)
}
}
_, err = bot.DeleteMessage(tgbotapi.DeleteMessageConfig{
ChatID: user.GroupID,
MessageID: user.MessageID,
})
if err != nil {
log.Println("problem while deleting a message in the verifier:")
log.Println(err)
}
}
}
for _, user := range delete {
data.RemoveUser(user.UserID, user.GroupID)
}
case <-quitChannel:
ticker.Stop()
return
}
}
wg.Wait()
}

18
main.go
View File

@ -1,14 +1,17 @@
package main package main
import ( import (
"git.dragon-labs.de/alphyron/group_helper/channels"
"git.dragon-labs.de/alphyron/group_helper/cli" "git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/cli/commands" "git.dragon-labs.de/alphyron/group_helper/cli/commands"
"git.dragon-labs.de/alphyron/group_helper/config" "git.dragon-labs.de/alphyron/group_helper/config"
"git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/logic"
"git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/repository" "git.dragon-labs.de/alphyron/group_helper/repository"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
"git.dragon-labs.de/alphyron/group_helper/telegram/routines" "git.dragon-labs.de/alphyron/group_helper/telegram/routines"
"log" "log"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
) )
@ -55,13 +58,22 @@ func main() {
service := logic.NewGroupHelperService(groupHelperRepo) service := logic.NewGroupHelperService(groupHelperRepo)
userData := &obj.VerifyData{
Users: make([]*obj.VerifyUser, 0),
}
cm := InitialCommandManager(bot, service) cm := InitialCommandManager(bot, service)
rm := InitialRoutineManager(bot, cm, service) rm := InitialRoutineManager(bot, cm, service, userData)
ticker := time.NewTicker(1 * time.Minute)
quit := make(chan struct{})
go channels.CheckUnverifiedUser(ticker, quit, service, bot, userData)
rm.StartUpdates() rm.StartUpdates()
} }
func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService) *telegram.RoutineManager { func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandManager, service logic.GroupHelperService, userData *obj.VerifyData) *telegram.RoutineManager {
rm := &telegram.RoutineManager{ rm := &telegram.RoutineManager{
Bot: bot, Bot: bot,
GroupHelperService: service, GroupHelperService: service,
@ -69,7 +81,7 @@ func InitialRoutineManager(bot *tgbotapi.BotAPI, commandManager *cli.CommandMana
rm.RegisterRoutine(routines.NewCommandRoutine(commandManager)) rm.RegisterRoutine(routines.NewCommandRoutine(commandManager))
rm.RegisterRoutine(routines.NewDatabaseRoutine(service)) rm.RegisterRoutine(routines.NewDatabaseRoutine(service))
rm.RegisterRoutine(routines.NewJoinCheckerRoutine()) rm.RegisterRoutine(routines.NewJoinCheckerRoutine(userData))
rm.RegisterRoutine(routines.NewVerifierRoutine(service)) rm.RegisterRoutine(routines.NewVerifierRoutine(service))
return rm return rm

View File

@ -2,6 +2,7 @@ package routines
import ( import (
"git.dragon-labs.de/alphyron/group_helper/models" "git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/telegram" "git.dragon-labs.de/alphyron/group_helper/telegram"
"git.dragon-labs.de/alphyron/group_helper/util" "git.dragon-labs.de/alphyron/group_helper/util"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
@ -12,10 +13,13 @@ import (
) )
type joinCheckerRoutine struct { type joinCheckerRoutine struct {
verifyData *obj.VerifyData
} }
func NewJoinCheckerRoutine() telegram.Routine { func NewJoinCheckerRoutine(verifyData *obj.VerifyData) telegram.Routine {
return joinCheckerRoutine{} return joinCheckerRoutine{
verifyData: verifyData,
}
} }
func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error { func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error {
@ -30,12 +34,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd
if update.Message.NewChatMembers != nil { if update.Message.NewChatMembers != nil {
for _, newUser := range *update.Message.NewChatMembers { for _, newUser := range *update.Message.NewChatMembers {
type check struct { length := 50
Task string
Solution bool
}
length := 3
tasks := make([][]tgbotapi.InlineKeyboardButton, length) tasks := make([][]tgbotapi.InlineKeyboardButton, length)
@ -53,7 +52,7 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
msg.ReplyMarkup = markup msg.ReplyMarkup = markup
_, err := botAPI.Send(msg) message, err := botAPI.Send(msg)
if err != nil { if err != nil {
log.Println("Problem to send verified Messages to user") log.Println("Problem to send verified Messages to user")
@ -61,7 +60,12 @@ func (j joinCheckerRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Upd
log.Println(err) log.Println(err)
} }
//TODO Add to countdown j.verifyData.AddUser(&obj.VerifyUser{
UserID: newUser.ID,
GroupID: group.GroupID,
LeftTime: group.KickCooldown,
MessageID: message.MessageID,
})
} }
} }
return nil return nil

View File

@ -36,7 +36,7 @@ func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update
if success { if success {
//TODO check if the user is realy in countdown handler //TODO check if the user is really in countdown handler
/*if isInGroup { /*if isInGroup {
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!")) _, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!"))
return err return err