Изменено: рефакторинг карточки с расписанием
This commit is contained in:
parent
88e3aa346b
commit
cf518e4aa7
|
@ -6,3 +6,5 @@ logs
|
||||||
*.exe
|
*.exe
|
||||||
*.env
|
*.env
|
||||||
.vscode
|
.vscode
|
||||||
|
*.html
|
||||||
|
*.pdf
|
38
main.go
38
main.go
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
||||||
|
@ -24,6 +23,7 @@ func main() {
|
||||||
|
|
||||||
bot := new(tg.Bot)
|
bot := new(tg.Bot)
|
||||||
bot.Week = 5
|
bot.Week = 5
|
||||||
|
bot.WkPath = os.Getenv("WK_PATH")
|
||||||
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -69,40 +69,8 @@ func main() {
|
||||||
} else if strings.Contains(tg_user.PosTag, "confirm_see") {
|
} else if strings.Contains(tg_user.PosTag, "confirm_see") {
|
||||||
bot.SeeShedule(query)
|
bot.SeeShedule(query)
|
||||||
bot.DeleteMsg(query)
|
bot.DeleteMsg(query)
|
||||||
} else if strings.Contains(query.Data, "day") {
|
} else if tg.KeywordContains(query.Data, tg.SumKey) {
|
||||||
// TODO: Зарефакторить
|
bot.HandleSummary(query)
|
||||||
data := strings.Split(query.Data, "_")
|
|
||||||
if data[1] == "personal" {
|
|
||||||
dt, err := strconv.ParseInt(data[2], 0, 64)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bot.GetPersonalDaySummary(int(dt), *query.Message)
|
|
||||||
} else {
|
|
||||||
shedule, dt, err := tg.ParseQuery(data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bot.GetDaySummary(shedule, dt, false, *query.Message)
|
|
||||||
}
|
|
||||||
} else if strings.Contains(query.Data, "near") {
|
|
||||||
data := strings.Split(query.Data, "_")
|
|
||||||
if data[1] == "personal" {
|
|
||||||
bot.GetPersonalSummary(*query.Message)
|
|
||||||
} else {
|
|
||||||
shedule, _, err := tg.ParseQuery(data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bot.GetSummary(shedule, false, *query.Message)
|
|
||||||
}
|
|
||||||
} else if strings.Contains(query.Data, "week") {
|
|
||||||
data := strings.Split(query.Data, "_")
|
|
||||||
shedule, dw, err := tg.ParseQuery(data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bot.GetWeekSummary(shedule, dw, false, *query.Message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ type Bot struct {
|
||||||
DB *xorm.Engine
|
DB *xorm.Engine
|
||||||
TG_user database.TgUser
|
TG_user database.TgUser
|
||||||
Week int
|
Week int
|
||||||
|
WkPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
|
func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
|
||||||
|
|
|
@ -178,6 +178,34 @@ func (bot *Bot) SeeShedule(query *tgbotapi.CallbackQuery) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *Bot) HandleSummary(query *tgbotapi.CallbackQuery) error {
|
||||||
|
data := strings.Split(query.Data, "_")
|
||||||
|
shedule, dt, err := ParseQuery(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if data[1] == "personal" {
|
||||||
|
switch data[0] {
|
||||||
|
case "day":
|
||||||
|
bot.GetPersonalDaySummary(int(dt), *query.Message)
|
||||||
|
case "week":
|
||||||
|
bot.GetPersonalWeekSummary(int(dt), *query.Message)
|
||||||
|
default:
|
||||||
|
bot.GetPersonalSummary(*query.Message)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch data[0] {
|
||||||
|
case "day":
|
||||||
|
bot.GetDaySummary(shedule, dt, false, *query.Message)
|
||||||
|
case "week":
|
||||||
|
bot.GetWeekSummary(shedule, dt, false, *query.Message)
|
||||||
|
default:
|
||||||
|
bot.GetSummary(shedule, false, *query.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) error {
|
func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) error {
|
||||||
isGroup := bot.TG_user.PosTag == "confirm_add_group"
|
isGroup := bot.TG_user.PosTag == "confirm_add_group"
|
||||||
groupId, err := strconv.ParseInt(query.Data, 0, 64)
|
groupId, err := strconv.ParseInt(query.Data, 0, 64)
|
||||||
|
|
|
@ -65,13 +65,18 @@ func SummaryKeyboard(clickedButton string, sheduleId int64, isTeacher bool, dt i
|
||||||
tgbotapi.NewInlineKeyboardButtonData("Неделя", "week"+tail),
|
tgbotapi.NewInlineKeyboardButtonData("Неделя", "week"+tail),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update := GenerateButtonTail(sheduleId, dt, isTeacher)
|
||||||
var arrows []tgbotapi.InlineKeyboardButton
|
var arrows []tgbotapi.InlineKeyboardButton
|
||||||
if clickedButton == "day" || clickedButton == "week" {
|
if clickedButton == "day" || clickedButton == "week" {
|
||||||
prev_arrow := GenerateButtonTail(sheduleId, dt-1, isTeacher)
|
prev_arrow := GenerateButtonTail(sheduleId, dt-1, isTeacher)
|
||||||
next_arrow := GenerateButtonTail(sheduleId, dt+1, isTeacher)
|
next_arrow := GenerateButtonTail(sheduleId, dt+1, isTeacher)
|
||||||
arrows = []tgbotapi.InlineKeyboardButton{
|
arrows = []tgbotapi.InlineKeyboardButton{
|
||||||
tgbotapi.NewInlineKeyboardButtonData("<", clickedButton+prev_arrow),
|
tgbotapi.NewInlineKeyboardButtonData("⏮", clickedButton+prev_arrow),
|
||||||
tgbotapi.NewInlineKeyboardButtonData(">", clickedButton+next_arrow),
|
tgbotapi.NewInlineKeyboardButtonData("⏭", clickedButton+next_arrow),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
arrows = []tgbotapi.InlineKeyboardButton{
|
||||||
|
tgbotapi.NewInlineKeyboardButtonData("🔄", clickedButton+update),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*options := []tgbotapi.InlineKeyboardButton{
|
/*options := []tgbotapi.InlineKeyboardButton{
|
||||||
|
@ -90,7 +95,7 @@ func SummaryKeyboard(clickedButton string, sheduleId int64, isTeacher bool, dt i
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
markup = [][]tgbotapi.InlineKeyboardButton{
|
markup = [][]tgbotapi.InlineKeyboardButton{
|
||||||
day, week,
|
arrows, day, week,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*if sheduleId == 0 {
|
/*if sheduleId == 0 {
|
||||||
|
@ -102,7 +107,7 @@ func SummaryKeyboard(clickedButton string, sheduleId int64, isTeacher bool, dt i
|
||||||
func GenerateButtonTail(sheduleId int64, dt int, isTeacher bool) string {
|
func GenerateButtonTail(sheduleId int64, dt int, isTeacher bool) string {
|
||||||
var tail string
|
var tail string
|
||||||
if sheduleId == 0 {
|
if sheduleId == 0 {
|
||||||
tail = fmt.Sprintf("_personal_%d", dt)
|
tail = fmt.Sprintf("_personal_%d_0", dt)
|
||||||
} else if isTeacher {
|
} else if isTeacher {
|
||||||
tail = fmt.Sprintf("_teacher_%d_%d", dt, sheduleId)
|
tail = fmt.Sprintf("_teacher_%d_%d", dt, sheduleId)
|
||||||
} else {
|
} else {
|
||||||
|
@ -144,6 +149,17 @@ func ParseQuery(data []string) ([]database.ShedulesInUser, int, error) {
|
||||||
return []database.ShedulesInUser{shedule}, int(dt), nil
|
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) {
|
func (bot *Bot) DeleteMsg(query *tgbotapi.CallbackQuery) {
|
||||||
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID)
|
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID)
|
||||||
bot.TG.Request(delete)
|
bot.TG.Request(delete)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package tg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
@ -45,7 +44,22 @@ func (bot *Bot) GetWeekLessons(shedules []database.ShedulesInUser, week int, isR
|
||||||
|
|
||||||
var days = [6]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
|
var days = [6]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
|
||||||
|
|
||||||
func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPersonal bool, editMsg ...tgbotapi.Message) {
|
func (bot *Bot) GetPersonalWeekSummary(dt int, msg ...tgbotapi.Message) {
|
||||||
|
var shedules []database.ShedulesInUser
|
||||||
|
bot.DB.ID(bot.TG_user.L9Id).Find(&shedules)
|
||||||
|
|
||||||
|
if len(shedules) == 0 {
|
||||||
|
bot.Etc()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
err := bot.GetWeekSummary(shedules, dt, true, msg...)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPersonal bool, editMsg ...tgbotapi.Message) error {
|
||||||
_, week := time.Now().ISOWeek()
|
_, week := time.Now().ISOWeek()
|
||||||
week += dw
|
week += dw
|
||||||
lessons, _ := bot.GetWeekLessons(shedules, week-bot.Week)
|
lessons, _ := bot.GetWeekLessons(shedules, week-bot.Week)
|
||||||
|
@ -94,15 +108,15 @@ func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPer
|
||||||
}
|
}
|
||||||
|
|
||||||
html := bot.CreateHTMLShedule(week, shedule, dates, times)
|
html := bot.CreateHTMLShedule(week, shedule, dates, times)
|
||||||
|
fname := fmt.Sprintf("./%d_%d.html", bot.TG_user.L9Id, week-bot.Week)
|
||||||
f, _ := os.Create("sh.html")
|
f, _ := os.Create(fname)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
f.WriteString(html)
|
f.WriteString(html)
|
||||||
|
|
||||||
wkhtml.SetPath("C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
|
wkhtml.SetPath(bot.WkPath)
|
||||||
pdfg, err := wkhtml.NewPDFGenerator()
|
pdfg, err := wkhtml.NewPDFGenerator()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
pdfg.Dpi.Set(300)
|
pdfg.Dpi.Set(300)
|
||||||
pdfg.MarginBottom.Set(0)
|
pdfg.MarginBottom.Set(0)
|
||||||
|
@ -115,19 +129,20 @@ func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPer
|
||||||
|
|
||||||
err = pdfg.Create()
|
err = pdfg.Create()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pdfg.WriteFile("./sh.pdf")
|
fname = fmt.Sprintf("./%d_%d.pdf", bot.TG_user.L9Id, week-bot.Week)
|
||||||
|
err = pdfg.WriteFile(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
photoBytes, err := ioutil.ReadFile("./sh.pdf")
|
photoBytes, err := os.ReadFile(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
fname := fmt.Sprintf("Расписание %d неделя.pdf", week-bot.Week)
|
fname = fmt.Sprintf("Расписание %d неделя.pdf", week-bot.Week)
|
||||||
photoFileBytes := tgbotapi.FileBytes{
|
photoFileBytes := tgbotapi.FileBytes{
|
||||||
Name: fname,
|
Name: fname,
|
||||||
Bytes: photoBytes,
|
Bytes: photoBytes,
|
||||||
|
@ -136,7 +151,7 @@ func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPer
|
||||||
msg := tgbotapi.NewDocument(bot.TG_user.TgId, photoFileBytes)
|
msg := tgbotapi.NewDocument(bot.TG_user.TgId, photoFileBytes)
|
||||||
_, err = bot.TG.Send(msg)
|
_, err = bot.TG.Send(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return err
|
||||||
}
|
}
|
||||||
var shId int64
|
var shId int64
|
||||||
if isPersonal {
|
if isPersonal {
|
||||||
|
@ -152,6 +167,7 @@ func (bot *Bot) GetWeekSummary(shedules []database.ShedulesInUser, dw int, isPer
|
||||||
)
|
)
|
||||||
str := fmt.Sprintf("Расписание на %d неделю сообщением ниже 👇", week-bot.Week)
|
str := fmt.Sprintf("Расписание на %d неделю сообщением ниже 👇", week-bot.Week)
|
||||||
bot.EditOrSend(str, markup, editMsg...)
|
bot.EditOrSend(str, markup, editMsg...)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const head = `<html lang="ru">
|
const head = `<html lang="ru">
|
||||||
|
|
Reference in New Issue