2021-03-15 20:29:43 +01:00
/ *
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
2021-03-15 20:58:52 +01:00
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
2021-03-15 20:29:43 +01:00
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 )
}
2021-03-22 09:24:33 +01:00
defer resp . Body . Close ( )
2021-03-15 20:29:43 +01:00
if resp . StatusCode != 201 {
log . Fatalln ( "Error while posting message, server returned " + resp . Status )
}
}