Добавлено: обработка отмены

This commit is contained in:
far-galaxy 2023-03-13 10:51:48 +04:00
parent 7f268865ea
commit 7601630b99
3 changed files with 47 additions and 10 deletions

View File

@ -57,8 +57,10 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
if strings.Contains(tg_user.PosTag, "confirm") { if query.Data == "cancel" {
bot.Confirm(query, tg_user, tg_user.PosTag == "confirm_group") bot.Cancel(query)
} else if strings.Contains(tg_user.PosTag, "confirm") {
bot.Confirm(query)
} }
} }
} }

View File

@ -53,7 +53,8 @@ func (bot *Bot) Start() error {
if err != nil { if err != nil {
return err return err
} }
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Привет! Введи свой номер группы или фамилию преподавателя") msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Привет! Введи свой <b>номер группы</b> или <b>фамилию преподавателя</b>")
msg.ParseMode = tgbotapi.ModeHTML
_, err = bot.TG.Send(msg) _, err = bot.TG.Send(msg)
return err return err
} }
@ -126,13 +127,15 @@ func (bot *Bot) Find(query string) error {
return err 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) groupId, err := strconv.ParseInt(query.Data, 0, 64)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var groups []database.ShedulesInUser var groups []database.ShedulesInUser
err = bot.DB.Find(&groups, &database.ShedulesInUser{ err = bot.DB.Find(&groups, &database.ShedulesInUser{
L9Id: bot.TG_user.L9Id,
SheduleId: groupId, SheduleId: groupId,
IsTeacher: !isGroup, IsTeacher: !isGroup,
}) })
@ -141,24 +144,51 @@ func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery, tg_user *database.TgUser,
} }
if len(groups) == 0 { if len(groups) == 0 {
shInUser := database.ShedulesInUser{ shInUser := database.ShedulesInUser{
L9Id: tg_user.L9Id, L9Id: bot.TG_user.L9Id,
IsTeacher: !isGroup, IsTeacher: !isGroup,
SheduleId: groupId, SheduleId: groupId,
} }
bot.DB.InsertOne(shInUser) bot.DB.InsertOne(shInUser)
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID) bot.DeleteMsg(query)
bot.TG.Request(delete)
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Подключено!") msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Подключено!")
bot.TG.Send(msg) bot.TG.Send(msg)
tg_user.PosTag = "ready" bot.TG_user.PosTag = "ready"
bot.DB.Update(tg_user) bot.DB.Update(bot.TG_user)
} else { } else {
callback := tgbotapi.NewCallback(query.ID, "Эта группа уже подключена!") var msg string
if isGroup {
msg = "Эта группа уже подключена!"
} else {
msg = "Этот преподаватель уже подключен!"
}
callback := tgbotapi.NewCallback(query.ID, msg)
bot.TG.Request(callback) 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() { func (bot *Bot) Etc() {
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Oй!") msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Oй!")
bot.TG.Send(msg) bot.TG.Send(msg)

View File

@ -41,3 +41,8 @@ func GenerateKeyboard(array []tgbotapi.InlineKeyboardButton, query string) tgbot
markup = append(markup, []tgbotapi.InlineKeyboardButton{no_one}) markup = append(markup, []tgbotapi.InlineKeyboardButton{no_one})
return tgbotapi.InlineKeyboardMarkup{InlineKeyboard: markup} 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)
}