2019-09-07 20:46:58 +00:00
|
|
|
package slack
|
|
|
|
|
|
|
|
// @NOTE: Blocks are in beta and subject to change.
|
|
|
|
|
|
|
|
// More Information: https://api.slack.com/block-kit
|
|
|
|
|
|
|
|
// MessageBlockType defines a named string type to define each block type
|
|
|
|
// as a constant for use within the package.
|
|
|
|
type MessageBlockType string
|
|
|
|
|
|
|
|
const (
|
2021-12-11 23:05:15 +00:00
|
|
|
MBTSection MessageBlockType = "section"
|
|
|
|
MBTDivider MessageBlockType = "divider"
|
|
|
|
MBTImage MessageBlockType = "image"
|
|
|
|
MBTAction MessageBlockType = "actions"
|
|
|
|
MBTContext MessageBlockType = "context"
|
|
|
|
MBTFile MessageBlockType = "file"
|
|
|
|
MBTInput MessageBlockType = "input"
|
|
|
|
MBTHeader MessageBlockType = "header"
|
|
|
|
MBTRichText MessageBlockType = "rich_text"
|
2024-05-23 21:44:31 +00:00
|
|
|
MBTVideo MessageBlockType = "video"
|
2019-09-07 20:46:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Block defines an interface all block types should implement
|
|
|
|
// to ensure consistency between blocks.
|
|
|
|
type Block interface {
|
|
|
|
BlockType() MessageBlockType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocks is a convenience struct defined to allow dynamic unmarshalling of
|
|
|
|
// the "blocks" value in Slack's JSON response, which varies depending on block type
|
|
|
|
type Blocks struct {
|
|
|
|
BlockSet []Block `json:"blocks,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockAction is the action callback sent when a block is interacted with
|
|
|
|
type BlockAction struct {
|
2020-07-18 15:27:41 +00:00
|
|
|
ActionID string `json:"action_id"`
|
|
|
|
BlockID string `json:"block_id"`
|
2021-05-05 20:03:28 +00:00
|
|
|
Type ActionType `json:"type"`
|
2020-07-18 15:27:41 +00:00
|
|
|
Text TextBlockObject `json:"text"`
|
|
|
|
Value string `json:"value"`
|
2024-05-23 21:44:31 +00:00
|
|
|
Files []File `json:"files"`
|
2020-07-18 15:27:41 +00:00
|
|
|
ActionTs string `json:"action_ts"`
|
|
|
|
SelectedOption OptionBlockObject `json:"selected_option"`
|
|
|
|
SelectedOptions []OptionBlockObject `json:"selected_options"`
|
|
|
|
SelectedUser string `json:"selected_user"`
|
|
|
|
SelectedUsers []string `json:"selected_users"`
|
|
|
|
SelectedChannel string `json:"selected_channel"`
|
|
|
|
SelectedChannels []string `json:"selected_channels"`
|
|
|
|
SelectedConversation string `json:"selected_conversation"`
|
|
|
|
SelectedConversations []string `json:"selected_conversations"`
|
|
|
|
SelectedDate string `json:"selected_date"`
|
2021-02-01 20:29:04 +00:00
|
|
|
SelectedTime string `json:"selected_time"`
|
2023-08-05 18:43:19 +00:00
|
|
|
SelectedDateTime int64 `json:"selected_date_time"`
|
2020-07-18 15:27:41 +00:00
|
|
|
InitialOption OptionBlockObject `json:"initial_option"`
|
|
|
|
InitialUser string `json:"initial_user"`
|
|
|
|
InitialChannel string `json:"initial_channel"`
|
|
|
|
InitialConversation string `json:"initial_conversation"`
|
|
|
|
InitialDate string `json:"initial_date"`
|
2021-02-01 20:29:04 +00:00
|
|
|
InitialTime string `json:"initial_time"`
|
2019-09-07 20:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// actionType returns the type of the action
|
2021-05-05 20:03:28 +00:00
|
|
|
func (b BlockAction) actionType() ActionType {
|
2019-09-07 20:46:58 +00:00
|
|
|
return b.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlockMessage creates a new Message that contains one or more blocks to be displayed
|
|
|
|
func NewBlockMessage(blocks ...Block) Message {
|
|
|
|
return Message{
|
|
|
|
Msg: Msg{
|
|
|
|
Blocks: Blocks{
|
|
|
|
BlockSet: blocks,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBlockMessage appends a block to the end of the existing list of blocks
|
|
|
|
func AddBlockMessage(message Message, newBlk Block) Message {
|
|
|
|
message.Msg.Blocks.BlockSet = append(message.Msg.Blocks.BlockSet, newBlk)
|
|
|
|
return message
|
|
|
|
}
|