37 lines
936 B
Go
Raw Normal View History

2018-08-26 14:12:23 +02:00
package slack
import (
"bytes"
"encoding/json"
"net/http"
"github.com/pkg/errors"
2018-08-26 14:12:23 +02:00
)
type WebhookMessage struct {
Username string `json:"username,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Channel string `json:"channel,omitempty"`
ThreadTimestamp string `json:"thread_ts,omitempty"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
Parse string `json:"parse,omitempty"`
2018-08-26 14:12:23 +02:00
}
func PostWebhook(url string, msg *WebhookMessage) error {
raw, err := json.Marshal(msg)
if err != nil {
return errors.Wrap(err, "marshal failed")
}
response, err := http.Post(url, "application/json", bytes.NewReader(raw))
2018-08-26 14:12:23 +02:00
if err != nil {
return errors.Wrap(err, "failed to post webhook")
}
return checkStatusCode(response, discard{})
2018-08-26 14:12:23 +02:00
}