Commit 122e2b5c by Matthew McGinn

break out slack upload into separate function

parent 89ca4b24
...@@ -163,29 +163,35 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -163,29 +163,35 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
return err return err
} }
if this.Token != "" { if this.Token != "" {
slackUploadUrl := "https://slack.com/api/files.upload" err = SlackFileUpload(evalContext, this.log, "https://slack.com/api/files.upload", this.Recipient, this.Token)
if err != nil {
return err
}
}
return nil
}
func SlackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url string, recipient string, token string) error {
if evalContext.ImageOnDiskPath == "" { if evalContext.ImageOnDiskPath == "" {
evalContext.ImageOnDiskPath = "public/img/mixed_styles.png" evalContext.ImageOnDiskPath = "public/img/mixed_styles.png"
} }
this.log.Info("Uploading to slack via file.upload API") log.Info("Uploading to slack via file.upload API")
headers, uploadBody, err := GenerateSlackUpload(evalContext.ImageOnDiskPath, this.Token, this.Recipient) headers, uploadBody, err := GenerateSlackBody(evalContext.ImageOnDiskPath, token, recipient)
if err != nil { if err != nil {
return err return err
} }
cmd := &m.SendWebhookSync{Url: slackUploadUrl, Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST"} cmd := &m.SendWebhookSync{Url: url, Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST"}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
this.log.Error("Failed to upload slack image", "error", err, "webhook", "file.upload") log.Error("Failed to upload slack image", "error", err, "webhook", "file.upload")
return err return err
} }
if err != nil { if err != nil {
return err return err
} }
}
return nil return nil
} }
func GenerateSlackUpload(file string, token string, recipient string) (map[string]string, bytes.Buffer, error) { func GenerateSlackBody(file string, token string, recipient string) (map[string]string, bytes.Buffer, error) {
// Slack requires all POSTs to files.upload to present // Slack requires all POSTs to files.upload to present
// an "application/x-www-form-urlencoded" encoded querystring // an "application/x-www-form-urlencoded" encoded querystring
// See https://api.slack.com/methods/files.upload // See https://api.slack.com/methods/files.upload
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment