Skip to content

Commit

Permalink
Merge pull request #150 from csunibo/rappresentanti
Browse files Browse the repository at this point in the history
Rappresentanti
  • Loading branch information
samuelemusiani authored Dec 6, 2023
2 parents 11689b4 + c9f5bce commit 8c6219a
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 29 deletions.
2 changes: 2 additions & 0 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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
Expand Down
8 changes: 8 additions & 0 deletions json/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,13 @@
"description": "Strumento per riassumere le attivit\u00e0 svolte durante il proprio tirocinio",
"text": "\ud83d\udcdd <a href='https://csunibo.github.io/diario-tirocinio/'>Diario Tirocinio</a>\n\ud83d\udcbb <a href='https://github.com/csunibo/diario-tirocinio'>Sorgente</a>"
}
},
"rappresentanti": {
"type": "buttonsRepresentatives",
"data": {
"description": "Comando per elencare tutti i rappresentanti",
"title": "<b>Rappresentanti %s:</b>",
"fallbackText": "Non ci sono rappresentanti per il corso selezionato (%s)"
}
}
}
47 changes: 39 additions & 8 deletions model/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
}

Expand Down Expand Up @@ -110,14 +110,45 @@ 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_")

repData, _ := Representatives[degreeName]
mails := repData.Representatives
courseName := repData.Course

var response string
if len(mails) == 0 {
response = fmt.Sprintf(data.FallbackText, courseName)
} else {
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)
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`")
}
23 changes: 23 additions & 0 deletions model/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"math/rand"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -149,6 +150,28 @@ func (data Lectures) HandleBotCommand(_ *tgbotapi.BotAPI, message *tgbotapi.Mess
return makeResponseWithInlineKeyboard(keyboard)
}

func (data RepresentativesData) HandleBotCommand(_ *tgbotapi.BotAPI,
message *tgbotapi.Message) CommandResponse {
rows := make([][]tgbotapi.InlineKeyboardButton, len(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
}
keyboard := tgbotapi.NewInlineKeyboardMarkup(rows...)
return makeResponseWithInlineKeyboard(keyboard)
}

func (data ListData) HandleBotCommand(*tgbotapi.BotAPI, *tgbotapi.Message) CommandResponse {
resultText := data.Header

Expand Down
4 changes: 4 additions & 0 deletions model/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
24 changes: 15 additions & 9 deletions model/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
}
}
15 changes: 14 additions & 1 deletion model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -69,7 +71,7 @@ type Action struct {
}

type Mantainer struct {
Id int `json:"id"`
Id int64 `json:"id"`
Username string `json:"username"`
}

Expand Down Expand Up @@ -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 []string `json:"representatives"`
}

type InvalidData struct{}
48 changes: 37 additions & 11 deletions model/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ 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"
mantainersFile = "mantainers.json"
representativesFile = "representatives.json"
)

func ParseAutoReplies() (autoReplies []AutoReply, err error) {
Expand Down Expand Up @@ -231,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 {
Expand All @@ -245,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 {
Expand All @@ -254,5 +257,28 @@ 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) {
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
}

0 comments on commit 8c6219a

Please sign in to comment.