commit 6f66aab5cf62832dc5dbb42e83b79e472200cd3c Author: olemorud Date: Wed Dec 7 17:54:31 2022 +0100 add cloud function source to git diff --git a/function.go b/function.go new file mode 100644 index 0000000..64d8ad2 --- /dev/null +++ b/function.go @@ -0,0 +1,43 @@ + +import ( + "encoding/json" + "log" + "net/http" +) + +type reply struct { + Response_type string `json:"response_type"` + Text string `json:"text"` +} + +// HelloWorld prints the JSON encoded "message" field in the body +// 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"` + Channel_id string `json:"channel_id"` + Channel_name string `json:"channel_name"` + Team_domain string `json:"team_domain"` + Team_id string `json:"team_id"` + Post_id string `json:"post_id"` + Text string `json:"text"` + Timestamp int64 `json:"timestamp"` + Token string `json:"token"` + Trigger_word string `json:"trigger_word"` + User_id string `json:"user_id"` + User_name string `json:"user_name"` + } + + err := json.NewDecoder(request.Body).Decode(&data) + if err != nil { + log.Printf("json.NewDecoder: %v\n", err) + http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + log.Printf("%+v", data.User_id) + if data.Token == "moha5q8xqjg9prbq7q73676z9y" || data.Token == "33szr8spqi8tx81ymdsxpq5mjh" { + json.NewEncoder(writer).Encode(&reply{Response_type: "comment", Text: "@omorud @ksarband @psvihra"}) + } +} + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e5c1a62 --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module example.com/cloudfunction