Skip to content

Commit

Permalink
Merge branch 'main' into buttons-for-lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Nov 26, 2023
2 parents 3b981cf + bd33388 commit 0bee321
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions json/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
"slashes": false
}
},
"issue": {
"type": "issue",
"data": {
"description": "C'è un problema tecnico/tattico, portalo all'attenzione di un mantainer",
"response": "Mantainers, c'è bisogno del vostro aiuto \n",
"fallback": "Nessun mantainer è presente in questo gruppo. Riportate il problema direttamente sul gruppo di @csunibo nel topic Bot"
}
},
"unknown": {
"type": "message",
"data": {
Expand Down
23 changes: 23 additions & 0 deletions model/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ func (data HelpData) HandleBotCommand(*tgbotapi.BotAPI, *tgbotapi.Message) Comma
return makeResponseWithText(answer.String())
}

func (data IssueData) HandleBotCommand(bot *tgbotapi.BotAPI, message *tgbotapi.Message) CommandResponse {
noMantainerFound := true
var answer strings.Builder
var Ids []int64

answer.WriteString(data.Response)

for _, m := range Mantainers {
Ids = append(Ids, int64(m.Id))
}

for i, participant := range utils.GetChatMembers(bot, message.Chat.ID, Ids) {
if Ids[i] == participant.User.ID && participant.User.UserName != "???" {
answer.WriteString("@" + participant.User.UserName + " ")
noMantainerFound = false
}
}
if noMantainerFound {
return makeResponseWithText(data.Fallback)
}
return makeResponseWithText(answer.String())
}

func (data LookingForData) HandleBotCommand(bot *tgbotapi.BotAPI, message *tgbotapi.Message) CommandResponse {
chatTitle := strings.ToLower(message.Chat.Title)

Expand Down
4 changes: 4 additions & 0 deletions model/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ func (d HelpData) GetDescription() string {
return d.Description
}

func (d IssueData) GetDescription() string {
return d.Description
}

func (d LookingForData) GetDescription() string {
return d.Description
}
Expand Down
6 changes: 6 additions & 0 deletions model/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
Teachings map[string]Teaching
Groups GroupsStruct
Timetables map[string]Timetable
Mantainers []Mantainer
)

func InitGlobals() {
Expand Down Expand Up @@ -63,4 +64,9 @@ func InitGlobals() {
if err != nil {
log.Fatalf(err.Error())
}

Mantainers, err = ParseMantainers()
if err != nil {
log.Fatalf("Error parsing mantainers.json file: %s", err.Error())
}
}
13 changes: 13 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func GetActionFromType(name string, commandType string) Action {
data = MessageData{}
case "help":
data = HelpData{}
case "issue":
data = IssueData{}
case "lookingFor":
data = LookingForData{}
case "notLookingFor":
Expand Down Expand Up @@ -65,6 +67,11 @@ type Action struct {
Data DataInterface `json:"data"`
}

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

// config/teachings.json

type Teaching struct {
Expand Down Expand Up @@ -122,6 +129,12 @@ type HelpData struct {
Slashes bool `json:"slashes"`
}

type IssueData struct {
Description string `json:"description"`
Response string `json:"response"`
Fallback string `json:"fallback"`
}

type LookingForData struct {
Description string `json:"description"`
SingularText string `json:"singularText"`
Expand Down
27 changes: 27 additions & 0 deletions model/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,30 @@ func ParseTimetables() (timetables map[string]Timetable, err error) {
timetables = mapData
return
}

func ParseMantainers() (mantainer []Mantainer, err error) {
file, err := os.ReadFile("./json/config/mantainers.json")
if errors.Is(err, os.ErrNotExist) {
return mantainer, fmt.Errorf("mantainers.json does not exist")
} else if err != nil {
return nil, fmt.Errorf("error reading mantainers.json file: %w", err)
}

var projects []struct {
Name string `json:"project"`
Mantainers []Mantainer `json:"mantainers"`
}

err = json.Unmarshal(file, &projects)
if err != nil {
return nil, fmt.Errorf("error parsing mantainers.json file: %w", err)
}

for _, p := range projects {
if p.Name == "informabot" {
return p.Mantainers, nil
}
}

return nil, fmt.Errorf("couldn't found informabot projects after parsing mantainers.json")
}

0 comments on commit 0bee321

Please sign in to comment.