get mattermost token from environment
This commit is contained in:
25
function.go
25
function.go
@@ -1,8 +1,10 @@
|
|||||||
|
package coffee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type reply struct {
|
type reply struct {
|
||||||
@@ -10,10 +12,7 @@ type reply struct {
|
|||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HelloWorld prints the JSON encoded "message" field in the body
|
type message struct {
|
||||||
// of the request or "Hello, World!" if there isn't one.
|
|
||||||
func Coffee(writer http.ResponseWriter, request *http.Request) {
|
|
||||||
var data struct {
|
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Channel_id string `json:"channel_id"`
|
Channel_id string `json:"channel_id"`
|
||||||
Channel_name string `json:"channel_name"`
|
Channel_name string `json:"channel_name"`
|
||||||
@@ -28,16 +27,26 @@ func Coffee(writer http.ResponseWriter, request *http.Request) {
|
|||||||
User_name string `json:"user_name"`
|
User_name string `json:"user_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := json.NewDecoder(request.Body).Decode(&data)
|
// Coffee() replies to mattermost webhooks with the correct token value
|
||||||
|
func Coffee(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
mattermost_token := os.Getenv("MATTERMOST_TOKEN")
|
||||||
|
|
||||||
|
if mattermost_token == "" {
|
||||||
|
log.Printf("Failed to load mattermost token")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
incoming_msg := message{}
|
||||||
|
err := json.NewDecoder(request.Body).Decode(&incoming_msg)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("json.NewDecoder: %v\n", err)
|
log.Printf("json.NewDecoder: %v\n", err)
|
||||||
http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%+v", data.User_id)
|
log.Printf("Coffee requested by user id: %+v", incoming_msg.User_id)
|
||||||
if data.Token == "moha5q8xqjg9prbq7q73676z9y" || data.Token == "33szr8spqi8tx81ymdsxpq5mjh" {
|
if incoming_msg.Token == mattermost_token || incoming_msg.Token == "33szr8spqi8tx81ymdsxpq5mjh" {
|
||||||
json.NewEncoder(writer).Encode(&reply{Response_type: "comment", Text: "@omorud @ksarband @psvihra"})
|
json.NewEncoder(writer).Encode(&reply{Response_type: "comment", Text: "@omorud @ksarband @psvihra"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user