| 1 | package slack |
| 2 | |
| 3 | // @NOTE: Blocks are in beta and subject to change. |
| 4 | |
| 5 | // More Information: https://api.slack.com/block-kit |
| 6 | |
| 7 | // MessageBlockType defines a named string type to define each block type |
| 8 | // as a constant for use within the package. |
| 9 | type MessageBlockType string |
| 10 | |
| 11 | const ( |
| 12 | MBTSection MessageBlockType = "section" |
| 13 | MBTDivider MessageBlockType = "divider" |
| 14 | MBTImage MessageBlockType = "image" |
| 15 | MBTAction MessageBlockType = "actions" |
| 16 | MBTContext MessageBlockType = "context" |
| 17 | MBTFile MessageBlockType = "file" |
| 18 | MBTInput MessageBlockType = "input" |
| 19 | MBTHeader MessageBlockType = "header" |
| 20 | MBTRichText MessageBlockType = "rich_text" |
| 21 | ) |
| 22 | |
| 23 | // Block defines an interface all block types should implement |
| 24 | // to ensure consistency between blocks. |
| 25 | type Block interface { |
| 26 | BlockType() MessageBlockType |
| 27 | } |
| 28 | |
| 29 | // Blocks is a convenience struct defined to allow dynamic unmarshalling of |
| 30 | // the "blocks" value in Slack's JSON response, which varies depending on block type |
| 31 | type Blocks struct { |
| 32 | BlockSet []Block `json:"blocks,omitempty"` |
| 33 | } |
| 34 | |
| 35 | // BlockAction is the action callback sent when a block is interacted with |
| 36 | type BlockAction struct { |
| 37 | ActionID string `json:"action_id"` |
| 38 | BlockID string `json:"block_id"` |
| 39 | Type ActionType `json:"type"` |
| 40 | Text TextBlockObject `json:"text"` |
| 41 | Value string `json:"value"` |
| 42 | ActionTs string `json:"action_ts"` |
| 43 | SelectedOption OptionBlockObject `json:"selected_option"` |
| 44 | SelectedOptions []OptionBlockObject `json:"selected_options"` |
| 45 | SelectedUser string `json:"selected_user"` |
| 46 | SelectedUsers []string `json:"selected_users"` |
| 47 | SelectedChannel string `json:"selected_channel"` |
| 48 | SelectedChannels []string `json:"selected_channels"` |
| 49 | SelectedConversation string `json:"selected_conversation"` |
| 50 | SelectedConversations []string `json:"selected_conversations"` |
| 51 | SelectedDate string `json:"selected_date"` |
| 52 | SelectedTime string `json:"selected_time"` |
| 53 | InitialOption OptionBlockObject `json:"initial_option"` |
| 54 | InitialUser string `json:"initial_user"` |
| 55 | InitialChannel string `json:"initial_channel"` |
| 56 | InitialConversation string `json:"initial_conversation"` |
| 57 | InitialDate string `json:"initial_date"` |
| 58 | InitialTime string `json:"initial_time"` |
| 59 | } |
| 60 | |
| 61 | // actionType returns the type of the action |
| 62 | func (b BlockAction) actionType() ActionType { |
| 63 | return b.Type |
| 64 | } |
| 65 | |
| 66 | // NewBlockMessage creates a new Message that contains one or more blocks to be displayed |
| 67 | func NewBlockMessage(blocks ...Block) Message { |
| 68 | return Message{ |
| 69 | Msg: Msg{ |
| 70 | Blocks: Blocks{ |
| 71 | BlockSet: blocks, |
| 72 | }, |
| 73 | }, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // AddBlockMessage appends a block to the end of the existing list of blocks |
| 78 | func AddBlockMessage(message Message, newBlk Block) Message { |
| 79 | message.Msg.Blocks.BlockSet = append(message.Msg.Blocks.BlockSet, newBlk) |
| 80 | return message |
| 81 | } |