Skip to content

Commit

Permalink
Use callback as interface in model module
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Nov 27, 2023
1 parent 9324821 commit 654f340
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 31 deletions.
17 changes: 16 additions & 1 deletion bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func run(bot *tgbotapi.BotAPI) {
callback_text := update.CallbackQuery.Data

if strings.HasPrefix(callback_text, "lectures_") {
lecturesCallback(bot, &update, callback_text)
handleCallback(bot, &update, "lezioni", callback_text)
}

continue
Expand Down Expand Up @@ -268,6 +268,21 @@ func handleAction(bot *tgbotapi.BotAPI, update *tgbotapi.Update, commandName str
return false
}

// Handle a callback searching a the good action
func handleCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, commandName string, callback_text string) bool {
idx := slices.IndexFunc(model.Actions, func(action model.Action) bool {
return action.Name == commandName
})

if idx != -1 {
model.Actions[idx].Data.HandleBotCallback(bot, update, callback_text)

return true
}

return false
}

func filterMessage(bot *tgbotapi.BotAPI, message *tgbotapi.Message) bool {
if message.Dice != nil {
// msg := tgbotapi.NewMessage(message.Chat.ID, "Found a dice")
Expand Down
4 changes: 3 additions & 1 deletion json/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
"lezioni": {
"type": "buttonsLecture",
"data": {
"description": "Orari lezioni"
"description": "Orari lezioni",
"title": " <b>Lezioni di %s di (%d\u00b0 anno) di giorno %s</b>",
"fallbackText": "Non ci sono lezioni in questo giorno. SMETTILA DI PRESSARMI ao"
}
},
"materiali": {
Expand Down
47 changes: 39 additions & 8 deletions bot/callbacks.go → model/callback.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bot
package model

import (
"fmt"
Expand All @@ -11,15 +11,34 @@ import (
tgbotapi "github.com/musianisamuele/telegram-bot-api"

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

func (_ MessageData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `MessageData`")
}

func (_ HelpData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `HelpData`")
}

func (_ IssueData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `IssueData`")
}

func (_ LookingForData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `LookingForData`")
}

func (_ NotLookingForData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `NotLookingForData`")
}

// 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) {
func (data Lectures) HandleBotCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_text string) {
var chatId = int64(update.CallbackQuery.Message.Chat.ID)
var messageId = update.CallbackQuery.Message.MessageID

Expand Down Expand Up @@ -52,16 +71,16 @@ func lecturesCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_te

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

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

if response == "" {
response = timetable.FallbackText
response = data.FallbackText
} else {
response = fmt.Sprintf(timetable.Title, timetable.Course, year, timeForLectures.Format("2006-01-02")) + "\n\n" + response
response = fmt.Sprintf(data.Title, timetable.Course, year, timeForLectures.Format("2006-01-02")) + "\n\n" + response
}

editConfig := tgbotapi.NewEditMessageText(chatId, messageId, response)
Expand All @@ -72,7 +91,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 := model.ChooseTimetableDay(callback_text)
rows := ChooseTimetableDay(callback_text)
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(chatId, messageId, keyboard)
_, err := bot.Send(editConfig)
Expand All @@ -81,7 +100,7 @@ func lecturesCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_te
}
} else {
timetableName := strings.TrimPrefix(callback_text, "lectures_")
rows := model.GetLectureYears(callback_text, model.Timetables[timetableName].Course)
rows := GetLectureYears(callback_text, Timetables[timetableName].Course)
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
editConfig := tgbotapi.NewEditMessageReplyMarkup(chatId, messageId, keyboard)
_, err := bot.Send(editConfig)
Expand All @@ -90,3 +109,15 @@ func lecturesCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_te
}
}
}

func (_ ListData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `ListData`")
}

func (_ LuckData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `LuckData`")
}

func (_ InvalidData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) {
log.Printf("`HandleBotCallback` not defined for `InvalidData`")
}
2 changes: 2 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type DataInterface interface {
HandleBotCommand(bot *tgbotapi.BotAPI, message *tgbotapi.Message) CommandResponse
HandleBotCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_text string)
GetDescription() string
}

Expand Down Expand Up @@ -153,6 +154,7 @@ type NotLookingForData struct {

type Lectures struct {
Description string `json:"description"`
Title string `json:"title"`
FallbackText string `json:"fallbackText"`
}

Expand Down
2 changes: 1 addition & 1 deletion model/timetables.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GetLectureYears(callback_text string, course string) InlineKeyboardRows {

i := 1
for i <= yearsNro {
buttonText := fmt.Sprintf("%s: %d^ anno", course, i)
buttonText := fmt.Sprintf("%s: %d\u00b0 anno", course, i)
buttonCallback := fmt.Sprintf("%s_y_%d", callback_text, i)
row := tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(buttonText, buttonCallback))
rows[i-1] = row
Expand Down
46 changes: 26 additions & 20 deletions model/timetables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,53 @@ import (
)

func TestGetTimetableCoursesRows(t *testing.T) {
var timetables = map[string]Timetable{
timetables := make([]map[string]Timetable, 2)

timetables[0] = map[string]Timetable{
"l_informatica": {
Course: "Informatica",
Type: "laurea",
Name: "informatica",
},
}
timetables[1] = map[string]Timetable{
"lm_informatica_software_techniques": {
Course: "Informatica Magistrale - Tecniche del software",
Type: "magistrale",
Name: "informatica",
Curriculum: "A58-000",
},
}
wants := make([]InlineKeyboardRows, 2)
wants[0] = InlineKeyboardRows{tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica", "lectures_l_informatica"))}
wants[1] = InlineKeyboardRows{tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica Magistrale - Tecniche del software", "lectures_lm_informatica_software_techniques"))}

type args struct {
data map[string]Timetable
data []map[string]Timetable
}
tests := []struct {
name string
args args
want InlineKeyboardRows
want []InlineKeyboardRows
}{
{
name: "All the timetables from the map",
args: args{data: timetables},
want: InlineKeyboardRows{
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica", "lectures_l_informatica")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica Magistrale - Tecniche del software", "lectures_lm_informatica_software_techniques")),
},
want: wants,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got InlineKeyboardRows = GetTimetableCoursesRows(&tt.args.data)
if len(got) != len(tt.want) {
t.Errorf("GetTimetableCoursesRows() = %v, want %v", got, tt.want)
} else {
for i, v := range got {
for j, w := range v {
if w.Text != tt.want[i][j].Text || *w.CallbackData != *tt.want[i][j].CallbackData {
t.Errorf("GetTimetableCoursesRows() = %v, want %v", w, tt.want[i][j])
for idx, timetable := range tt.args.data {
var got InlineKeyboardRows = GetTimetableCoursesRows(&timetable)
if len(got) != len(tt.want[idx]) {
t.Errorf("GetTimetableCoursesRows() = %v, want %v", got, tt.want)
} else {
for i, v := range got {
for j, w := range v {
if w.Text != tt.want[idx][i][j].Text || *w.CallbackData != *tt.want[idx][i][j].CallbackData {
t.Errorf("GetTimetableCoursesRows() = %v, want %v", w, tt.want[idx][i][j])
}
}
}
}
Expand Down Expand Up @@ -118,17 +124,17 @@ func TestGetLectureYears(t *testing.T) {
name: "Get rows for bachelor's degree",
args: args{data: [2]string{"lectures_l_informatica", "Informatica"}},
want: InlineKeyboardRows{
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica: 1^ anno", "lectures_l_informatica_y_1")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica: 2^ anno", "lectures_l_informatica_y_2")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica: 3^ anno", "lectures_l_informatica_y_3")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica: 1\u00b0 anno", "lectures_l_informatica_y_1")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica: 2\u00b0 anno", "lectures_l_informatica_y_2")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica: 3\u00b0 anno", "lectures_l_informatica_y_3")),
},
},
{
name: "Get rows for master's degree",
args: args{data: [2]string{"lectures_lm_informatica_software_techniques", "Informatica Magistrale"}},
want: InlineKeyboardRows{
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica Magistrale: 1^ anno", "lectures_lm_informatica_software_techniques_y_1")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica Magistrale: 2^ anno", "lectures_lm_informatica_software_techniques_y_2")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica Magistrale: 1\u00b0 anno", "lectures_lm_informatica_software_techniques_y_1")),
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData("Informatica Magistrale: 2\u00b0 anno", "lectures_lm_informatica_software_techniques_y_2")),
},
},
}
Expand Down

0 comments on commit 654f340

Please sign in to comment.