Skip to content

Commit

Permalink
feat(slack): use attachments for easier view
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Jul 5, 2024
1 parent 92576ac commit 1ae224e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
52 changes: 28 additions & 24 deletions pkg/bot/slack/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func (ctx *slackContext) L() *zap.Logger {
}

func (ctx *slackContext) MessageLimit() int {
// https://stackoverflow.com/questions/60344831/slack-api-invalid-block
return 2500
return 2000
}

func (ctx *slackContext) StampNames() *domain.StampNames {
Expand All @@ -57,21 +56,26 @@ func (ctx *slackContext) StampNames() *domain.StampNames {
}
}

func (ctx *slackContext) sendSlackMessage(channelID string, text string) error {
func (ctx *slackContext) sendSlackMessage(channelID string, lines []string, color string) error {
api := ctx.api
return utils.WithRetry(ctx, 10, func(ctx context.Context) error {
_, _, err := api.PostMessage(channelID, slack.MsgOptionBlocks(
slack.NewSectionBlock(
slack.NewTextBlockObject(
slack.MarkdownType,
text,
false,
false,
),
nil,
nil,
),
))
var options []slack.MsgOption
options = append(options, slack.MsgOptionText(lines[0], false))
if len(lines) >= 2 {
options = append(options, slack.MsgOptionAttachments(
slack.Attachment{
Color: color,
Fields: []slack.AttachmentField{
{
Title: "",
Value: strings.Join(lines[1:], "\n"),
Short: false,
},
},
},
))
}
_, _, err := api.PostMessage(channelID, options...)
return err
})
}
Expand All @@ -83,17 +87,17 @@ func (ctx *slackContext) pushSlackReaction(message slack.ItemRef, stampID string
})
}

func (ctx *slackContext) reply(message ...string) error {
return ctx.sendSlackMessage(ctx.message.Channel, strings.Join(message, "\n"))
func (ctx *slackContext) reply(color string, message ...string) error {
return ctx.sendSlackMessage(ctx.message.Channel, message, color)
}

func (ctx *slackContext) replyWithStamp(stamp string, message ...string) error {
func (ctx *slackContext) replyWithStamp(stamp string, color string, message ...string) error {
err := ctx.pushSlackReaction(ctx.message, stamp)
if err != nil {
return err
}
if len(message) > 0 {
err = ctx.reply(message...)
err = ctx.reply(color, message...)
if err != nil {
return err
}
Expand All @@ -102,21 +106,21 @@ func (ctx *slackContext) replyWithStamp(stamp string, message ...string) error {
}

func (ctx *slackContext) ReplyBad(message ...string) error {
return ctx.replyWithStamp(config.C.Stamps.BadCommand, message...)
return ctx.replyWithStamp(config.C.Stamps.BadCommand, config.C.Slack.Colors.BadCommand, message...)
}

func (ctx *slackContext) ReplyForbid(message ...string) error {
return ctx.replyWithStamp(config.C.Stamps.Forbid, message...)
return ctx.replyWithStamp(config.C.Stamps.Forbid, config.C.Slack.Colors.Forbid, message...)
}

func (ctx *slackContext) ReplySuccess(message ...string) error {
return ctx.replyWithStamp(config.C.Stamps.Success, message...)
return ctx.replyWithStamp(config.C.Stamps.Success, config.C.Slack.Colors.Success, message...)
}

func (ctx *slackContext) ReplyFailure(message ...string) error {
return ctx.replyWithStamp(config.C.Stamps.Failure, message...)
return ctx.replyWithStamp(config.C.Stamps.Failure, config.C.Slack.Colors.Failure, message...)
}

func (ctx *slackContext) ReplyRunning(message ...string) error {
return ctx.replyWithStamp(config.C.Stamps.Running, message...)
return ctx.replyWithStamp(config.C.Stamps.Running, config.C.Slack.Colors.Running, message...)
}
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type SlackConfig struct {
//
// Trusted workflows are allowed to impersonate the execution user via adding user mention at the start of message.
TrustedWorkflows []string `mapstructure:"trustedWorkflows" yaml:"trustedWorkflows"`
// Colors sets colors used for reply blocks.
Colors Stamps `mapstructure:"colors" yaml:"colors"`
}

type Stamps struct {
Expand Down Expand Up @@ -123,6 +125,12 @@ func init() {
viper.SetDefault("slack.channelID", "")
viper.SetDefault("slack.trustedWorkflows", nil)

viper.SetDefault("slack.colors.badCommand", "#dd0204")
viper.SetDefault("slack.colors.forbid", "#dd0204")
viper.SetDefault("slack.colors.success", "#56c59c")
viper.SetDefault("slack.colors.failure", "#dd0204")
viper.SetDefault("slack.colors.running", "#e3e4e6")

viper.SetDefault("prefix", "/")

viper.SetDefault("stamps.badCommand", "")
Expand Down

0 comments on commit 1ae224e

Please sign in to comment.