From 36304c9a7b7f63a389660d4417569e6be0353440 Mon Sep 17 00:00:00 2001 From: Matt Burchett Date: Thu, 4 Feb 2021 02:29:49 -0600 Subject: [PATCH] Slack endpoint. --- pkg/slack/slack.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pkg/slack/slack.go diff --git a/pkg/slack/slack.go b/pkg/slack/slack.go new file mode 100644 index 0000000..8f70d08 --- /dev/null +++ b/pkg/slack/slack.go @@ -0,0 +1,45 @@ +package slack + +import ( + "encoding/json" + "io/ioutil" + "net/http" + + "git.linuxrocker.com/mattburchett/matrix-handler/pkg/config" + "git.linuxrocker.com/mattburchett/matrix-handler/pkg/matrix" + "git.linuxrocker.com/mattburchett/matrix-handler/pkg/router" + "github.com/gorilla/mux" + "github.com/rs/zerolog/log" +) + +// Handle is the incoming handler for Slack-type requests. +func Handle(cfg config.Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + // Get Matrix Token for User/Pass in path + token := matrix.GetToken(cfg, vars) + + reqBody, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Error().Err(err).Msg("An error has occurred") + } + + data := parseSlack(reqBody) + + resp := matrix.PublishText(cfg, vars, []byte(data), token) + + router.Respond(w, 200, resp) + } + +} + +func parseSlack(body []byte) string { + reqBody := struct { + Text string + }{} + + json.Unmarshal(body, &respbody) + + return respBody.Text +}