From e9470f1bd21dab52c4483319edc998bdf529483a Mon Sep 17 00:00:00 2001 From: far-galaxy Date: Wed, 8 Mar 2023 10:43:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE:=20=D0=BA=D1=80=D1=83=D1=82=D0=BE=D0=B9=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B8=D1=81=D0=BA=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=B5=D0=BF=D0=BE=D0=B4=D0=B0=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 ++ modules/tg/handlers.go | 63 ++++++++++++++++++++++++++++++++++++++++++ modules/tg/utils.go | 43 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 modules/tg/utils.go diff --git a/main.go b/main.go index ebbee97..3da4c1a 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,8 @@ func main() { if tg_user.PosTag == "not_started" { bot.Start() + } else if tg_user.PosTag == "add" { + bot.Find(update.Message.Text) } else { bot.Etc() } diff --git a/modules/tg/handlers.go b/modules/tg/handlers.go index 59bfb5e..fd8d585 100644 --- a/modules/tg/handlers.go +++ b/modules/tg/handlers.go @@ -2,9 +2,12 @@ package tg import ( "log" + "strings" "git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database" + "git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/ssau_parser" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "xorm.io/builder" ) func (bot *Bot) InitUser(msg *tgbotapi.Message) (*database.TgUser, error) { @@ -53,6 +56,66 @@ func (bot *Bot) Start() { bot.TG.Send(msg) } +func (bot *Bot) Find(query string) { + var groups []database.Group + bot.DB.Where(builder.Like{"GroupName", query}).Find(&groups) + + var teachers []database.Teacher + bot.DB.Where(builder.Like{"LastName", query}).Find(&teachers) + + list, _ := ssau_parser.FindInRasp(query) + + allGroups := groups + allTeachers := teachers + + for _, elem := range list { + if strings.Contains(elem.Url, "group") { + exists := false + for _, group := range groups { + if elem.Id == group.GroupId { + exists = true + break + } + } + if !exists { + allGroups = append(allGroups, database.Group{GroupId: elem.Id, GroupName: elem.Text}) + } + } + if strings.Contains(elem.Url, "staff") { + exists := false + for _, teacher := range teachers { + if elem.Id == teacher.TeacherId { + exists = true + break + } + } + if !exists { + name := strings.Split(elem.Text, " ") + allTeachers = append(allTeachers, database.Teacher{ + TeacherId: elem.Id, + LastName: name[0], + FirstName: name[1], + MidName: name[2], + }) + } + } + } + + if len(allGroups) != 0 { + msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Many groups in base. Please sekect one") + msg.ReplyMarkup = GenerateKeyboard(GenerateGroupsArray(allGroups), query) + bot.TG.Send(msg) + } else if len(allTeachers) != 0 { + msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Many teachers in base. Please sekect one") + msg.ReplyMarkup = GenerateKeyboard(GenerateTeachersArray(allTeachers), query) + bot.TG.Send(msg) + } else { + msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Nothing found ):") + bot.TG.Send(msg) + } + +} + func (bot *Bot) Etc() { msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Oops!") bot.TG.Send(msg) diff --git a/modules/tg/utils.go b/modules/tg/utils.go new file mode 100644 index 0000000..eb91834 --- /dev/null +++ b/modules/tg/utils.go @@ -0,0 +1,43 @@ +package tg + +import ( + "fmt" + "strconv" + + "git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +func GenerateGroupsArray(groups []database.Group) []tgbotapi.InlineKeyboardButton { + var grKeys []tgbotapi.InlineKeyboardButton + for _, gr := range groups { + grKeys = append(grKeys, tgbotapi.NewInlineKeyboardButtonData(gr.GroupName, strconv.FormatInt(gr.GroupId, 10))) + } + return grKeys +} + +func GenerateTeachersArray(groups []database.Teacher) []tgbotapi.InlineKeyboardButton { + var teacherKeys []tgbotapi.InlineKeyboardButton + for _, t := range groups { + name := fmt.Sprintf("%s %s.%s.", t.LastName, t.FirstName[0:2], t.MidName[0:2]) + teacherKeys = append(teacherKeys, tgbotapi.NewInlineKeyboardButtonData(name, strconv.FormatInt(t.TeacherId, 10))) + } + return teacherKeys +} + +func GenerateKeyboard(array []tgbotapi.InlineKeyboardButton, query string) tgbotapi.InlineKeyboardMarkup { + var keys []tgbotapi.InlineKeyboardButton + var markup [][]tgbotapi.InlineKeyboardButton + + for _, key := range array { + keys = append(keys, key) + if len(keys) >= 3 { + markup = append(markup, keys) + keys = []tgbotapi.InlineKeyboardButton{} + } + } + markup = append(markup, keys) + no_one := tgbotapi.NewInlineKeyboardButtonData("No one", "no_one_"+query) + markup = append(markup, []tgbotapi.InlineKeyboardButton{no_one}) + return tgbotapi.InlineKeyboardMarkup{InlineKeyboard: markup} +}