diff --git a/pkg/bot/slack/context.go b/pkg/bot/slack/context.go index f21f76c..58205f8 100644 --- a/pkg/bot/slack/context.go +++ b/pkg/bot/slack/context.go @@ -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 { @@ -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 }) } @@ -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 } @@ -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...) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 33d29ac..48c4a69 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 { @@ -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", "")