2018-08-10 00:38:19 +02:00
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-10-07 23:17:46 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-08-10 00:38:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebhookMessage struct {
|
2018-10-07 23:17:46 +02:00
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
Attachments []Attachment `json:"attachments,omitempty"`
|
2018-08-10 00:38:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func PostWebhook(url string, msg *WebhookMessage) error {
|
|
|
|
raw, err := json.Marshal(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "marshal failed")
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:17:46 +02:00
|
|
|
response, err := http.Post(url, "application/json", bytes.NewReader(raw))
|
2018-08-10 00:38:19 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to post webhook")
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return statusCodeError{Code: response.StatusCode, Status: response.Status}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|