Skip to content
Snippets Groups Projects

Slack utils

Merged Jano Hendriks requested to merge slack-utils into main
1 unresolved thread
+ 45
32
@@ -51,48 +51,61 @@ func (s *SlackClient) SendAlertWithFields(message string, channel string, slackF
return ""
}
blocks := []slack.Block{slack.NewSectionBlock(slack.NewTextBlockObject(
"plain_text",
message,
hasEmoji(message),
false),
nil,
nil,
),
}
slackFieldsPerMessage := lo.Chunk(slackFields, 50) // Slack has a limit of 50 blocks per message
var messageTimestamp string
for i, slackFieldsForMessage := range slackFieldsPerMessage {
messageText := message
if len(slackFieldsPerMessage) > 1 {
messageText = fmt.Sprintf("%s (%d/%d)", message, i+1, len(slackFieldsPerMessage))
}
slackFieldsChunked := lo.Chunk(slackFields, 2)
blocks := []slack.Block{slack.NewSectionBlock(slack.NewTextBlockObject(
"plain_text",
messageText,
hasEmoji(messageText),
false),
nil,
nil,
),
}
for _, slackFieldsChunk := range slackFieldsChunked {
fieldBlocksForChunk := []*slack.TextBlockObject{}
for _, slackField := range slackFieldsChunk {
slackFieldValue := slackField.Value
// if slackField.Value is a pointer, dereference it
if reflect.ValueOf(slackField.Value).Kind() == reflect.Ptr {
slackFieldValue = reflect.ValueOf(slackField.Value).Elem().Interface()
slackFieldsChunked := lo.Chunk(slackFieldsForMessage, 2)
for _, slackFieldsChunk := range slackFieldsChunked {
fieldBlocksForChunk := []*slack.TextBlockObject{}
for _, slackField := range slackFieldsChunk {
slackFieldValue := slackField.Value
// if slackField.Value is a pointer, dereference it
if reflect.ValueOf(slackField.Value).Kind() == reflect.Ptr {
slackFieldValue = reflect.ValueOf(slackField.Value).Elem().Interface()
}
text := fmt.Sprintf("*%s*\n%v", slackField.Label, slackFieldValue)
fieldBlocksForChunk = append(fieldBlocksForChunk, slack.NewTextBlockObject(
"mrkdwn",
text,
false,
false,
))
}
text := fmt.Sprintf("*%s*\n%v", slackField.Label, slackFieldValue)
fieldBlocksForChunk = append(fieldBlocksForChunk, slack.NewTextBlockObject(
"mrkdwn",
text,
false,
false,
))
blocks = append(blocks, slack.NewSectionBlock(nil, fieldBlocksForChunk, nil))
}
blocks = append(blocks, slack.NewSectionBlock(nil, fieldBlocksForChunk, nil))
}
slackMessageOptions := []slack.MsgOption{
slack.MsgOptionBlocks(blocks...),
}
slackMessageOptions := []slack.MsgOption{
slack.MsgOptionBlocks(blocks...),
}
if len(parentMessageTimestamp) > 0 {
slackMessageOptions = append(slackMessageOptions, slack.MsgOptionTS(parentMessageTimestamp[0]))
}
if len(parentMessageTimestamp) > 0 {
slackMessageOptions = append(slackMessageOptions, slack.MsgOptionTS(parentMessageTimestamp[0]))
messageTimestamp = s.postMessage(channel, slackMessageOptions)
}
return s.postMessage(channel, slackMessageOptions)
// Return the last message timestamp
return messageTimestamp
}
func (s *SlackClient) postMessage(channel string, messageOptions []slack.MsgOption) string {
Loading