diff --git a/main.go b/main.go index adda675..4db2c4d 100644 --- a/main.go +++ b/main.go @@ -159,7 +159,8 @@ func newHandlerPutAPIV1RPCScrapeSlack(cfg Config) http.HandlerFunc { if cfg.Debug { log.Printf("rpc/scrapeslack => %s", messageJSON) } - if err := cfg.slackToModelPipeline.reader.Enqueue(r.Context(), messageJSON); err != nil { + b, _ := json.Marshal(ChannelWrapper{Channel: channel, V: messageJSON}) + if err := cfg.slackToModelPipeline.reader.Enqueue(r.Context(), b); err != nil { errs = append(errs, err) } else { n += 1 @@ -217,8 +218,8 @@ func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Reques func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - b, _ := io.ReadAll(r.Body) - r.Body = io.NopCloser(bytes.NewReader(b)) + body, _ := io.ReadAll(r.Body) + r.Body = io.NopCloser(bytes.NewReader(body)) var allowList struct { Token string @@ -226,7 +227,7 @@ func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc { Channel string } } - if err := json.Unmarshal(b, &allowList); err != nil { + if err := json.Unmarshal(body, &allowList); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } else if allowList.Token != cfg.SlackToken { @@ -243,7 +244,7 @@ func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc { return } - if err := cfg.slackToModelPipeline.reader.Enqueue(r.Context(), b); err != nil { + if err := cfg.slackToModelPipeline.reader.Enqueue(r.Context(), body); err != nil { log.Printf("failed to ingest: %v", err) http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/slack.go b/slack.go index f7342fa..875898d 100644 --- a/slack.go +++ b/slack.go @@ -227,6 +227,11 @@ func parseSlack(b []byte) (parsedSlackMessage, error) { } func _parseSlack(b []byte) (slackMessage, error) { + var wrapper ChannelWrapper + if err := json.Unmarshal(b, &wrapper); err == nil && len(wrapper.V) > 0 { + b = wrapper.V + } + var result slackMessage err := json.Unmarshal(b, &result) switch result.Type { @@ -247,6 +252,9 @@ func _parseSlack(b []byte) (slackMessage, error) { } result.Event.PreviousMessage = nil } + if wrapper.Channel != "" { + result.Event.Channel = wrapper.Channel + } return result, err } @@ -257,3 +265,8 @@ func (this slackEvent) Empty() bool { func (this parsedSlackMessage) Time() time.Time { return time.Unix(int64(this.TS), 0) } + +type ChannelWrapper struct { + Channel string + V json.RawMessage +} diff --git a/slack_test.go b/slack_test.go index cb00785..7caa95b 100644 --- a/slack_test.go +++ b/slack_test.go @@ -249,3 +249,20 @@ func TestParseSlackTestdata(t *testing.T) { }) } } + +func TestWrappedSlack(t *testing.T) { + b, _ := os.ReadFile("testdata/slack_events/human_thread_message_from_opsgenie_alert.json") + b2, _ := json.Marshal(ChannelWrapper{Channel: "X", V: json.RawMessage(b)}) + + if got, err := _parseSlack(b); err != nil { + t.Fatal(err) + } else if got2, err := _parseSlack(b2); err != nil { + t.Fatal(err) + } else if got2.Event.Channel != "X" { + t.Error(got2.Event.Channel) + } else if got2.Event.ParentID == "" { + t.Error(got2.Event) + } else if got.Event.ParentID != got2.Event.ParentID { + t.Error(got, got2) + } +}