Skip to content

Commit 1ae224e

Browse files
committed
feat(slack): use attachments for easier view
1 parent 92576ac commit 1ae224e

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

pkg/bot/slack/context.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func (ctx *slackContext) L() *zap.Logger {
4343
}
4444

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

5049
func (ctx *slackContext) StampNames() *domain.StampNames {
@@ -57,21 +56,26 @@ func (ctx *slackContext) StampNames() *domain.StampNames {
5756
}
5857
}
5958

60-
func (ctx *slackContext) sendSlackMessage(channelID string, text string) error {
59+
func (ctx *slackContext) sendSlackMessage(channelID string, lines []string, color string) error {
6160
api := ctx.api
6261
return utils.WithRetry(ctx, 10, func(ctx context.Context) error {
63-
_, _, err := api.PostMessage(channelID, slack.MsgOptionBlocks(
64-
slack.NewSectionBlock(
65-
slack.NewTextBlockObject(
66-
slack.MarkdownType,
67-
text,
68-
false,
69-
false,
70-
),
71-
nil,
72-
nil,
73-
),
74-
))
62+
var options []slack.MsgOption
63+
options = append(options, slack.MsgOptionText(lines[0], false))
64+
if len(lines) >= 2 {
65+
options = append(options, slack.MsgOptionAttachments(
66+
slack.Attachment{
67+
Color: color,
68+
Fields: []slack.AttachmentField{
69+
{
70+
Title: "",
71+
Value: strings.Join(lines[1:], "\n"),
72+
Short: false,
73+
},
74+
},
75+
},
76+
))
77+
}
78+
_, _, err := api.PostMessage(channelID, options...)
7579
return err
7680
})
7781
}
@@ -83,17 +87,17 @@ func (ctx *slackContext) pushSlackReaction(message slack.ItemRef, stampID string
8387
})
8488
}
8589

86-
func (ctx *slackContext) reply(message ...string) error {
87-
return ctx.sendSlackMessage(ctx.message.Channel, strings.Join(message, "\n"))
90+
func (ctx *slackContext) reply(color string, message ...string) error {
91+
return ctx.sendSlackMessage(ctx.message.Channel, message, color)
8892
}
8993

90-
func (ctx *slackContext) replyWithStamp(stamp string, message ...string) error {
94+
func (ctx *slackContext) replyWithStamp(stamp string, color string, message ...string) error {
9195
err := ctx.pushSlackReaction(ctx.message, stamp)
9296
if err != nil {
9397
return err
9498
}
9599
if len(message) > 0 {
96-
err = ctx.reply(message...)
100+
err = ctx.reply(color, message...)
97101
if err != nil {
98102
return err
99103
}
@@ -102,21 +106,21 @@ func (ctx *slackContext) replyWithStamp(stamp string, message ...string) error {
102106
}
103107

104108
func (ctx *slackContext) ReplyBad(message ...string) error {
105-
return ctx.replyWithStamp(config.C.Stamps.BadCommand, message...)
109+
return ctx.replyWithStamp(config.C.Stamps.BadCommand, config.C.Slack.Colors.BadCommand, message...)
106110
}
107111

108112
func (ctx *slackContext) ReplyForbid(message ...string) error {
109-
return ctx.replyWithStamp(config.C.Stamps.Forbid, message...)
113+
return ctx.replyWithStamp(config.C.Stamps.Forbid, config.C.Slack.Colors.Forbid, message...)
110114
}
111115

112116
func (ctx *slackContext) ReplySuccess(message ...string) error {
113-
return ctx.replyWithStamp(config.C.Stamps.Success, message...)
117+
return ctx.replyWithStamp(config.C.Stamps.Success, config.C.Slack.Colors.Success, message...)
114118
}
115119

116120
func (ctx *slackContext) ReplyFailure(message ...string) error {
117-
return ctx.replyWithStamp(config.C.Stamps.Failure, message...)
121+
return ctx.replyWithStamp(config.C.Stamps.Failure, config.C.Slack.Colors.Failure, message...)
118122
}
119123

120124
func (ctx *slackContext) ReplyRunning(message ...string) error {
121-
return ctx.replyWithStamp(config.C.Stamps.Running, message...)
125+
return ctx.replyWithStamp(config.C.Stamps.Running, config.C.Slack.Colors.Running, message...)
122126
}

pkg/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type SlackConfig struct {
5252
//
5353
// Trusted workflows are allowed to impersonate the execution user via adding user mention at the start of message.
5454
TrustedWorkflows []string `mapstructure:"trustedWorkflows" yaml:"trustedWorkflows"`
55+
// Colors sets colors used for reply blocks.
56+
Colors Stamps `mapstructure:"colors" yaml:"colors"`
5557
}
5658

5759
type Stamps struct {
@@ -123,6 +125,12 @@ func init() {
123125
viper.SetDefault("slack.channelID", "")
124126
viper.SetDefault("slack.trustedWorkflows", nil)
125127

128+
viper.SetDefault("slack.colors.badCommand", "#dd0204")
129+
viper.SetDefault("slack.colors.forbid", "#dd0204")
130+
viper.SetDefault("slack.colors.success", "#56c59c")
131+
viper.SetDefault("slack.colors.failure", "#dd0204")
132+
viper.SetDefault("slack.colors.running", "#e3e4e6")
133+
126134
viper.SetDefault("prefix", "/")
127135

128136
viper.SetDefault("stamps.badCommand", "")

0 commit comments

Comments
 (0)