group_helper/telegram/routines/verifier.go
2020-06-28 20:35:36 +02:00

67 lines
2.1 KiB
Go

package routines
import (
"git.dragon-labs.de/alphyron/group_helper/logic"
message2 "git.dragon-labs.de/alphyron/group_helper/message"
"git.dragon-labs.de/alphyron/group_helper/models"
"git.dragon-labs.de/alphyron/group_helper/obj"
"git.dragon-labs.de/alphyron/group_helper/telegram"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
type verifierRoutine struct {
groupHelperService logic.GroupHelperService
verifyData *obj.VerifyData
}
func NewVerifierRoutine(groupHelperService logic.GroupHelperService, verifyData *obj.VerifyData) telegram.Routine {
return verifierRoutine{
groupHelperService: groupHelperService,
verifyData: verifyData,
}
}
func (v verifierRoutine) Update(botAPI *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) error {
if update.CallbackQuery == nil {
return nil
}
if !strings.HasPrefix(update.CallbackQuery.Data, "UserValidation:") {
return nil
}
success, err := strconv.ParseBool(strings.TrimPrefix(update.CallbackQuery.Data, "UserValidation: "))
if err != nil {
return err
}
if success {
if !v.verifyData.ExistCountdownForUserInGroup(update.CallbackQuery.From.ID, group.GroupID) {
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!"))
return err
}
// Delete after accept the Message
botAPI.DeleteMessage(tgbotapi.DeleteMessageConfig{
ChatID: update.CallbackQuery.Message.Chat.ID,
MessageID: update.CallbackQuery.Message.MessageID,
})
botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are now verified. Welcome to the chat!"))
verifyMessage := group.UserVerifiedMessage
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, message2.ReplaceMessage(verifyMessage, update.CallbackQuery.From))
msg.ParseMode = "Markdown"
_, err := botAPI.Send(msg)
v.verifyData.RemoveUser(update.CallbackQuery.From.ID, group.GroupID)
return err
} else {
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "Wrong answer"))
return err
}
}