diff --git a/assos.go b/assos.go
new file mode 100644
index 0000000..d6d709f
--- /dev/null
+++ b/assos.go
@@ -0,0 +1,47 @@
+/*
+ Pont Mattermost Portail des assos
+ Copyright (C) 2021 Romain de Laage
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+*/
+
+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "net/http"
+ "encoding/json"
+)
+
+func getArticles(endpoint string) []Article {
+ resp, err := http.Get(endpoint)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ if resp.StatusCode != 200 {
+ log.Fatalln("Error when fetching articles, server returned " + resp.Status)
+ }
+
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ log.Printf("Successfully got articles")
+ var articles []Article
+ json.Unmarshal(body, &articles)
+
+ return articles
+}
diff --git a/bot.go b/bot.go
deleted file mode 100644
index b37a700..0000000
--- a/bot.go
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- Pont Mattermost Portail des assos
- Copyright (C) 2021 Romain de Laage
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see .
-*/
-
-package main
-
-import (
- "io/ioutil"
- "bufio"
- "bytes"
- "log"
- "net/http"
- "os"
- "encoding/json"
-)
-
-type Owner struct {
- Id string `json:"id"`
- Login string `json:"login"`
- Name string `json:"name"`
- Image string `json:"image"`
- Deleted_at *string `json:"deleted_at,omitempty"`
- In_cemetery_at *string `json:"in_cemetry_at,omitempty"`
- Model string `json:"model"`
-}
-
-type VisibilityT struct {
- Id string `json:"id"`
- Type string `json:"type"`
- Name string `json:"name"`
-}
-
-type Article struct {
- Id string `json:"id"`
- Title string `json:"title"`
- Description string `json:"description"`
- Content string `json:"content"`
- Image string `json:"image,omitempty"`
- Created_at string `json:"created_at"`
- Owned_by Owner `json:"owned_by"`
- Visibility VisibilityT `json:"visibility"`
- Event *string `json:"event,omitempty"`
-}
-
-type Message struct {
- Channel_id string `json:"channel_id"`
- Message_content string `json:"message"`
-}
-
-func main() {
- channel_id := os.Getenv("MM_ASSOS_CHANNEL")
- api_token := os.Getenv("MM_ASSOS_TOKEN")
- api_endpointmm := os.Getenv("MM_ASSOS_ENDPOINTMM")
- api_endpointassos := os.Getenv("MM_ASSOS_ENDPOINTASSOS")
- db_path := os.Getenv("MM_ASSOS_DBPATH")
-
- if channel_id == "" || api_token == "" || api_endpointmm == "" || api_endpointassos == "" || db_path == "" {
- log.Fatalln("MM_ASSOS_CHANNEL, MM_ASSOS_ENDPOINTMM, MM_ASSOS_ENDPOINTASSOS, MM_ASSOS_DBPATH or MM_ASSOS_TOKEN not defined")
- os.Exit(1)
- }
-
- resp, err := http.Get(api_endpointassos)
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
- if resp.StatusCode != 200 {
- log.Fatalln("Error when fetching articles, server returned " + resp.Status)
- os.Exit(1)
- }
-
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
-
- log.Printf("Successfully got articles")
- var articles []Article
- json.Unmarshal(body, &articles)
-
- for i := 0; i < len(articles); i++ {
- db_file, err := os.OpenFile(db_path, os.O_CREATE|os.O_RDWR, 0777)
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
-
- scanner := bufio.NewScanner(db_file)
- scanner.Split(bufio.ScanLines)
- var entries []string
-
- for scanner.Scan() {
- entries = append(entries, scanner.Text())
- }
-
- in_db := 0
-
- for _, entry := range entries {
- if articles[i].Id == entry {
- in_db = 1
- }
- }
-
- if in_db == 0 {
- var message Message
- message.Channel_id = channel_id
- message.Message_content = "# " + articles[i].Title + "\n" + "par *" + articles[i].Owned_by.Name + "*\n\n" + articles[i].Content
-
- json_content, err := json.Marshal(message)
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
-
- req, err := http.NewRequest("POST", api_endpointmm, bytes.NewBuffer(json_content))
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
-
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", "Bearer " + api_token)
-
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
- if resp.StatusCode != 201 {
- log.Fatalln("Error while posting message, server returned " + resp.Status)
- os.Exit(1)
- }
-
- _, err = db_file.WriteString(articles[i].Id + "\n")
- if err != nil {
- log.Fatalln(err)
- os.Exit(1)
- }
- }
-
- db_file.Close()
- }
-}
diff --git a/db.go b/db.go
new file mode 100644
index 0000000..fa1996c
--- /dev/null
+++ b/db.go
@@ -0,0 +1,68 @@
+/*
+ Pont Mattermost Portail des assos
+ Copyright (C) 2021 Romain de Laage
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+*/
+
+package main
+
+import (
+ "bufio"
+ "log"
+ "os"
+)
+
+func openDb(dbPath string) []string {
+ dbFile, err := os.OpenFile(dbPath, os.O_CREATE|os.O_RDONLY, 0644)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ scanner := bufio.NewScanner(dbFile)
+ scanner.Split(bufio.ScanLines)
+ var entries []string
+
+ for scanner.Scan() {
+ entries = append(entries, scanner.Text())
+ }
+
+ dbFile.Close()
+
+ return entries
+}
+
+func addInDb(articleId, dbPath string) {
+ dbFile, err := os.OpenFile(dbPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ _, err = dbFile.WriteString(articleId + "\n")
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ dbFile.Close()
+}
+
+func isInDb(articleId string, entries []string) bool {
+ for _, entry := range entries {
+ if articleId == entry {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..b48dafc
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module main
+
+go 1.16
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..6b4e921
--- /dev/null
+++ b/main.go
@@ -0,0 +1,73 @@
+/*
+ Pont Mattermost Portail des assos
+ Copyright (C) 2021 Romain de Laage
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+*/
+
+package main
+
+type Owner struct {
+ Id string `json:"id"`
+ Login string `json:"login"`
+ Name string `json:"name"`
+ Image string `json:"image"`
+ Deleted_at *string `json:"deleted_at,omitempty"`
+ In_cemetery_at *string `json:"in_cemetry_at,omitempty"`
+ Model string `json:"model"`
+}
+
+type VisibilityT struct {
+ Id string `json:"id"`
+ Type string `json:"type"`
+ Name string `json:"name"`
+}
+
+type Article struct {
+ Id string `json:"id"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+ Content string `json:"content"`
+ Image string `json:"image,omitempty"`
+ Created_at string `json:"created_at"`
+ Owned_by Owner `json:"owned_by"`
+ Visibility VisibilityT `json:"visibility"`
+ Event *string `json:"event,omitempty"`
+}
+
+type Message struct {
+ Channel_id string `json:"channel_id"`
+ Message_content string `json:"message"`
+}
+
+type Config struct {
+ ChannelId string
+ ApiToken string
+ ApiEndpointMM string
+ ApiEndpointAssos string
+ DbPath string
+}
+
+func main() {
+ config := getConfig()
+ articles := getArticles(config.ApiEndpointAssos)
+ entries := openDb(config.DbPath)
+
+ for _, article := range articles {
+ if !isInDb(article.Id, entries) {
+ sendMessage(article, config.ChannelId, config.ApiEndpointMM, config.ApiToken)
+ addInDb(article.Id, config.DbPath)
+ }
+ }
+}
diff --git a/mm.go b/mm.go
new file mode 100644
index 0000000..c94fb91
--- /dev/null
+++ b/mm.go
@@ -0,0 +1,54 @@
+/*
+ Pont Mattermost Portail des assos
+ Copyright (C) 2021 Romain de Laage
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+*/
+
+package main
+
+import (
+ "bytes"
+ "log"
+ "net/http"
+ "encoding/json"
+)
+
+func sendMessage(article Article, channel, endpoint, token string) {
+ var message Message
+ message.Channel_id = channel
+ message.Message_content = "# " + article.Title + "\n" + "par *" + article.Owned_by.Name + "*\n\n" + article.Content
+
+ json_content, err := json.Marshal(message)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(json_content))
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Authorization", "Bearer " + token)
+
+ client := &http.Client{}
+ resp, err := client.Do(req)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ if resp.StatusCode != 201 {
+ log.Fatalln("Error while posting message, server returned " + resp.Status)
+ }
+}
diff --git a/util.go b/util.go
new file mode 100644
index 0000000..6137911
--- /dev/null
+++ b/util.go
@@ -0,0 +1,40 @@
+/*
+ Pont Mattermost Portail des assos
+ Copyright (C) 2021 Romain de Laage
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+*/
+
+package main
+
+import (
+ "log"
+ "os"
+)
+
+func getConfig() Config {
+ var config Config
+
+ config.ChannelId = os.Getenv("MM_ASSOS_CHANNEL")
+ config.ApiToken = os.Getenv("MM_ASSOS_TOKEN")
+ config.ApiEndpointMM = os.Getenv("MM_ASSOS_ENDPOINTMM")
+ config.ApiEndpointAssos = os.Getenv("MM_ASSOS_ENDPOINTASSOS")
+ config.DbPath = os.Getenv("MM_ASSOS_DBPATH")
+
+ if config.ChannelId == "" || config.ApiToken == "" || config.ApiEndpointMM == "" || config.ApiEndpointAssos == "" || config.DbPath == "" {
+ log.Fatalln("MM_ASSOS_CHANNEL, MM_ASSOS_ENDPOINTMM, MM_ASSOS_ENDPOINTASSOS, MM_ASSOS_DBPATH or MM_ASSOS_TOKEN not defined")
+ }
+
+ return config
+}