-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reactions to Slack messages (#1156)
- Loading branch information
Showing
8 changed files
with
202 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package bot | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/slack-go/slack" | ||
) | ||
|
||
const ( | ||
msgReceivedEmoji = "eyes" | ||
msgProcessedEmoji = "white_check_mark" | ||
) | ||
|
||
// SlackReactionClient defines the interface for managing reactions on Slack messages. | ||
type SlackReactionClient interface { | ||
AddReaction(name string, item slack.ItemRef) error | ||
RemoveReaction(name string, item slack.ItemRef) error | ||
} | ||
|
||
// SlackMessageStatusTracker marks messages with emoji for easy tracking the status of Slack messages. | ||
type SlackMessageStatusTracker struct { | ||
log logrus.FieldLogger | ||
client SlackReactionClient | ||
} | ||
|
||
// NewSlackMessageStatusTracker creates a new instance of SlackMessageStatusTracker. | ||
func NewSlackMessageStatusTracker(log logrus.FieldLogger, client SlackReactionClient) *SlackMessageStatusTracker { | ||
return &SlackMessageStatusTracker{log: log, client: client} | ||
} | ||
|
||
// GetMsgRef retrieves the Slack item reference for a given event. | ||
// It returns nil if the message doesn't support reactions or lacks necessary information. | ||
func (b *SlackMessageStatusTracker) GetMsgRef(event slackMessage) *slack.ItemRef { | ||
// We may not have it when it is visible only to a user, | ||
// or it was a modal that has only a trigger ID. | ||
if event.EventTimeStamp == "" || event.Channel == "" { | ||
b.log.WithField("commandOrigin", event.CommandOrigin).Debug("Message doesn't support reactions. Skipping...") | ||
return nil | ||
} | ||
ref := slack.NewRefToMessage(event.Channel, event.EventTimeStamp) | ||
return &ref | ||
} | ||
|
||
// MarkAsReceived marks a message as received by adding the "eyes" reaction. | ||
// If msgRef is nil, no action is performed. | ||
func (b *SlackMessageStatusTracker) MarkAsReceived(msgRef *slack.ItemRef) { | ||
if msgRef == nil { | ||
return | ||
} | ||
err := b.client.AddReaction(msgReceivedEmoji, *msgRef) | ||
b.handleReactionError(err, "received") | ||
} | ||
|
||
// MarkAsProcessed marks a message as processed by removing the "eyes" reaction and adding the "heavy_check_mark" reaction. | ||
// If msgRef is nil, no action is performed. | ||
func (b *SlackMessageStatusTracker) MarkAsProcessed(msgRef *slack.ItemRef) { | ||
if msgRef == nil { | ||
return | ||
} | ||
|
||
_ = b.client.RemoveReaction(msgReceivedEmoji, *msgRef) // The reaction may be missing as there was an error earlier. | ||
|
||
err := b.client.AddReaction(msgProcessedEmoji, *msgRef) | ||
b.handleReactionError(err, "processed") | ||
} | ||
|
||
func (b *SlackMessageStatusTracker) handleReactionError(err error, ctx string) { | ||
logMsg := fmt.Sprintf("Cannot mark message as %s.", ctx) | ||
switch terr := err.(type) { | ||
case nil: | ||
// No error occurred, do nothing. | ||
case slack.SlackErrorResponse: | ||
b.log.WithFields(logrus.Fields{ | ||
"messages": terr.ResponseMetadata.Messages, | ||
}).WithError(err).Warn(logMsg) | ||
default: | ||
b.log.WithError(err).Warn(logMsg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.