Skip to content

Commit

Permalink
add slack (#318)
Browse files Browse the repository at this point in the history
* add slack
  • Loading branch information
xiaojingchen authored and tennix committed Mar 21, 2019
1 parent bdb746b commit 707e6c3
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions tests/slack/slack.go
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
}

0 comments on commit 707e6c3

Please sign in to comment.