2022-03-12 18:41:07 +00:00
|
|
|
//go:build go1.13
|
2020-03-01 19:59:19 +00:00
|
|
|
// +build go1.13
|
|
|
|
|
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-03-12 18:41:07 +00:00
|
|
|
"fmt"
|
2020-03-01 19:59:19 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PostWebhookCustomHTTPContext(ctx context.Context, url string, httpClient *http.Client, msg *WebhookMessage) error {
|
|
|
|
raw, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
2022-03-12 18:41:07 +00:00
|
|
|
return fmt.Errorf("marshal failed: %w", err)
|
2020-03-01 19:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(raw))
|
|
|
|
if err != nil {
|
2022-03-12 18:41:07 +00:00
|
|
|
return fmt.Errorf("failed new request: %w", err)
|
2020-03-01 19:59:19 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
2022-03-12 18:41:07 +00:00
|
|
|
return fmt.Errorf("failed to post webhook: %w", err)
|
2020-03-01 19:59:19 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return checkStatusCode(resp, discard{})
|
|
|
|
}
|