diff --git a/main.go b/main.go index 22ced70..3de006e 100644 --- a/main.go +++ b/main.go @@ -57,8 +57,10 @@ func main() { log.Fatal(err) } - if strings.Contains(tg_user.PosTag, "confirm") { - bot.Confirm(query, tg_user, tg_user.PosTag == "confirm_group") + if query.Data == "cancel" { + bot.Cancel(query) + } else if strings.Contains(tg_user.PosTag, "confirm") { + bot.Confirm(query) } } } diff --git a/modules/tg/handlers.go b/modules/tg/handlers.go index d04f9eb..3913377 100644 --- a/modules/tg/handlers.go +++ b/modules/tg/handlers.go @@ -53,7 +53,8 @@ func (bot *Bot) Start() error { if err != nil { return err } - msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Привет! Введи свой номер группы или фамилию преподавателя") + msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Привет! Введи свой номер группы или фамилию преподавателя") + msg.ParseMode = tgbotapi.ModeHTML _, err = bot.TG.Send(msg) return err } @@ -126,13 +127,15 @@ func (bot *Bot) Find(query string) error { return err } -func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery, tg_user *database.TgUser, isGroup bool) { +func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) { + isGroup := bot.TG_user.PosTag == "confirm_group" groupId, err := strconv.ParseInt(query.Data, 0, 64) if err != nil { log.Fatal(err) } var groups []database.ShedulesInUser err = bot.DB.Find(&groups, &database.ShedulesInUser{ + L9Id: bot.TG_user.L9Id, SheduleId: groupId, IsTeacher: !isGroup, }) @@ -141,24 +144,51 @@ func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery, tg_user *database.TgUser, } if len(groups) == 0 { shInUser := database.ShedulesInUser{ - L9Id: tg_user.L9Id, + L9Id: bot.TG_user.L9Id, IsTeacher: !isGroup, SheduleId: groupId, } bot.DB.InsertOne(shInUser) - delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID) - bot.TG.Request(delete) + bot.DeleteMsg(query) msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Подключено!") bot.TG.Send(msg) - tg_user.PosTag = "ready" - bot.DB.Update(tg_user) + bot.TG_user.PosTag = "ready" + bot.DB.Update(bot.TG_user) } else { - callback := tgbotapi.NewCallback(query.ID, "Эта группа уже подключена!") + var msg string + if isGroup { + msg = "Эта группа уже подключена!" + } else { + msg = "Этот преподаватель уже подключен!" + } + callback := tgbotapi.NewCallback(query.ID, msg) bot.TG.Request(callback) } } +func (bot *Bot) Cancel(query *tgbotapi.CallbackQuery) { + shedules, err := bot.DB.Count(&database.ShedulesInUser{ + L9Id: bot.TG_user.L9Id, + }) + if err != nil { + log.Fatal(err) + } + if shedules == 0 { + bot.TG_user.PosTag = "add" + bot.DB.Update(bot.TG_user) + bot.DeleteMsg(query) + msg := tgbotapi.NewMessage( + bot.TG_user.TgId, + "Ой, для работы с ботом нужно подключить хотя бы одно расписание группы или преподавателя!\nВведи свой номер группы или фамилию преподавателя", + ) + bot.TG.Send(msg) + } else { + bot.TG_user.PosTag = "ready" + bot.DeleteMsg(query) + } +} + func (bot *Bot) Etc() { msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Oй!") bot.TG.Send(msg) diff --git a/modules/tg/utils.go b/modules/tg/utils.go index 89d3dd5..b5fc910 100644 --- a/modules/tg/utils.go +++ b/modules/tg/utils.go @@ -41,3 +41,8 @@ func GenerateKeyboard(array []tgbotapi.InlineKeyboardButton, query string) tgbot markup = append(markup, []tgbotapi.InlineKeyboardButton{no_one}) return tgbotapi.InlineKeyboardMarkup{InlineKeyboard: markup} } + +func (bot *Bot) DeleteMsg(query *tgbotapi.CallbackQuery) { + delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID) + bot.TG.Request(delete) +}