github.com/blend/go-sdk@v1.20220411.3/slack/message_option.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package slack 9 10 // ApplyMessageOptions applies a set of options against a message and returns the mutated copy. 11 func ApplyMessageOptions(m Message, options ...MessageOption) Message { 12 for _, option := range options { 13 option(&m) 14 } 15 return m 16 } 17 18 // MessageOption is a mutator for messages. 19 type MessageOption func(m *Message) 20 21 // OptMessageChannel sets the channel. 22 func OptMessageChannel(channel string) MessageOption { 23 return func(m *Message) { 24 m.Channel = channel 25 } 26 } 27 28 // OptMessageChannelOrDefault sets the channel if its unset. 29 func OptMessageChannelOrDefault(channel string) MessageOption { 30 return func(m *Message) { 31 if len(m.Channel) == 0 { 32 m.Channel = channel 33 } 34 } 35 } 36 37 // OptMessageResponseType sets the response type. 38 func OptMessageResponseType(responseType string) MessageOption { 39 return func(m *Message) { 40 m.ResponseType = responseType 41 } 42 } 43 44 // OptMessageUsername sets the username. 45 func OptMessageUsername(username string) MessageOption { 46 return func(m *Message) { 47 m.Username = username 48 } 49 } 50 51 // OptMessageUsernameOrDefault sets the username. 52 func OptMessageUsernameOrDefault(username string) MessageOption { 53 return func(m *Message) { 54 if len(m.Username) == 0 { 55 m.Username = username 56 } 57 } 58 } 59 60 // OptMessageIconEmoji sets the icon emoji. 61 func OptMessageIconEmoji(emoji string) MessageOption { 62 return func(m *Message) { 63 m.IconEmoji = emoji 64 } 65 } 66 67 // OptMessageIconEmojiOrDefault sets the icon emoji. 68 func OptMessageIconEmojiOrDefault(emoji string) MessageOption { 69 return func(m *Message) { 70 if len(m.IconEmoji) == 0 { 71 m.IconEmoji = emoji 72 } 73 } 74 } 75 76 // OptMessageIconURL sets the icon url. 77 func OptMessageIconURL(url string) MessageOption { 78 return func(m *Message) { 79 m.IconURL = url 80 } 81 } 82 83 // OptMessageIconURLOrDefault sets the icon url. 84 func OptMessageIconURLOrDefault(url string) MessageOption { 85 return func(m *Message) { 86 if len(m.IconURL) == 0 { 87 m.IconURL = url 88 } 89 } 90 } 91 92 // OptMessageAttachment adds a message attachment. 93 func OptMessageAttachment(attachment MessageAttachment) MessageOption { 94 return func(m *Message) { 95 m.Attachments = append(m.Attachments, attachment) 96 } 97 }