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

slack-dm: fix dm message updates #2864

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Changes from all commits
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
44 changes: 37 additions & 7 deletions notification/slack/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slack
import (
"context"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -337,23 +338,41 @@ func alertMsgOption(ctx context.Context, callbackID string, id int, summary, log
)
}

func chanTS(origChannelID, externalID string) (channelID, ts string) {
ts = externalID
if strings.Contains(ts, ":") {
// DMs have a channel ID and timestamp separated by a colon,
// so we need to split them out. Trying to update a message
// with a user ID will fail.
channelID, ts, _ = strings.Cut(ts, ":")
} else {
channelID = origChannelID
}

return channelID, ts
}

func (s *ChannelSender) Send(ctx context.Context, msg notification.Message) (*notification.SentMessage, error) {
cfg := config.FromContext(ctx)

// Note: We don't use cfg.ApplicationName() here since that is configured in the Slack app as the bot name.

var opts []slack.MsgOption
var isUpdate bool
channelID := msg.Destination().Value
switch t := msg.(type) {
case notification.Test:
opts = append(opts, slack.MsgOptionText("This is a test message.", false))
case notification.Verification:
opts = append(opts, slack.MsgOptionText(fmt.Sprintf("Your verification code is: %06d", t.Code), false))
case notification.Alert:
if t.OriginalStatus != nil {
var ts string
channelID, ts = chanTS(channelID, t.OriginalStatus.ProviderMessageID.ExternalID)

// Reply in thread if we already sent a message for this alert.
opts = append(opts,
slack.MsgOptionTS(t.OriginalStatus.ProviderMessageID.ExternalID),
slack.MsgOptionTS(ts),
slack.MsgOptionBroadcast(),
slack.MsgOptionText(alertLink(ctx, t.AlertID, t.Summary), false),
)
Expand All @@ -363,8 +382,10 @@ func (s *ChannelSender) Send(ctx context.Context, msg notification.Message) (*no
opts = append(opts, alertMsgOption(ctx, t.CallbackID, t.AlertID, t.Summary, "Unacknowledged", notification.AlertStateUnacknowledged))
case notification.AlertStatus:
isUpdate = true
var ts string
channelID, ts = chanTS(channelID, t.OriginalStatus.ProviderMessageID.ExternalID)
opts = append(opts,
slack.MsgOptionUpdate(t.OriginalStatus.ProviderMessageID.ExternalID),
slack.MsgOptionUpdate(ts),
alertMsgOption(ctx, t.OriginalStatus.ID, t.AlertID, t.Summary, t.LogEntry, t.NewAlertState),
)
case notification.AlertBundle:
Expand All @@ -377,25 +398,34 @@ func (s *ChannelSender) Send(ctx context.Context, msg notification.Message) (*no
return nil, errors.Errorf("unsupported message type: %T", t)
}

var msgTS string
var externalID string
err := s.withClient(ctx, func(c *slack.Client) error {
_, _msgTS, err := c.PostMessageContext(ctx, msg.Destination().Value, opts...)
msgChan, msgTS, err := c.PostMessageContext(ctx, channelID, opts...)
if err != nil {
return err
}
msgTS = _msgTS
if msgChan != channelID {
// DMs have a generated channel ID that we need to store
// along with the timestamp that does not match the original
// in order to update the message.
externalID = fmt.Sprintf("%s:%s", msgChan, msgTS)
} else {
// For other channels, we can just store the timestamp,
// to preserve compatibility with older versions of GoAlert.
externalID = msgTS
}
return nil
})
if err != nil {
return nil, err
}

if isUpdate {
msgTS = ""
externalID = ""
}

return &notification.SentMessage{
ExternalID: msgTS,
ExternalID: externalID,
State: notification.StateDelivered,
}, nil
}
Expand Down