2023-08-03 12:13:09 +03:00
|
|
|
|
package tg
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
2023-08-03 20:52:18 +03:00
|
|
|
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/ssau_parser"
|
2023-08-03 12:13:09 +03:00
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-11 21:20:09 +03:00
|
|
|
|
func GeneralKeyboard(options bool) tgbotapi.ReplyKeyboardMarkup {
|
2023-08-12 12:33:25 +03:00
|
|
|
|
keyboard := [][]tgbotapi.KeyboardButton{{
|
2023-08-11 21:20:09 +03:00
|
|
|
|
tgbotapi.NewKeyboardButton("Моё расписание"),
|
2023-08-12 12:33:25 +03:00
|
|
|
|
}}
|
2023-08-11 21:20:09 +03:00
|
|
|
|
if options {
|
2023-08-12 12:33:25 +03:00
|
|
|
|
keyboard = append(keyboard, []tgbotapi.KeyboardButton{tgbotapi.NewKeyboardButton("Настройки")})
|
2023-08-11 21:20:09 +03:00
|
|
|
|
}
|
2023-08-12 12:33:25 +03:00
|
|
|
|
key := tgbotapi.NewReplyKeyboard(keyboard...)
|
|
|
|
|
key.ResizeKeyboard = true
|
|
|
|
|
return key
|
2023-08-07 21:18:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 12:13:09 +03:00
|
|
|
|
// Создание ряда кнопок из списка групп
|
2023-08-03 12:39:59 +03:00
|
|
|
|
func GenerateGroupsArray(groups []database.Group, isAdd bool) []tgbotapi.InlineKeyboardButton {
|
2023-08-03 12:13:09 +03:00
|
|
|
|
var grKeys []tgbotapi.InlineKeyboardButton
|
|
|
|
|
for _, gr := range groups {
|
2023-08-03 12:39:59 +03:00
|
|
|
|
grKeys = append(grKeys, tgbotapi.NewInlineKeyboardButtonData(
|
|
|
|
|
gr.GroupName,
|
|
|
|
|
fmt.Sprintf("%t_group_%d", isAdd, gr.GroupId),
|
|
|
|
|
))
|
2023-08-03 12:13:09 +03: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 12:39:59 +03:00
|
|
|
|
func GenerateTeachersArray(teachers []database.Teacher, isAdd bool) []tgbotapi.InlineKeyboardButton {
|
2023-08-03 12:13:09 +03:00
|
|
|
|
var teacherKeys []tgbotapi.InlineKeyboardButton
|
|
|
|
|
for _, t := range teachers {
|
|
|
|
|
name := fmt.Sprintf("%s %s", t.FirstName, t.ShortName)
|
2023-08-03 12:39:59 +03:00
|
|
|
|
teacherKeys = append(teacherKeys, tgbotapi.NewInlineKeyboardButtonData(
|
|
|
|
|
name,
|
|
|
|
|
fmt.Sprintf("%t_staff_%d", isAdd, t.TeacherId),
|
|
|
|
|
))
|
2023-08-03 12:13:09 +03: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 20:52:18 +03:00
|
|
|
|
func SummaryKeyboard(clickedButton string, sheduleId int64, isGroup bool, dt int) tgbotapi.InlineKeyboardMarkup {
|
|
|
|
|
tail := GenerateButtonTail(sheduleId, 0, isGroup)
|
2023-08-03 12:13:09 +03:00
|
|
|
|
|
|
|
|
|
near := []tgbotapi.InlineKeyboardButton{
|
2023-08-03 20:52:18 +03:00
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("Краткая сводка", "sh_near"+tail),
|
2023-08-03 12:13:09 +03:00
|
|
|
|
}
|
|
|
|
|
day := []tgbotapi.InlineKeyboardButton{
|
2023-08-03 20:52:18 +03:00
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("День", "sh_day"+tail),
|
2023-08-03 12:13:09 +03:00
|
|
|
|
}
|
2023-08-08 18:36:33 +03:00
|
|
|
|
week := []tgbotapi.InlineKeyboardButton{
|
2023-08-03 20:52:18 +03:00
|
|
|
|
tgbotapi.NewInlineKeyboardButtonData("Неделя", "sh_week"+tail),
|
2023-08-08 18:36:33 +03:00
|
|
|
|
}
|
2023-08-03 12:13:09 +03:00
|
|
|
|
|
2023-08-03 20:52:18 +03:00
|
|
|
|
update := GenerateButtonTail(sheduleId, dt, isGroup)
|
2023-08-03 12:13:09 +03:00
|
|
|
|
var arrows []tgbotapi.InlineKeyboardButton
|
2023-08-03 20:52:18 +03: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 12:13:09 +03: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 20:52:18 +03:00
|
|
|
|
case "sh_day":
|
2023-08-03 12:13:09 +03:00
|
|
|
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
2023-08-08 18:36:33 +03:00
|
|
|
|
arrows, near, week,
|
2023-08-03 12:13:09 +03:00
|
|
|
|
}
|
2023-08-03 20:52:18 +03:00
|
|
|
|
case "sh_week":
|
2023-08-03 12:13:09 +03:00
|
|
|
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
|
|
|
|
arrows, near, day,
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
2023-08-08 18:36:33 +03:00
|
|
|
|
arrows, day, week,
|
2023-08-03 12:13:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*if sheduleId == 0 {
|
|
|
|
|
markup = append(markup, options)
|
|
|
|
|
}*/
|
|
|
|
|
return tgbotapi.InlineKeyboardMarkup{InlineKeyboard: markup}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 20:52:18 +03:00
|
|
|
|
func GenerateButtonTail(sheduleId int64, dt int, isGroup bool) string {
|
2023-08-03 12:13:09 +03:00
|
|
|
|
var tail string
|
|
|
|
|
if sheduleId == 0 {
|
|
|
|
|
tail = fmt.Sprintf("_personal_%d_0", dt)
|
2023-08-03 20:52:18 +03:00
|
|
|
|
} else if !isGroup {
|
2023-08-03 12:13:09 +03:00
|
|
|
|
tail = fmt.Sprintf("_teacher_%d_%d", dt, sheduleId)
|
|
|
|
|
} else {
|
|
|
|
|
tail = fmt.Sprintf("_group_%d_%d", dt, sheduleId)
|
|
|
|
|
}
|
|
|
|
|
return tail
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 14:05:51 +03:00
|
|
|
|
// Отправка сообщения или его редактирование, если в editMsg указано сообщение
|
2023-08-30 08:42:27 +03:00
|
|
|
|
// TODO: Обрабатывать старые сообщения, которые уже нельзя редактировать (message can't be deleted for everyone)
|
2023-08-06 11:26:48 +03:00
|
|
|
|
func (bot *Bot) EditOrSend(
|
|
|
|
|
id int64,
|
|
|
|
|
str string,
|
2023-08-09 14:05:51 +03:00
|
|
|
|
imageId string,
|
2023-08-06 11:26:48 +03:00
|
|
|
|
markup tgbotapi.InlineKeyboardMarkup,
|
2023-08-09 14:05:51 +03:00
|
|
|
|
editMsg ...tgbotapi.Message,
|
|
|
|
|
) (
|
2023-08-06 11:26:48 +03:00
|
|
|
|
tgbotapi.Message,
|
2023-08-09 14:05:51 +03:00
|
|
|
|
error,
|
|
|
|
|
) {
|
|
|
|
|
nilMsg := tgbotapi.Message{}
|
|
|
|
|
|
2023-08-03 12:13:09 +03:00
|
|
|
|
if len(editMsg) > 0 {
|
2023-08-09 14:05:51 +03:00
|
|
|
|
// Редактируем
|
|
|
|
|
if imageId != "" {
|
|
|
|
|
// Обновляем фото, если есть
|
|
|
|
|
// TODO: реализовать нормальное обновление фото, когда нужный метод появится в tgbotapi
|
|
|
|
|
del := tgbotapi.NewDeleteMessage(
|
|
|
|
|
editMsg[0].Chat.ID,
|
|
|
|
|
editMsg[0].MessageID,
|
|
|
|
|
)
|
|
|
|
|
if _, err := bot.TG.Request(del); err != nil {
|
|
|
|
|
return nilMsg, err
|
|
|
|
|
}
|
|
|
|
|
newMsg := tgbotapi.NewPhoto(
|
|
|
|
|
editMsg[0].Chat.ID,
|
|
|
|
|
tgbotapi.FileID(imageId),
|
|
|
|
|
)
|
|
|
|
|
newMsg.Caption = str
|
|
|
|
|
newMsg.ParseMode = tgbotapi.ModeHTML
|
2023-08-11 19:39:46 +03:00
|
|
|
|
if len(markup.InlineKeyboard) != 0 {
|
|
|
|
|
newMsg.ReplyMarkup = &markup
|
|
|
|
|
}
|
2023-08-09 14:05:51 +03:00
|
|
|
|
return bot.TG.Send(newMsg)
|
|
|
|
|
} else if len(editMsg[0].Photo) == 0 {
|
|
|
|
|
// Фото нет и не было, только текст
|
|
|
|
|
msg := tgbotapi.NewEditMessageText(
|
|
|
|
|
editMsg[0].Chat.ID,
|
|
|
|
|
editMsg[0].MessageID,
|
|
|
|
|
str,
|
|
|
|
|
)
|
2023-08-11 19:39:46 +03:00
|
|
|
|
if len(markup.InlineKeyboard) != 0 {
|
|
|
|
|
msg.ReplyMarkup = &markup
|
|
|
|
|
}
|
2023-08-09 14:05:51 +03:00
|
|
|
|
msg.ParseMode = tgbotapi.ModeHTML
|
|
|
|
|
if _, err := bot.TG.Request(msg); err != nil {
|
|
|
|
|
if strings.Contains(err.Error(), "message is not modified") {
|
|
|
|
|
bot.Debug.Println("Message no modified")
|
|
|
|
|
return nilMsg, nil
|
|
|
|
|
}
|
|
|
|
|
return nilMsg, err
|
|
|
|
|
}
|
|
|
|
|
return nilMsg, nil
|
|
|
|
|
} else {
|
|
|
|
|
// Фото было, но теперь его не будет
|
|
|
|
|
del := tgbotapi.NewDeleteMessage(
|
|
|
|
|
editMsg[0].Chat.ID,
|
|
|
|
|
editMsg[0].MessageID,
|
|
|
|
|
)
|
|
|
|
|
if _, err := bot.TG.Request(del); err != nil {
|
|
|
|
|
return nilMsg, err
|
2023-08-06 11:26:48 +03:00
|
|
|
|
}
|
2023-08-09 14:05:51 +03:00
|
|
|
|
|
|
|
|
|
msg := tgbotapi.NewMessage(id, str)
|
2023-08-11 19:39:46 +03:00
|
|
|
|
if len(markup.InlineKeyboard) != 0 {
|
|
|
|
|
msg.ReplyMarkup = &markup
|
|
|
|
|
}
|
2023-08-09 14:05:51 +03:00
|
|
|
|
msg.ParseMode = tgbotapi.ModeHTML
|
|
|
|
|
return bot.TG.Send(msg)
|
2023-08-05 12:40:24 +03:00
|
|
|
|
}
|
2023-08-03 12:13:09 +03:00
|
|
|
|
} else {
|
2023-08-09 14:05:51 +03:00
|
|
|
|
// Обновлений нет, новое сообщение
|
|
|
|
|
if imageId != "" {
|
|
|
|
|
// С фото
|
|
|
|
|
newMsg := tgbotapi.NewPhoto(
|
|
|
|
|
id,
|
|
|
|
|
tgbotapi.FileID(imageId),
|
|
|
|
|
)
|
|
|
|
|
newMsg.Caption = str
|
|
|
|
|
newMsg.ParseMode = tgbotapi.ModeHTML
|
2023-08-11 19:39:46 +03:00
|
|
|
|
if len(markup.InlineKeyboard) != 0 {
|
|
|
|
|
newMsg.ReplyMarkup = &markup
|
|
|
|
|
}
|
2023-08-09 14:05:51 +03:00
|
|
|
|
return bot.TG.Send(newMsg)
|
|
|
|
|
} else {
|
|
|
|
|
// Только текст
|
|
|
|
|
msg := tgbotapi.NewMessage(id, str)
|
2023-08-11 19:39:46 +03:00
|
|
|
|
if len(markup.InlineKeyboard) != 0 {
|
|
|
|
|
msg.ReplyMarkup = &markup
|
2023-08-30 08:42:27 +03:00
|
|
|
|
} else {
|
|
|
|
|
msg.ReplyMarkup = GeneralKeyboard(false)
|
2023-08-11 19:39:46 +03:00
|
|
|
|
}
|
2023-08-09 14:05:51 +03:00
|
|
|
|
msg.ParseMode = tgbotapi.ModeHTML
|
|
|
|
|
return bot.TG.Send(msg)
|
|
|
|
|
}
|
2023-08-03 12:13:09 +03:00
|
|
|
|
}
|
2023-08-03 20:52:18 +03:00
|
|
|
|
}
|
2023-08-03 12:13:09 +03:00
|
|
|
|
|
|
|
|
|
func ParseQuery(data []string) ([]database.ShedulesInUser, int, error) {
|
2023-08-03 20:52:18 +03:00
|
|
|
|
isGroup := data[2] == "group"
|
|
|
|
|
sheduleId, err := strconv.ParseInt(data[4], 0, 64)
|
2023-08-03 12:13:09 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
shedule := database.ShedulesInUser{
|
2023-08-06 11:26:48 +03:00
|
|
|
|
IsGroup: isGroup,
|
2023-08-03 12:13:09 +03:00
|
|
|
|
SheduleId: sheduleId,
|
|
|
|
|
}
|
2023-08-03 20:52:18 +03:00
|
|
|
|
dt, err := strconv.ParseInt(data[3], 0, 0)
|
2023-08-03 12:13:09 +03: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
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 11:26:48 +03:00
|
|
|
|
/*
|
2023-08-03 12:13:09 +03:00
|
|
|
|
func (bot *Bot) DeleteMsg(query *tgbotapi.CallbackQuery) {
|
|
|
|
|
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID)
|
|
|
|
|
bot.TG.Request(delete)
|
2023-08-06 11:26:48 +03:00
|
|
|
|
}*/
|
2023-08-03 20:52:18 +03:00
|
|
|
|
|
|
|
|
|
// Меняем шило на мыло
|
|
|
|
|
func Swap(sh ssau_parser.WeekShedule) database.ShedulesInUser {
|
|
|
|
|
return database.ShedulesInUser{
|
|
|
|
|
IsGroup: sh.IsGroup,
|
|
|
|
|
SheduleId: sh.SheduleId,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-06 14:38:33 +03:00
|
|
|
|
|
2023-08-12 17:15:19 +03:00
|
|
|
|
var Month = []string{
|
2023-08-06 14:38:33 +03:00
|
|
|
|
"января",
|
|
|
|
|
"февраля",
|
|
|
|
|
"марта",
|
|
|
|
|
"апреля",
|
|
|
|
|
"мая",
|
|
|
|
|
"июня",
|
|
|
|
|
"июля",
|
|
|
|
|
"августа",
|
|
|
|
|
"сентября",
|
|
|
|
|
"октября",
|
|
|
|
|
"ноября",
|
|
|
|
|
"декабря",
|
|
|
|
|
}
|
|
|
|
|
var weekdays = []string{
|
|
|
|
|
"воскресенье",
|
|
|
|
|
"понедельник",
|
|
|
|
|
"вторник",
|
|
|
|
|
"среду",
|
|
|
|
|
"четверг",
|
|
|
|
|
"пятницу",
|
|
|
|
|
"субботу",
|
|
|
|
|
}
|