Skip to content

Commit

Permalink
add blocks attribute to attachments, too
Browse files Browse the repository at this point in the history
blocks can be embedded within attachments, to work around the lack of
colorbar support in blocks, or as a migration path to blocks

- https://api.slack.com/messaging/attachments-to-blocks#finding_equivalents_for_older_visual_elements
- https://api.slack.com/reference/messaging/attachments#fields

This also extends the existing postMessage test to include an attachment
with a block
  • Loading branch information
novas0x2a authored and james-lawrence committed Nov 2, 2019
1 parent eb42091 commit 2613fda
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
2 changes: 2 additions & 0 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type Attachment struct {
Actions []AttachmentAction `json:"actions,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`

Blocks []Block `json:"blocks,omitempty"`

Footer string `json:"footer,omitempty"`
FooterIcon string `json:"footer_icon,omitempty"`

Expand Down
83 changes: 60 additions & 23 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,70 @@ func TestGetPermalink(t *testing.T) {
}
}

func TestPostMessageWithBlocks(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/chat.postMessage", func(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
form, err := url.ParseQuery(string(body))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
actualBlocks := form.Get("blocks")
expectedBlocks := `[{"type":"context","block_id":"context","elements":[{"type":"plain_text","text":"hello"}]}]`
if expectedBlocks != actualBlocks {
t.Errorf("expected: %s, got: %s", expectedBlocks, actualBlocks)
return
}
})
func TestPostMessage(t *testing.T) {
type messageTest struct {
opt []MsgOption
expected url.Values
}

blocks := []Block{NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", false, false))}
blockStr := `[{"type":"context","block_id":"context","elements":[{"type":"plain_text","text":"hello"}]}]`

tests := map[string]messageTest{
"Blocks": {
opt: []MsgOption{
MsgOptionBlocks(blocks...),
MsgOptionText("text", false),
},
expected: url.Values{
"blocks": []string{blockStr},
"channel": []string{"CXXX"},
"text": []string{"text"},
"token": []string{"testing-token"},
},
},
"Attachment": {
opt: []MsgOption{
MsgOptionAttachments(
Attachment{
Text: "text",
Blocks: blocks,
}),
},
expected: url.Values{
"attachments": []string{`[{"fallback":"","text":"text","blocks":` + blockStr + `}]`},
"channel": []string{"CXXX"},
"token": []string{"testing-token"},
},
},
}

once.Do(startServer)
api := New(validToken, OptionAPIURL("http://"+serverAddr+"/"))

blocks := []Block{NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", false, false))}

_, _, _ = api.PostMessage("CXXX", MsgOptionBlocks(blocks...), MsgOptionText("text", false))
for name, test := range tests {
t.Run(name, func(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/chat.postMessage", func(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
actual, err := url.ParseQuery(string(body))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if !reflect.DeepEqual(actual, test.expected) {
t.Errorf("\nexpected: %s\n actual: %s", test.expected, actual)
return
}
})

_, _, _ = api.PostMessage("CXXX", test.opt...)
})
}
}

func TestPostMessageWithBlocksWhenMsgOptionResponseURLApplied(t *testing.T) {
Expand Down

0 comments on commit 2613fda

Please sign in to comment.