2023-08-03 13:13:09 +04:00
|
|
|
package tg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
2023-08-03 21:52:18 +04:00
|
|
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/ssau_parser"
|
2023-08-03 13:13:09 +04:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Создание ряда кнопок из списка групп
|
2023-08-03 13:39:59 +04:00
|
|
|
func GenerateGroupsArray(groups []database.Group, isAdd bool) []tgbotapi.InlineKeyboardButton {
|
2023-08-03 13:13:09 +04:00
|
|
|
var grKeys []tgbotapi.InlineKeyboardButton
|
|
|
|
for _, gr := range groups {
|
2023-08-03 13:39:59 +04:00
|
|
|
grKeys = append(grKeys, tgbotapi.NewInlineKeyboardButtonData(
|
|
|
|
gr.GroupName,
|
|
|
|
fmt.Sprintf("%t_group_%d", isAdd, gr.GroupId),
|
|
|
|
))
|
2023-08-03 13:13:09 +04:00
|
|
|
}
|
|
|
|
return grKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateName(t database.Teacher) string {
|
|
|
|
var initials string
|
|
|
|
for _, n := range strings.Split(t.FirstName, " ") {
|
|
|
|
initials += fmt.Sprintf("%s.", n[:2])
|
|
|
|
}
|
|
|
|
name := fmt.Sprintf("%s %s", t.LastName, initials)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Создание ряда кнопок из списка преподавателей
|
2023-08-03 13:39:59 +04:00
|
|
|
func GenerateTeachersArray(teachers []database.Teacher, isAdd bool) []tgbotapi.InlineKeyboardButton {
|
2023-08-03 13:13:09 +04:00
|
|
|
var teacherKeys []tgbotapi.InlineKeyboardButton
|
|
|
|
for _, t := range teachers {
|
|
|
|
name := fmt.Sprintf("%s %s", t.FirstName, t.ShortName)
|
2023-08-03 13:39:59 +04:00
|
|
|
teacherKeys = append(teacherKeys, tgbotapi.NewInlineKeyboardButtonData(
|
|
|
|
name,
|
|
|
|
fmt.Sprintf("%t_staff_%d", isAdd, t.TeacherId),
|
|
|
|
))
|
2023-08-03 13:13:09 +04:00
|
|
|
}
|
|
|
|
return teacherKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Создание полноценной клавиатуры выбора
|
|
|
|
func GenerateKeyboard(array []tgbotapi.InlineKeyboardButton) tgbotapi.InlineKeyboardMarkup {
|
|
|
|
var keys []tgbotapi.InlineKeyboardButton
|
|
|
|
var markup [][]tgbotapi.InlineKeyboardButton
|
|
|
|
// Разбиваем список кнопок в ряды по 3 кнопки
|
|
|
|
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("Отмена", "cancel")
|
|
|
|
markup = append(markup, []tgbotapi.InlineKeyboardButton{no_one})
|
|
|
|
return tgbotapi.InlineKeyboardMarkup{InlineKeyboard: markup}
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:52:18 +04:00
|
|
|
func SummaryKeyboard(clickedButton string, sheduleId int64, isGroup bool, dt int) tgbotapi.InlineKeyboardMarkup {
|
|
|
|
tail := GenerateButtonTail(sheduleId, 0, isGroup)
|
2023-08-03 13:13:09 +04:00
|
|
|
|
|
|
|
near := []tgbotapi.InlineKeyboardButton{
|
2023-08-03 21:52:18 +04:00
|
|
|
tgbotapi.NewInlineKeyboardButtonData("Краткая сводка", "sh_near"+tail),
|
2023-08-03 13:13:09 +04:00
|
|
|
}
|
|
|
|
day := []tgbotapi.InlineKeyboardButton{
|
2023-08-03 21:52:18 +04:00
|
|
|
tgbotapi.NewInlineKeyboardButtonData("День", "sh_day"+tail),
|
2023-08-03 13:13:09 +04:00
|
|
|
}
|
|
|
|
week := []tgbotapi.InlineKeyboardButton{
|
2023-08-03 21:52:18 +04:00
|
|
|
tgbotapi.NewInlineKeyboardButtonData("Неделя", "sh_week"+tail),
|
2023-08-03 13:13:09 +04:00
|
|
|
}
|
|
|
|
|
2023-08-03 21:52:18 +04:00
|
|
|
update := GenerateButtonTail(sheduleId, dt, isGroup)
|
2023-08-03 13:13:09 +04:00
|
|
|
var arrows []tgbotapi.InlineKeyboardButton
|
2023-08-03 21:52:18 +04:00
|
|
|
if clickedButton == "sh_day" || clickedButton == "sh_week" {
|
|
|
|
prev_arrow := GenerateButtonTail(sheduleId, dt-1, isGroup)
|
|
|
|
next_arrow := GenerateButtonTail(sheduleId, dt+1, isGroup)
|
2023-08-03 13:13:09 +04:00
|
|
|
arrows = []tgbotapi.InlineKeyboardButton{
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("⏮", clickedButton+prev_arrow),
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("🔄", clickedButton+update),
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("⏭", clickedButton+next_arrow),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arrows = []tgbotapi.InlineKeyboardButton{
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("🔄", clickedButton+update),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*options := []tgbotapi.InlineKeyboardButton{
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("Настройки", "options"),
|
|
|
|
}*/
|
|
|
|
|
|
|
|
var markup [][]tgbotapi.InlineKeyboardButton
|
|
|
|
switch clickedButton {
|
2023-08-03 21:52:18 +04:00
|
|
|
case "sh_day":
|
2023-08-03 13:13:09 +04:00
|
|
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
|
|
|
arrows, near, week,
|
|
|
|
}
|
2023-08-03 21:52:18 +04:00
|
|
|
case "sh_week":
|
2023-08-03 13:13:09 +04:00
|
|
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
|
|
|
arrows, near, day,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
|
|
|
arrows, day, week,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*if sheduleId == 0 {
|
|
|
|
markup = append(markup, options)
|
|
|
|
}*/
|
|
|
|
return tgbotapi.InlineKeyboardMarkup{InlineKeyboard: markup}
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:52:18 +04:00
|
|
|
func GenerateButtonTail(sheduleId int64, dt int, isGroup bool) string {
|
2023-08-03 13:13:09 +04:00
|
|
|
var tail string
|
|
|
|
if sheduleId == 0 {
|
|
|
|
tail = fmt.Sprintf("_personal_%d_0", dt)
|
2023-08-03 21:52:18 +04:00
|
|
|
} else if !isGroup {
|
2023-08-03 13:13:09 +04:00
|
|
|
tail = fmt.Sprintf("_teacher_%d_%d", dt, sheduleId)
|
|
|
|
} else {
|
|
|
|
tail = fmt.Sprintf("_group_%d_%d", dt, sheduleId)
|
|
|
|
}
|
|
|
|
return tail
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:52:18 +04:00
|
|
|
func (bot *Bot) EditOrSend(id int64, str string, markup tgbotapi.InlineKeyboardMarkup, editMsg ...tgbotapi.Message) {
|
2023-08-03 13:13:09 +04:00
|
|
|
if len(editMsg) > 0 {
|
|
|
|
msg := tgbotapi.NewEditMessageText(
|
|
|
|
editMsg[0].Chat.ID,
|
|
|
|
editMsg[0].MessageID,
|
|
|
|
str,
|
|
|
|
)
|
2023-08-05 13:40:24 +04:00
|
|
|
//msg.ReplyMarkup = &markup
|
|
|
|
if _, err := bot.TG.Request(msg); err != nil {
|
|
|
|
bot.Debug.Println(err)
|
|
|
|
}
|
2023-08-03 13:13:09 +04:00
|
|
|
} else {
|
2023-08-03 21:52:18 +04:00
|
|
|
msg := tgbotapi.NewMessage(id, str)
|
|
|
|
//msg.ReplyMarkup = &markup
|
2023-08-05 13:40:24 +04:00
|
|
|
if _, err := bot.TG.Send(msg); err != nil {
|
2023-08-03 21:52:18 +04:00
|
|
|
bot.Debug.Println(err)
|
|
|
|
}
|
2023-08-03 13:13:09 +04:00
|
|
|
}
|
2023-08-03 21:52:18 +04:00
|
|
|
}
|
2023-08-03 13:13:09 +04:00
|
|
|
|
|
|
|
func ParseQuery(data []string) ([]database.ShedulesInUser, int, error) {
|
2023-08-03 21:52:18 +04:00
|
|
|
isGroup := data[2] == "group"
|
|
|
|
sheduleId, err := strconv.ParseInt(data[4], 0, 64)
|
2023-08-03 13:13:09 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
shedule := database.ShedulesInUser{
|
2023-08-03 21:52:18 +04:00
|
|
|
IsGroup: !isGroup,
|
2023-08-03 13:13:09 +04:00
|
|
|
SheduleId: sheduleId,
|
|
|
|
}
|
2023-08-03 21:52:18 +04:00
|
|
|
dt, err := strconv.ParseInt(data[3], 0, 0)
|
2023-08-03 13:13:09 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return []database.ShedulesInUser{shedule}, int(dt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var SumKey = []string{"near", "day", "week"}
|
|
|
|
|
|
|
|
func KeywordContains(str string, keywords []string) bool {
|
|
|
|
for _, key := range keywords {
|
|
|
|
if strings.Contains(str, key) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bot *Bot) DeleteMsg(query *tgbotapi.CallbackQuery) {
|
|
|
|
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID)
|
|
|
|
bot.TG.Request(delete)
|
|
|
|
}
|
2023-08-03 21:52:18 +04:00
|
|
|
|
|
|
|
// Меняем шило на мыло
|
|
|
|
func Swap(sh ssau_parser.WeekShedule) database.ShedulesInUser {
|
|
|
|
return database.ShedulesInUser{
|
|
|
|
IsGroup: sh.IsGroup,
|
|
|
|
SheduleId: sh.SheduleId,
|
|
|
|
}
|
|
|
|
}
|