2020-05-16 20:06:58 +00:00
|
|
|
package routines
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.dragon-labs.de/alphyron/group_helper/logic"
|
|
|
|
"git.dragon-labs.de/alphyron/group_helper/models"
|
2020-06-13 18:26:15 +00:00
|
|
|
"git.dragon-labs.de/alphyron/group_helper/obj"
|
2020-05-16 20:06:58 +00:00
|
|
|
"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
|
2020-06-13 18:26:15 +00:00
|
|
|
verifyData *obj.VerifyData
|
2020-05-16 20:06:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 18:26:15 +00:00
|
|
|
func NewVerifierRoutine(groupHelperService logic.GroupHelperService, verifyData *obj.VerifyData) telegram.Routine {
|
2020-05-16 20:06:58 +00:00
|
|
|
return verifierRoutine{
|
|
|
|
groupHelperService: groupHelperService,
|
2020-06-13 18:26:15 +00:00
|
|
|
verifyData: verifyData,
|
2020-05-16 20:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
2020-06-13 18:26:15 +00:00
|
|
|
if !v.verifyData.ExistCountdownForUserInGroup(update.CallbackQuery.From.ID, group.GroupID) {
|
2020-05-16 20:06:58 +00:00
|
|
|
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "You are already verified!"))
|
|
|
|
return err
|
2020-06-13 18:26:15 +00:00
|
|
|
}
|
2020-05-16 20:06:58 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
//TODO Replace placeholder
|
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, verifyMessage)
|
|
|
|
msg.ParseMode = "Markdown"
|
|
|
|
_, err := botAPI.Send(msg)
|
|
|
|
|
2020-06-13 18:26:15 +00:00
|
|
|
v.verifyData.RemoveUser(update.CallbackQuery.From.ID, group.GroupID)
|
2020-05-16 20:06:58 +00:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
_, err = botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, "Wrong answer"))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|