56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
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 + "* (obtenu sur le [portail des assos](https://assos.utc.fr))\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)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 201 {
|
|
log.Fatalln("Error while posting message, server returned " + resp.Status)
|
|
}
|
|
}
|