package slack import ( "encoding/json" "io/ioutil" "net/http" "strings" log "github.com/rs/zerolog/log" ) type Slack struct { Text string `json:"text"` Username string `json:"username"` Attachments []Attachments `json:"attachments"` } type Attachments struct { Fields []Fields `json:"fields"` Text string `json:"text"` } type Fields struct { Title string `json:"title"` Value string `json:"value"` Short bool `json:"short"` } func Connect(manager Slack) { url := "https://hooks.slack.com/services/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" method := "POST" mPayload, err := json.Marshal(manager) if err != nil { log.Print(err) return } log.Print(string(mPayload)) payload := strings.NewReader("payload=" + string(mPayload)) client := &http.Client{} req, err := http.NewRequest(method, url, payload) if err != nil { log.Print(err) return } req.Header.Add("Content-Type", "application/x-www-form-urlencoded") res, err := client.Do(req) if err != nil { log.Print(err) return } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { log.Print(err) return } log.Print(string(body)) } func SendSlack(title string, params map[string]interface{}) { var slackInstance Slack slackInstance.Text = title slackInstance.Username = "CFC System" var fields []Fields for k, v := range params { var field Fields field.Title = k log.Print(field) field.Value = v.(string) field.Short = true fields = append(fields, field) } log.Print(fields) var attachments []Attachments var attachment Attachments attachment.Fields = fields attachment.Text = "CFC" log.Print(attachment) attachments = append(attachments, attachment) slackInstance.Attachments = attachments Connect(slackInstance) log.Print("Slack sent!") }