forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock.go
65 lines (55 loc) · 1.85 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 (
mbtSection MessageBlockType = "section"
mbtDivider MessageBlockType = "divider"
mbtImage MessageBlockType = "image"
mbtAction MessageBlockType = "actions"
mbtContext MessageBlockType = "context"
)
// 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 {
ActionBlocks []*ActionBlock
ContextBlocks []*ContextBlock
DividerBlocks []*DividerBlock
ImageBlocks []*ImageBlock
SectionBlocks []*SectionBlock
}
// BlockAction is the action callback sent when a block is interacted with
type BlockAction struct {
ActionID string `json:"action_id"`
BlockID string `json:"block_id"`
Text TextBlockObject `json:"text"`
Value string `json:"value"`
Type actionType `json:"type"`
ActionTs string `json:"action_ts"`
}
// actionType returns the type of the block action
func (b BlockAction) actionType() actionType {
return b.Type
}
// NewBlockMessage creates a new Message that contains one or more blocks to be displayed
func NewBlockMessage(blocks ...Block) Message {
b := Blocks{}
b.appendToBlocks(blocks)
return Message{
Msg: Msg{
Blocks: b,
},
}
}
// AddBlockMessage appends a block to the end of the existing list of blocks
func AddBlockMessage(message Message, newBlk Block) Message {
message.Msg.Blocks.appendToBlocks([]Block{newBlk})
return message
}