Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add slack #318

Merged
merged 2 commits into from
Mar 18, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 success",
xiaojingchen marked this conversation as resolved.
Show resolved Hide resolved
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
}