From c9ba89ececce32d17461895febe2b28c2777f1f8 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Mon, 4 Dec 2023 23:54:21 +0100 Subject: [PATCH 1/9] first implementation of rappresentanti --- bot/bot.go | 7 +++ json/actions.json | 143 +++++++++++++++++++++++++++++++++++-------- model/callback.go | 39 +++++++++--- model/controller.go | 21 +++++++ model/description.go | 4 ++ model/globals.go | 24 +++++--- model/model.go | 15 ++++- model/parse.go | 36 +++++++++-- 8 files changed, 238 insertions(+), 51 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 5349fce..af9560b 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -40,6 +40,8 @@ func run(bot *tgbotapi.BotAPI) { callbackText := update.CallbackQuery.Data + log.Println("callback: " + callbackText) + callback := tgbotapi.NewCallback(update.CallbackQuery.ID, callbackText) if _, err := bot.Request(callback); err != nil { log.Printf("Error [bot.Request() for the callback]: %s\n", err) @@ -48,6 +50,8 @@ func run(bot *tgbotapi.BotAPI) { if strings.HasPrefix(callbackText, "lectures_") { handleCallback(bot, &update, "lezioni", callbackText) + } else if strings.HasPrefix(callbackText, "representatives_") { + handleCallback(bot, &update, "rappresentanti", callbackText) } continue @@ -265,6 +269,9 @@ func handleCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, commandName s return action.Name == commandName }) + log.Println("index: ", idx) + log.Println("Action: ", model.Actions[idx]) + if idx != -1 { model.Actions[idx].Data.HandleBotCallback(bot, update, callback_text) diff --git a/json/actions.json b/json/actions.json index ec1ede9..3b9fb2e 100644 --- a/json/actions.json +++ b/json/actions.json @@ -136,33 +136,114 @@ "header": "Esami a libera scelta:\n", "template": "\ud83c\udfeb %s\n", "items": [ - ["INF%2F01", "INF/01 Informatica"], - ["MAT%2F01", "MAT/01 Logica matematica"], - ["MAT%2F02", "MAT/02 Algebra"], - ["MAT%2F03", "MAT/03 Geometria"], - ["MAT%2F04", "MAT/04 Matematiche compl."], - ["MAT%2F05", "MAT/05 Analisi matematica"], - ["MAT%2F06", "MAT/06 Prob. e stat. mat."], - ["MAT%2F07", "MAT/07 Fisica matematica"], - ["MAT%2F08", "MAT/08 Analisi numerica"], - ["MAT%2F09", "MAT/09 Ricerca operativa"], - ["FIS%2F01", "FIS/01 Fis. sperimentale"], - ["FIS%2F02", "FIS/02 Fisica teorica"], - ["FIS%2F03", "FIS/03 Fis. della materia"], - ["FIS%2F07", "FIS/07 Fisica applicata"], - ["ING-INF%2F01", "ING-INF/01 Elettronica"], - ["ING-INF%2F02", "ING-INF/02 Campi elettromagn."], - ["ING-INF%2F03", "ING-INF/03 Telecomunicazioni"], - ["ING-INF%2F04", "ING-INF/04 Automatica"], - ["ING-INF%2F05", "ING-INF/05 Sis. elab. info."], - ["ING-INF%2F06", "ING-INF/06 Bioing. el. inf."], - ["ING-INF%2F07", "ING-INF/07 Misure elettriche"], - ["SECS-P%2F07", "SECS-P/07 Economia aziendale"], - ["SECS-P%2F08", "SECS-P/08 Economia, gest. impr."], - ["SECS-P%2F09", "SECS-P/09 Finanza aziendale"], - ["SECS-P%2F10", "SECS-P/10 Org. aziendale"], - ["IUS%2F20", "IUS/20 Filsofia del diritto"], - ["M-FIL%2F02", "M-FIL/02 Logica, filo. scienza"] + [ + "INF%2F01", + "INF/01 Informatica" + ], + [ + "MAT%2F01", + "MAT/01 Logica matematica" + ], + [ + "MAT%2F02", + "MAT/02 Algebra" + ], + [ + "MAT%2F03", + "MAT/03 Geometria" + ], + [ + "MAT%2F04", + "MAT/04 Matematiche compl." + ], + [ + "MAT%2F05", + "MAT/05 Analisi matematica" + ], + [ + "MAT%2F06", + "MAT/06 Prob. e stat. mat." + ], + [ + "MAT%2F07", + "MAT/07 Fisica matematica" + ], + [ + "MAT%2F08", + "MAT/08 Analisi numerica" + ], + [ + "MAT%2F09", + "MAT/09 Ricerca operativa" + ], + [ + "FIS%2F01", + "FIS/01 Fis. sperimentale" + ], + [ + "FIS%2F02", + "FIS/02 Fisica teorica" + ], + [ + "FIS%2F03", + "FIS/03 Fis. della materia" + ], + [ + "FIS%2F07", + "FIS/07 Fisica applicata" + ], + [ + "ING-INF%2F01", + "ING-INF/01 Elettronica" + ], + [ + "ING-INF%2F02", + "ING-INF/02 Campi elettromagn." + ], + [ + "ING-INF%2F03", + "ING-INF/03 Telecomunicazioni" + ], + [ + "ING-INF%2F04", + "ING-INF/04 Automatica" + ], + [ + "ING-INF%2F05", + "ING-INF/05 Sis. elab. info." + ], + [ + "ING-INF%2F06", + "ING-INF/06 Bioing. el. inf." + ], + [ + "ING-INF%2F07", + "ING-INF/07 Misure elettriche" + ], + [ + "SECS-P%2F07", + "SECS-P/07 Economia aziendale" + ], + [ + "SECS-P%2F08", + "SECS-P/08 Economia, gest. impr." + ], + [ + "SECS-P%2F09", + "SECS-P/09 Finanza aziendale" + ], + [ + "SECS-P%2F10", + "SECS-P/10 Org. aziendale" + ], + [ + "IUS%2F20", + "IUS/20 Filsofia del diritto" + ], + [ + "M-FIL%2F02", + "M-FIL/02 Logica, filo. scienza" + ] ] } }, @@ -186,5 +267,13 @@ "description": "Strumento per riassumere le attivit\u00e0 svolte durante il proprio tirocinio", "text": "\ud83d\udcdd Diario Tirocinio\n\ud83d\udcbb Sorgente" } + }, + "rappresentanti": { + "type": "buttonsRepresentatives", + "data": { + "description": "Comando per elencare tutti i rappresentanti", + "title": "Rappresentanti:", + "fallback": "Non ci sono rappresentanti per il corso selezionato" + } } } diff --git a/model/callback.go b/model/callback.go index ceeabaf..6332d9b 100644 --- a/model/callback.go +++ b/model/callback.go @@ -13,23 +13,23 @@ import ( "github.com/csunibo/informabot/commands" ) -func (_ MessageData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ MessageData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `MessageData`") } -func (_ HelpData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ HelpData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `HelpData`") } -func (_ IssueData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ IssueData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `IssueData`") } -func (_ LookingForData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ LookingForData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `LookingForData`") } -func (_ NotLookingForData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ NotLookingForData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `NotLookingForData`") } @@ -110,14 +110,37 @@ func (data Lectures) HandleBotCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Up } } -func (_ ListData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (data RepresentativesData) HandleBotCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, callback_text string) { + var chatId = int64(update.CallbackQuery.Message.Chat.ID) + var messageId = update.CallbackQuery.Message.MessageID + + degreeName := strings.TrimPrefix(callback_text, "representatives_") + + var response string + rep := Representatives[degreeName].Representatives + if len(rep) == 0 { + response = data.FallbackText + } else { + response = "Abbiamo i rappresentanti!!" //Da fare il parse degli utenti + } + + 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) + } +} + +func (_ ListData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `ListData`") } -func (_ LuckData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ LuckData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `LuckData`") } -func (_ InvalidData) HandleBotCallback(_bot *tgbotapi.BotAPI, _udpate *tgbotapi.Update, _callback_text string) { +func (_ InvalidData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { log.Printf("`HandleBotCallback` not defined for `InvalidData`") } diff --git a/model/controller.go b/model/controller.go index f9cb8fb..a27f309 100644 --- a/model/controller.go +++ b/model/controller.go @@ -144,8 +144,29 @@ func (data NotLookingForData) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbo } func (data Lectures) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbotapi.Message) CommandResponse { + log.Println("HandleBotCommand: data Lectures") rows := GetTimetableCoursesRows(&Timetables) keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...) + log.Println(rows) + log.Println(keyboard) + return makeResponseWithInlineKeyboard(keyboard) +} + +func (data RepresentativesData) HandleBotCommand(_ *tgbotapi.BotAPI, + message *tgbotapi.Message) CommandResponse { + log.Println("HandleBotCommand: data Representatives") + + rows := make([][]tgbotapi.InlineKeyboardButton, len(Representatives)) + + i := 0 + for callback, repData := range Representatives { + row := tgbotapi.NewInlineKeyboardRow( + tgbotapi.NewInlineKeyboardButtonData(repData.Course, + fmt.Sprintf("representatives_%s", callback))) + rows[i] = row + i++ + } + keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...) return makeResponseWithInlineKeyboard(keyboard) } diff --git a/model/description.go b/model/description.go index 1fc7548..e6f193f 100644 --- a/model/description.go +++ b/model/description.go @@ -32,6 +32,10 @@ func (d LuckData) GetDescription() string { return d.Description } +func (d RepresentativesData) GetDescription() string { + return d.Description +} + func (d InvalidData) GetDescription() string { return "This data is invalidly parsed, please report this bug to the developer." } diff --git a/model/globals.go b/model/globals.go index 94fa601..c611bee 100644 --- a/model/globals.go +++ b/model/globals.go @@ -12,15 +12,16 @@ import ( ) var ( - Autoreplies []AutoReply - Actions []Action - Degrees map[string]Degree - MemeList []Meme - Settings SettingsStruct - Teachings map[string]Teaching - Groups GroupsStruct - Timetables map[string]Timetable - Mantainers []Mantainer + Autoreplies []AutoReply + Actions []Action + Degrees map[string]Degree + MemeList []Meme + Settings SettingsStruct + Teachings map[string]Teaching + Groups GroupsStruct + Timetables map[string]Timetable + Mantainers []Mantainer + Representatives map[string]Representative ) func InitGlobals() { @@ -69,4 +70,9 @@ func InitGlobals() { if err != nil { log.Fatalf("Error parsing mantainers.json file: %s", err.Error()) } + + Representatives, err = ParseRepresentatives() + if err != nil { + log.Fatalf("Error parsing representatives.json file: %s", err.Error()) + } } diff --git a/model/model.go b/model/model.go index 4e98815..abb32cb 100644 --- a/model/model.go +++ b/model/model.go @@ -30,6 +30,8 @@ func GetActionFromType(name string, commandType string) Action { data = NotLookingForData{} case "buttonsLecture": data = Lectures{} + case "buttonsRepresentatives": + data = RepresentativesData{} case "list": data = ListData{} case "luck": @@ -69,7 +71,7 @@ type Action struct { } type Mantainer struct { - Id int `json:"id"` + Id int64 `json:"id"` Username string `json:"username"` } @@ -170,4 +172,15 @@ type LuckData struct { NoLuckGroupText string `json:"noLuckGroupText"` } +type RepresentativesData struct { + Description string `json:"description"` + Title string `json:"title"` + FallbackText string `json:"fallbackText"` +} + +type Representative struct { + Course string `json:"course"` + Representatives []int64 `json:"representatives"` +} + type InvalidData struct{} diff --git a/model/parse.go b/model/parse.go index 3db1ca7..3cd9765 100644 --- a/model/parse.go +++ b/model/parse.go @@ -16,12 +16,13 @@ import ( ) const ( - jsonPath = "./json/" - groupsFile = "groups.json" - configSubpath = "config/" - degreesFile = "degrees.json" - teachingsFile = "teachings.json" - timetablesFile = "timetables.json" + jsonPath = "./json/" + groupsFile = "groups.json" + configSubpath = "config/" + degreesFile = "degrees.json" + teachingsFile = "teachings.json" + timetablesFile = "timetables.json" + representativesFile = "representatives.json" ) func ParseAutoReplies() (autoReplies []AutoReply, err error) { @@ -256,3 +257,26 @@ func ParseMantainers() (mantainer []Mantainer, err error) { return nil, fmt.Errorf("couldn't found informabot projects after parsing mantainers.json") } + +func ParseRepresentatives() (map[string]Representative, error) { + representatives := make(map[string]Representative) + + filepath := filepath.Join(jsonPath, configSubpath, representativesFile) + byteValue, err := os.ReadFile(filepath) + if errors.Is(err, os.ErrNotExist) { + return representatives, nil + } else if err != nil { + return nil, fmt.Errorf("error reading %s file: %w", filepath, err) + } + + err = json.Unmarshal(byteValue, &representatives) + if err != nil { + return nil, fmt.Errorf("error parsing %s file: %w", filepath, err) + } + + if representatives == nil { + representatives = make(map[string]Representative) + } + + return representatives, nil +} From bb450e9212b4cceaf158e53beba881806490a7d3 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Mon, 4 Dec 2023 23:56:39 +0100 Subject: [PATCH 2/9] fix: generalized mantainers file --- model/parse.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/model/parse.go b/model/parse.go index 3cd9765..4762c08 100644 --- a/model/parse.go +++ b/model/parse.go @@ -22,6 +22,7 @@ const ( degreesFile = "degrees.json" teachingsFile = "teachings.json" timetablesFile = "timetables.json" + mantainersFile = "mantainers.json" representativesFile = "representatives.json" ) @@ -232,11 +233,12 @@ func ParseTimetables() (timetables map[string]Timetable, err error) { } func ParseMantainers() (mantainer []Mantainer, err error) { - file, err := os.ReadFile("./json/config/mantainers.json") + filepath := filepath.Join(jsonPath, configSubpath, mantainersFile) + file, err := os.ReadFile(filepath) if errors.Is(err, os.ErrNotExist) { - return mantainer, fmt.Errorf("mantainers.json does not exist") + return mantainer, fmt.Errorf("%s does not exist", mantainersFile) } else if err != nil { - return nil, fmt.Errorf("error reading mantainers.json file: %w", err) + return nil, fmt.Errorf("error reading %s file: %w", mantainersFile, err) } var projects []struct { @@ -246,7 +248,7 @@ func ParseMantainers() (mantainer []Mantainer, err error) { err = json.Unmarshal(file, &projects) if err != nil { - return nil, fmt.Errorf("error parsing mantainers.json file: %w", err) + return nil, fmt.Errorf("error parsing %s file: %w", mantainersFile, err) } for _, p := range projects { @@ -255,7 +257,7 @@ func ParseMantainers() (mantainer []Mantainer, err error) { } } - return nil, fmt.Errorf("couldn't found informabot projects after parsing mantainers.json") + return nil, fmt.Errorf("couldn't found informabot projects after parsing %s", mantainersFile) } func ParseRepresentatives() (map[string]Representative, error) { From decc5c489e7958447e1cc6649fcf8b6dcd5c5584 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 00:18:36 +0100 Subject: [PATCH 3/9] fix: fallbackText --- json/actions.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json/actions.json b/json/actions.json index 3b9fb2e..a5fe870 100644 --- a/json/actions.json +++ b/json/actions.json @@ -273,7 +273,7 @@ "data": { "description": "Comando per elencare tutti i rappresentanti", "title": "Rappresentanti:", - "fallback": "Non ci sono rappresentanti per il corso selezionato" + "fallbackText": "Non ci sono rappresentanti per il corso selezionato" } } } From 9fc6e6fa3e4a1253f1ac90ff39ff04c7a8f80cb2 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 00:24:16 +0100 Subject: [PATCH 4/9] make buttons sorted --- model/controller.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/model/controller.go b/model/controller.go index a27f309..2ee843b 100644 --- a/model/controller.go +++ b/model/controller.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "math/rand" + "sort" "strings" "time" @@ -158,13 +159,19 @@ func (data RepresentativesData) HandleBotCommand(_ *tgbotapi.BotAPI, rows := make([][]tgbotapi.InlineKeyboardButton, len(Representatives)) - i := 0 - for callback, repData := range Representatives { + // get all keys in orderd to iterate on them sorted + keys := make([]string, 0) + for k := range Representatives { + keys = append(keys, k) + } + sort.Strings(keys) + + for i, callback := range keys { + repData := Representatives[callback] row := tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData(repData.Course, fmt.Sprintf("representatives_%s", callback))) rows[i] = row - i++ } keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...) return makeResponseWithInlineKeyboard(keyboard) From ea25b4b8f2575228d6ae06976c75f5908df20e79 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 00:30:23 +0100 Subject: [PATCH 5/9] response with course name --- json/actions.json | 4 ++-- model/callback.go | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/json/actions.json b/json/actions.json index a5fe870..f22d49e 100644 --- a/json/actions.json +++ b/json/actions.json @@ -272,8 +272,8 @@ "type": "buttonsRepresentatives", "data": { "description": "Comando per elencare tutti i rappresentanti", - "title": "Rappresentanti:", - "fallbackText": "Non ci sono rappresentanti per il corso selezionato" + "title": "Rappresentanti %s:", + "fallbackText": "Non ci sono rappresentanti per il corso selezionato (%s)" } } } diff --git a/model/callback.go b/model/callback.go index 6332d9b..ef93333 100644 --- a/model/callback.go +++ b/model/callback.go @@ -117,11 +117,13 @@ func (data RepresentativesData) HandleBotCallback(bot *tgbotapi.BotAPI, update * degreeName := strings.TrimPrefix(callback_text, "representatives_") var response string - rep := Representatives[degreeName].Representatives - if len(rep) == 0 { - response = data.FallbackText + repData, _ := Representatives[degreeName] + reps := repData.Representatives + courseName := repData.Course + if len(reps) == 0 { + response = fmt.Sprintf(data.FallbackText, courseName) } else { - response = "Abbiamo i rappresentanti!!" //Da fare il parse degli utenti + response = fmt.Sprintf(data.Title, courseName) + "\n\n" + response } editConfig := tgbotapi.NewEditMessageText(chatId, messageId, response) From 0daac82b25f3aeba0c138081a8480a356a538816 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 01:08:15 +0100 Subject: [PATCH 6/9] get usernames from ids if in the same group --- model/callback.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/model/callback.go b/model/callback.go index ef93333..1ff8ff7 100644 --- a/model/callback.go +++ b/model/callback.go @@ -11,6 +11,7 @@ import ( tgbotapi "github.com/musianisamuele/telegram-bot-api" "github.com/csunibo/informabot/commands" + "github.com/csunibo/informabot/utils" ) func (_ MessageData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { @@ -116,14 +117,33 @@ func (data RepresentativesData) HandleBotCallback(bot *tgbotapi.BotAPI, update * degreeName := strings.TrimPrefix(callback_text, "representatives_") - var response string repData, _ := Representatives[degreeName] - reps := repData.Representatives + ids := repData.Representatives courseName := repData.Course - if len(reps) == 0 { + + var response string + if len(ids) == 0 { response = fmt.Sprintf(data.FallbackText, courseName) } else { - response = fmt.Sprintf(data.Title, courseName) + "\n\n" + response + // Get chat mebers based on ids + var tmp strings.Builder + noRepresentativeFound := true + for i, participant := range utils.GetChatMembers(bot, chatId, ids) { + if ids[i] == participant.User.ID && + participant.User.UserName != "???" { + tmp.WriteString("@" + participant.User.UserName + "\n") + noRepresentativeFound = false + } + } + if noRepresentativeFound { + response = fmt.Sprintf(data.Title, courseName) + "\n\n" + "Nessun rappresentante del corso è presente nel gruppo." + // Fallback sulle mail? + // Hardcodiamo gli username? + // Qui srvirebbero sempre tutti i rappresentanti indipendentemente dal + // gruppo o no + } else { + response = fmt.Sprintf(data.Title, courseName) + "\n\n" + tmp.String() + } } editConfig := tgbotapi.NewEditMessageText(chatId, messageId, response) From 26cc702eee8e076b8867b37fe3eeb3bb09932400 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 01:12:16 +0100 Subject: [PATCH 7/9] json formatting --- json/actions.json | 135 ++++++++++------------------------------------ 1 file changed, 27 insertions(+), 108 deletions(-) diff --git a/json/actions.json b/json/actions.json index f22d49e..649382d 100644 --- a/json/actions.json +++ b/json/actions.json @@ -136,114 +136,33 @@ "header": "Esami a libera scelta:\n", "template": "\ud83c\udfeb %s\n", "items": [ - [ - "INF%2F01", - "INF/01 Informatica" - ], - [ - "MAT%2F01", - "MAT/01 Logica matematica" - ], - [ - "MAT%2F02", - "MAT/02 Algebra" - ], - [ - "MAT%2F03", - "MAT/03 Geometria" - ], - [ - "MAT%2F04", - "MAT/04 Matematiche compl." - ], - [ - "MAT%2F05", - "MAT/05 Analisi matematica" - ], - [ - "MAT%2F06", - "MAT/06 Prob. e stat. mat." - ], - [ - "MAT%2F07", - "MAT/07 Fisica matematica" - ], - [ - "MAT%2F08", - "MAT/08 Analisi numerica" - ], - [ - "MAT%2F09", - "MAT/09 Ricerca operativa" - ], - [ - "FIS%2F01", - "FIS/01 Fis. sperimentale" - ], - [ - "FIS%2F02", - "FIS/02 Fisica teorica" - ], - [ - "FIS%2F03", - "FIS/03 Fis. della materia" - ], - [ - "FIS%2F07", - "FIS/07 Fisica applicata" - ], - [ - "ING-INF%2F01", - "ING-INF/01 Elettronica" - ], - [ - "ING-INF%2F02", - "ING-INF/02 Campi elettromagn." - ], - [ - "ING-INF%2F03", - "ING-INF/03 Telecomunicazioni" - ], - [ - "ING-INF%2F04", - "ING-INF/04 Automatica" - ], - [ - "ING-INF%2F05", - "ING-INF/05 Sis. elab. info." - ], - [ - "ING-INF%2F06", - "ING-INF/06 Bioing. el. inf." - ], - [ - "ING-INF%2F07", - "ING-INF/07 Misure elettriche" - ], - [ - "SECS-P%2F07", - "SECS-P/07 Economia aziendale" - ], - [ - "SECS-P%2F08", - "SECS-P/08 Economia, gest. impr." - ], - [ - "SECS-P%2F09", - "SECS-P/09 Finanza aziendale" - ], - [ - "SECS-P%2F10", - "SECS-P/10 Org. aziendale" - ], - [ - "IUS%2F20", - "IUS/20 Filsofia del diritto" - ], - [ - "M-FIL%2F02", - "M-FIL/02 Logica, filo. scienza" - ] + ["INF%2F01", "INF/01 Informatica"], + ["MAT%2F01", "MAT/01 Logica matematica"], + ["MAT%2F02", "MAT/02 Algebra"], + ["MAT%2F03", "MAT/03 Geometria"], + ["MAT%2F04", "MAT/04 Matematiche compl."], + ["MAT%2F05", "MAT/05 Analisi matematica"], + ["MAT%2F06", "MAT/06 Prob. e stat. mat."], + ["MAT%2F07", "MAT/07 Fisica matematica"], + ["MAT%2F08", "MAT/08 Analisi numerica"], + ["MAT%2F09", "MAT/09 Ricerca operativa"], + ["FIS%2F01", "FIS/01 Fis. sperimentale"], + ["FIS%2F02", "FIS/02 Fisica teorica"], + ["FIS%2F03", "FIS/03 Fis. della materia"], + ["FIS%2F07", "FIS/07 Fisica applicata"], + ["ING-INF%2F01", "ING-INF/01 Elettronica"], + ["ING-INF%2F02", "ING-INF/02 Campi elettromagn."], + ["ING-INF%2F03", "ING-INF/03 Telecomunicazioni"], + ["ING-INF%2F04", "ING-INF/04 Automatica"], + ["ING-INF%2F05", "ING-INF/05 Sis. elab. info."], + ["ING-INF%2F06", "ING-INF/06 Bioing. el. inf."], + ["ING-INF%2F07", "ING-INF/07 Misure elettriche"], + ["SECS-P%2F07", "SECS-P/07 Economia aziendale"], + ["SECS-P%2F08", "SECS-P/08 Economia, gest. impr."], + ["SECS-P%2F09", "SECS-P/09 Finanza aziendale"], + ["SECS-P%2F10", "SECS-P/10 Org. aziendale"], + ["IUS%2F20", "IUS/20 Filsofia del diritto"], + ["M-FIL%2F02", "M-FIL/02 Logica, filo. scienza"] ] } }, From fd469a55c1938035812dbf9141f73a72ebebd9d7 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 01:20:36 +0100 Subject: [PATCH 8/9] removed debug prints --- bot/bot.go | 5 ----- model/controller.go | 5 ----- 2 files changed, 10 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index af9560b..8d15b64 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -40,8 +40,6 @@ func run(bot *tgbotapi.BotAPI) { callbackText := update.CallbackQuery.Data - log.Println("callback: " + callbackText) - callback := tgbotapi.NewCallback(update.CallbackQuery.ID, callbackText) if _, err := bot.Request(callback); err != nil { log.Printf("Error [bot.Request() for the callback]: %s\n", err) @@ -269,9 +267,6 @@ func handleCallback(bot *tgbotapi.BotAPI, update *tgbotapi.Update, commandName s return action.Name == commandName }) - log.Println("index: ", idx) - log.Println("Action: ", model.Actions[idx]) - if idx != -1 { model.Actions[idx].Data.HandleBotCallback(bot, update, callback_text) diff --git a/model/controller.go b/model/controller.go index 2ee843b..2a829d7 100644 --- a/model/controller.go +++ b/model/controller.go @@ -145,18 +145,13 @@ func (data NotLookingForData) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbo } func (data Lectures) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbotapi.Message) CommandResponse { - log.Println("HandleBotCommand: data Lectures") rows := GetTimetableCoursesRows(&Timetables) keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...) - log.Println(rows) - log.Println(keyboard) return makeResponseWithInlineKeyboard(keyboard) } func (data RepresentativesData) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbotapi.Message) CommandResponse { - log.Println("HandleBotCommand: data Representatives") - rows := make([][]tgbotapi.InlineKeyboardButton, len(Representatives)) // get all keys in orderd to iterate on them sorted From c9f5bcef630e2875b119338ee098954b96f01816 Mon Sep 17 00:00:00 2001 From: Samuele Musiani Date: Tue, 5 Dec 2023 11:00:37 +0100 Subject: [PATCH 9/9] mail on response --- model/callback.go | 28 +++++++--------------------- model/model.go | 4 ++-- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/model/callback.go b/model/callback.go index 1ff8ff7..2340893 100644 --- a/model/callback.go +++ b/model/callback.go @@ -11,7 +11,6 @@ import ( tgbotapi "github.com/musianisamuele/telegram-bot-api" "github.com/csunibo/informabot/commands" - "github.com/csunibo/informabot/utils" ) func (_ MessageData) HandleBotCallback(_bot *tgbotapi.BotAPI, _update *tgbotapi.Update, _callback_text string) { @@ -118,32 +117,19 @@ func (data RepresentativesData) HandleBotCallback(bot *tgbotapi.BotAPI, update * degreeName := strings.TrimPrefix(callback_text, "representatives_") repData, _ := Representatives[degreeName] - ids := repData.Representatives + mails := repData.Representatives courseName := repData.Course var response string - if len(ids) == 0 { + if len(mails) == 0 { response = fmt.Sprintf(data.FallbackText, courseName) } else { - // Get chat mebers based on ids - var tmp strings.Builder - noRepresentativeFound := true - for i, participant := range utils.GetChatMembers(bot, chatId, ids) { - if ids[i] == participant.User.ID && - participant.User.UserName != "???" { - tmp.WriteString("@" + participant.User.UserName + "\n") - noRepresentativeFound = false - } - } - if noRepresentativeFound { - response = fmt.Sprintf(data.Title, courseName) + "\n\n" + "Nessun rappresentante del corso è presente nel gruppo." - // Fallback sulle mail? - // Hardcodiamo gli username? - // Qui srvirebbero sempre tutti i rappresentanti indipendentemente dal - // gruppo o no - } else { - response = fmt.Sprintf(data.Title, courseName) + "\n\n" + tmp.String() + tmp := strings.Builder{} + for _, m := range mails { + tmp.WriteString(m) + tmp.WriteString("\n") } + response = fmt.Sprintf(data.Title, courseName) + "\n\n" + tmp.String() } editConfig := tgbotapi.NewEditMessageText(chatId, messageId, response) diff --git a/model/model.go b/model/model.go index abb32cb..6f21b49 100644 --- a/model/model.go +++ b/model/model.go @@ -179,8 +179,8 @@ type RepresentativesData struct { } type Representative struct { - Course string `json:"course"` - Representatives []int64 `json:"representatives"` + Course string `json:"course"` + Representatives []string `json:"representatives"` } type InvalidData struct{}