Skip to content

Commit

Permalink
Use a timetable module for common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Nov 22, 2023
1 parent c564f01 commit fb948b8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
23 changes: 2 additions & 21 deletions bot/callbacks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bot

import (
"fmt"
"log"
"regexp"
"strconv"
Expand Down Expand Up @@ -57,10 +56,7 @@ func lecturesCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_te
log.Printf("Error [bot.Send() for the NewEditMessageText]: %s\n", err)
}
} else if strings.Contains(callback_text, "_y_") {
rows := make([][]tgbotapi.InlineKeyboardButton, 2)
rows[0] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Oggi", fmt.Sprintf("%s_today", callback_text)))
rows[1] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Domani", fmt.Sprintf("%s_tomorrow", callback_text)))

rows := model.ChooseTimetableDay(callback_text)
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(chatId, messageId, keyboard)
_, err := bot.Send(editConfig)
Expand All @@ -69,22 +65,7 @@ func lecturesCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_te
}
} else {
timetableName := strings.TrimPrefix(callback_text, "lectures_")
yearsNro := 3
// Master degrees has a duration of only 2 years
if strings.HasPrefix(callback_text, "lectures_lm") {
yearsNro = 2
}
rows := make([][]tgbotapi.InlineKeyboardButton, yearsNro)

i := 1
for i <= yearsNro {
buttonText := fmt.Sprintf("%s: %d^ anno", model.Timetables[timetableName].Course, i)
buttonCallback := fmt.Sprintf("%s_y_%d", callback_text, i)
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(buttonText, buttonCallback))
rows[i-1] = row

i++
}
rows := model.GetLectureYears(callback_text, model.Timetables[timetableName].Course)
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(chatId, messageId, keyboard)
_, err := bot.Send(editConfig)
Expand Down
34 changes: 7 additions & 27 deletions model/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,43 +136,23 @@ func (data Lectures) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbotapi.Mess
if groupYear != nil {
if len(groupYear.Timetables) == 1 {
callback_text := fmt.Sprintf("lectures_%s_y_%d_", groupYear.Timetables[0], groupYear.Year)
rows := make([][]tgbotapi.InlineKeyboardButton, 2)
rows[0] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Oggi", fmt.Sprintf("%s_today", callback_text)))
rows[1] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Domani", fmt.Sprintf("%s_tomorrow", callback_text)))
rows := ChooseTimetableDay(callback_text)

keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
return makeResponseWithInlineKeyboard(keyboard)
} else {
callback_text := fmt.Sprintf("lectures_%s", groupYear.Timetables[0])
timetableName := strings.TrimPrefix(callback_text, "lectures_")
yearsNro := 3
// Master degrees has a duration of only 2 years
if strings.HasPrefix(callback_text, "lectures_lm") {
yearsNro = 2
}
rows := make([][]tgbotapi.InlineKeyboardButton, yearsNro)

i := 1
for i <= yearsNro {
buttonText := fmt.Sprintf("%s: %d^ anno", Timetables[timetableName].Course, i)
buttonCallback := fmt.Sprintf("%s_y_%d", callback_text, i)
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(buttonText, buttonCallback))
rows[i-1] = row
timetables := make(map[string]Timetable, len(groupYear.Timetables))
for _, t := range groupYear.Timetables {
timetables[t] = Timetables[t]

i++
}
rows := GetTimetableCoursesRows(&timetables)
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
return makeResponseWithInlineKeyboard(keyboard)
}
} else {
rows := make([][]tgbotapi.InlineKeyboardButton, len(Timetables))

i := 0
for callback, timetable := range Timetables {
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(timetable.Course, fmt.Sprintf("lectures_%s", callback)))
rows[i] = row
i++
}
} else {
rows := GetTimetableCoursesRows(&Timetables)
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
return makeResponseWithInlineKeyboard(keyboard)
}
Expand Down
55 changes: 55 additions & 0 deletions model/timetables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package model

import (
"fmt"
"strings"

tgbotapi "github.com/musianisamuele/telegram-bot-api"
)

type InlineKeyboardRows [][]tgbotapi.InlineKeyboardButton

// Returns a group of button rows for a selected groups on `timetables`
func GetTimetableCoursesRows(timetables *map[string]Timetable) InlineKeyboardRows {
rows := make([][]tgbotapi.InlineKeyboardButton, len(*timetables))

i := 0
for callback, timetable := range *timetables {
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(timetable.Course, fmt.Sprintf("lectures_%s", callback)))
rows[i] = row
i++
}

return rows
}

// Returns buttons which permits to choose the day for the timetable
func ChooseTimetableDay(callback_text string) InlineKeyboardRows {
rows := make([][]tgbotapi.InlineKeyboardButton, 2)
rows[0] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Oggi", fmt.Sprintf("%s_today", callback_text)))
rows[1] = tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Domani", fmt.Sprintf("%s_tomorrow", callback_text)))

return rows
}

// Returns a group of buttons rows for the available years of a `course`
func GetLectureYears(callback_text string, course string) InlineKeyboardRows {
yearsNro := 3
// Master degrees has a duration of only 2 years
if strings.HasPrefix(callback_text, "lectures_lm") {
yearsNro = 2
}
rows := make([][]tgbotapi.InlineKeyboardButton, yearsNro)

i := 1
for i <= yearsNro {
buttonText := fmt.Sprintf("%s: %d^ anno", course, i)
buttonCallback := fmt.Sprintf("%s_y_%d", callback_text, i)
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(buttonText, buttonCallback))
rows[i-1] = row

i++
}

return rows
}

0 comments on commit fb948b8

Please sign in to comment.