-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package slack | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
var ( | ||
Channel string | ||
WebhookUrl string | ||
) | ||
|
||
type Field struct { | ||
Title string `json:"title"` | ||
Value string `json:"value"` | ||
Short bool `json:"short"` | ||
} | ||
|
||
type Attachment struct { | ||
Fallback string `json:"fallback"` | ||
Color string `json:"color"` | ||
PreText string `json:"pretext"` | ||
AuthorName string `json:"author_name"` | ||
AuthorLink string `json:"author_link"` | ||
AuthorIcon string `json:"author_icon"` | ||
Title string `json:"title"` | ||
TitleLink string `json:"title_link"` | ||
Text string `json:"text"` | ||
ImageUrl string `json:"image_url"` | ||
Fields []*Field `json:"fields"` | ||
Footer string `json:"footer"` | ||
FooterIcon string `json:"footer_icon"` | ||
Timestamp int64 `json:"ts"` | ||
MarkdownIn []string `json:"mrkdwn_in"` | ||
} | ||
|
||
type Payload struct { | ||
Parse string `json:"parse,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
IconUrl string `json:"icon_url,omitempty"` | ||
IconEmoji string `json:"icon_emoji,omitempty"` | ||
Channel string `json:"channel,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
LinkNames string `json:"link_names,omitempty"` | ||
Attachments []Attachment `json:"attachments,omitempty"` | ||
UnfurlLinks bool `json:"unfurl_links,omitempty"` | ||
UnfurlMedia bool `json:"unfurl_media,omitempty"` | ||
} | ||
|
||
func (attachment *Attachment) AddField(field Field) *Attachment { | ||
attachment.Fields = append(attachment.Fields, &field) | ||
return attachment | ||
} | ||
|
||
func Send(webhookUrl string, proxy string, payload Payload) error { | ||
body, err := json.Marshal(payload) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequest("POST", webhookUrl, bytes.NewBuffer(body)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode >= http.StatusBadRequest { | ||
return fmt.Errorf("Error sending msg %+v. Status: %v", payload, resp.Status) | ||
} | ||
return nil | ||
} | ||
|
||
func SendErrMsg(msg string) error { | ||
attachment := Attachment{ | ||
Title: "operator stability test failed", | ||
Color: "fatal", | ||
} | ||
payload := Payload{ | ||
Username: "operator-test", | ||
Channel: Channel, | ||
Text: msg, | ||
IconEmoji: ":ghost:", | ||
Attachments: []Attachment{attachment}, | ||
} | ||
err := Send(WebhookUrl, "", payload) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func SendGoodMsg(msg string) error { | ||
attachment := Attachment{ | ||
Title: "operator stability test succeeded", | ||
Color: "good", | ||
} | ||
payload := Payload{ | ||
Username: "operator-test", | ||
Channel: Channel, | ||
Text: msg, | ||
IconEmoji: ":sun_with_face:", | ||
Attachments: []Attachment{attachment}, | ||
} | ||
err := Send(WebhookUrl, "", payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func SendWarnMsg(msg string) error { | ||
attachment := Attachment{ | ||
Title: "operator stability test happen warning", | ||
Color: "warning", | ||
} | ||
payload := Payload{ | ||
Username: "operator-test", | ||
Channel: Channel, | ||
Text: msg, | ||
IconEmoji: ":imp:", | ||
Attachments: []Attachment{attachment}, | ||
} | ||
err := Send(WebhookUrl, "", payload) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |