Добавлено: крутой поиск групп и преподавателей
This commit is contained in:
parent
7a16cc719a
commit
e9470f1bd2
2
main.go
2
main.go
|
@ -35,6 +35,8 @@ func main() {
|
||||||
|
|
||||||
if tg_user.PosTag == "not_started" {
|
if tg_user.PosTag == "not_started" {
|
||||||
bot.Start()
|
bot.Start()
|
||||||
|
} else if tg_user.PosTag == "add" {
|
||||||
|
bot.Find(update.Message.Text)
|
||||||
} else {
|
} else {
|
||||||
bot.Etc()
|
bot.Etc()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,12 @@ package tg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
"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"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (bot *Bot) InitUser(msg *tgbotapi.Message) (*database.TgUser, error) {
|
func (bot *Bot) InitUser(msg *tgbotapi.Message) (*database.TgUser, error) {
|
||||||
|
@ -53,6 +56,66 @@ func (bot *Bot) Start() {
|
||||||
bot.TG.Send(msg)
|
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() {
|
func (bot *Bot) Etc() {
|
||||||
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Oops!")
|
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Oops!")
|
||||||
bot.TG.Send(msg)
|
bot.TG.Send(msg)
|
||||||
|
|
|
@ -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}
|
||||||
|
}
|
Reference in New Issue