Skip to content

Commit

Permalink
Use callbacks.go module
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Nov 21, 2023
1 parent b3faec3 commit 5a8d83b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 51 deletions.
52 changes: 1 addition & 51 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package bot
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"

tgbotapi "github.com/musianisamuele/telegram-bot-api"
"golang.org/x/exp/slices"

"github.com/csunibo/informabot/commands"
"github.com/csunibo/informabot/model"
"github.com/csunibo/informabot/utils"
)
Expand Down Expand Up @@ -47,53 +43,7 @@ func run(bot *tgbotapi.BotAPI) {
callback_text := update.CallbackQuery.Data

if strings.HasPrefix(callback_text, "lectures_") {
if strings.HasSuffix(callback_text, "_today") || strings.HasSuffix(callback_text, "_tomorrow") {
timeForLectures := time.Now()

if strings.HasSuffix(callback_text, "_tomorrow") {
timeForLectures = timeForLectures.AddDate(0, 0, 1)
}

yearRegex := regexp.MustCompile(`_y_(\d)_`)
year, _ := strconv.Atoi(yearRegex.FindString(callback_text)[3:4])

cdlKey := callback_text[len("lectures_"):strings.Index(callback_text, "_y_")]

cdl := model.Cdls[cdlKey]
response, err := commands.GetTimeTable(cdl.Type, cdl.Name, cdl.Curriculum, year, timeForLectures)
if err != nil {
log.Printf("Error [TomorrowLecturesData]: %s\n", err)
}

editConfig := tgbotapi.NewEditMessageText(int64(update.CallbackQuery.Message.Chat.ID), update.CallbackQuery.Message.MessageID, response)
editConfig.ParseMode = tgbotapi.ModeHTML
bot.Send(editConfig)

} 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)))

keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(int64(update.CallbackQuery.Message.Chat.ID), update.CallbackQuery.Message.MessageID, keyboard)
bot.Send(editConfig)
} else {
years_nro := 3
if strings.HasPrefix(callback_text, "lectures_lm") {
years_nro = 2
}
rows := make([][]tgbotapi.InlineKeyboardButton, years_nro)

i := 1
for i <= years_nro {
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(fmt.Sprintf("%s: %d^ anno", model.Cdls[strings.TrimPrefix(callback_text, "lectures_")].Course, i), fmt.Sprintf("%s_y_%d", callback_text, i)))
rows[i-1] = row
i++
}
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(int64(update.CallbackQuery.Message.Chat.ID), update.CallbackQuery.Message.MessageID, keyboard)
bot.Send(editConfig)
}
lecturesCallback(bot, &update, callback_text)
}

continue
Expand Down
95 changes: 95 additions & 0 deletions bot/callbacks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package bot

import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"

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

"github.com/csunibo/informabot/commands"
"github.com/csunibo/informabot/model"
)

// Handle the callback for the lectures command (`/lezioni`)
// Parse the `callback_text` to check which operation it must to do:
// - If the string ends with "_today" or "_tomorrow" it returns the timetable
// - If the string just contains a "_y_<number>" it asks for today or tomorrow
// - Otherwise prints the course year of what timetable the user wants to see
func lecturesCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_text string) {
var chatId = int64(update.CallbackQuery.Message.Chat.ID)
var messageId = update.CallbackQuery.Message.MessageID

if strings.HasSuffix(callback_text, "_today") || strings.HasSuffix(callback_text, "_tomorrow") {
timeForLectures := time.Now()

if strings.HasSuffix(callback_text, "_tomorrow") {
timeForLectures = timeForLectures.AddDate(0, 0, 1)
}

yearRegex, err := regexp.Compile(`_y_(\d)_`)
if err != nil {
log.Printf("Error [yearRegex]: %s\n", err)
return
}

year, err := strconv.Atoi(yearRegex.FindString(callback_text)[3:4])
if err != nil {
log.Printf("Error [convert to integer the year regex]: %s\n", err)
return
}

cdlKey := callback_text[len("lectures_"):strings.Index(callback_text, "_y_")]

cdl := model.Cdls[cdlKey]
response, err := commands.GetTimeTable(cdl.Type, cdl.Name, cdl.Curriculum, year, timeForLectures)
if err != nil {
log.Printf("Error [GetTimeTable]: %s\n", err)
}

editConfig := tgbotapi.NewEditMessageText(chatId, messageId, response)
editConfig.ParseMode = tgbotapi.ModeHTML
_, err = bot.Send(editConfig)
if err != nil {
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)))

keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(chatId, messageId, keyboard)
_, err := bot.Send(editConfig)
if err != nil {
log.Printf("Error [bot.Send() for the NewEditMessageReplyMarkup]: %s\n", err)
}
} else {
cdlName := 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.Cdls[cdlName].Course, i)
buttonCallback := fmt.Sprintf("%s_y_%d", callback_text, i)
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(buttonText, buttonCallback))
rows[i-1] = row

i++
}
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(chatId, messageId, keyboard)
_, err := bot.Send(editConfig)
if err != nil {
log.Printf("Error [bot.Send() for the NewEditMessageReplyMarkup]: %s\n", err)
}
}
}

0 comments on commit 5a8d83b

Please sign in to comment.