Slack: Support use of chat.postMessage (#25818)
* set bearer token if token is provided * update slack notification document Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
co-authored by
Arve Knudsen
parent
75706a4a8b
commit
9f82cd4713
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -291,13 +292,15 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
attachment["image_url"] = imageURL
|
||||
}
|
||||
body := map[string]interface{}{
|
||||
"text": evalContext.GetNotificationTitle(),
|
||||
"blocks": blocks,
|
||||
"text": evalContext.GetNotificationTitle(),
|
||||
"attachments": []map[string]interface{}{
|
||||
attachment,
|
||||
},
|
||||
"parse": "full", // to linkify urls, users and channels in alert message.
|
||||
}
|
||||
if len(blocks) > 0 {
|
||||
body["blocks"] = blocks
|
||||
}
|
||||
|
||||
//recipient override
|
||||
if sn.Recipient != "" {
|
||||
@@ -317,7 +320,17 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := &models.SendWebhookSync{Url: sn.URL, Body: string(data)}
|
||||
cmd := &models.SendWebhookSync{
|
||||
Url: sn.URL,
|
||||
Body: string(data),
|
||||
HttpMethod: http.MethodPost,
|
||||
}
|
||||
if sn.Token != "" {
|
||||
sn.log.Debug("Adding authorization header to HTTP request")
|
||||
cmd.HttpHeader = map[string]string{
|
||||
"Authorization": fmt.Sprintf("Bearer %s", sn.Token),
|
||||
}
|
||||
}
|
||||
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
||||
sn.log.Error("Failed to send slack notification", "error", err, "webhook", sn.Name)
|
||||
return err
|
||||
|
||||
@@ -76,6 +76,7 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode/100 == 2 {
|
||||
ns.log.Debug("Webhook succeeded", "url", webhook.Url, "statuscode", resp.Status)
|
||||
// flushing the body enables the transport to reuse the same connection
|
||||
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
|
||||
ns.log.Error("Failed to copy resp.Body to ioutil.Discard", "err", err)
|
||||
@@ -88,6 +89,6 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
|
||||
return err
|
||||
}
|
||||
|
||||
ns.log.Debug("Webhook failed", "statuscode", resp.Status, "body", string(body))
|
||||
ns.log.Debug("Webhook failed", "url", webhook.Url, "statuscode", resp.Status, "body", string(body))
|
||||
return fmt.Errorf("Webhook response status %v", resp.Status)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user