Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type PostMessageParameters struct {

// chat metadata support
MetaData SlackMetadata `json:"metadata"`

// file_ids support
FileIDs []string `json:"file_ids,omitempty"`
}

// NewPostMessageParameters provides an instance of PostMessageParameters with all the sane default values set
Expand Down Expand Up @@ -715,6 +718,23 @@ func MsgOptionLinkNames(linkName bool) MsgOption {
}
}

// MsgOptionFileIDs sets file IDs for the message
func MsgOptionFileIDs(fileIDs []string) MsgOption {
return func(config *sendConfig) error {
if len(fileIDs) == 0 {
return nil
}

fileIDsBytes, err := json.Marshal(fileIDs)
if err != nil {
return err
}

config.values.Set("file_ids", string(fileIDsBytes))
return nil
}
}

// UnsafeMsgOptionEndpoint deliver the message to the specified endpoint.
// NOTE: USE AT YOUR OWN RISK: No issues relating to the use of this Option
// will be supported by the library, it is subject to change without notice that
Expand Down Expand Up @@ -778,6 +798,10 @@ func MsgOptionPostMessageParameters(params PostMessageParameters) MsgOption {
config.values.Set("reply_broadcast", "true")
}

if len(params.FileIDs) > 0 {
return MsgOptionFileIDs(params.FileIDs)(config)
}

return nil
}
}
Expand Down
58 changes: 58 additions & 0 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,61 @@ func TestSendMessageContextRedactsTokenInDebugLog(t *testing.T) {
})
}
}

func TestUpdateMessage(t *testing.T) {
type messageTest struct {
endpoint string
opt []MsgOption
expected url.Values
}
tests := map[string]messageTest{
"empty file_ids": {
endpoint: "/chat.update",
opt: []MsgOption{},
expected: url.Values{
"channel": []string{"CXXX"},
"token": []string{"testing-token"},
"ts": []string{"1234567890.123456"},
},
},
"with file_ids": {
endpoint: "/chat.update",
opt: []MsgOption{
MsgOptionFileIDs([]string{"F123", "F456"}),
},
expected: url.Values{
"channel": []string{"CXXX"},
"token": []string{"testing-token"},
"ts": []string{"1234567890.123456"},
"file_ids": []string{`["F123","F456"]`},
},
},
}

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

for name, test := range tests {
t.Run(name, func(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc(test.endpoint, func(rw http.ResponseWriter, r *http.Request) {
body, err := io.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.UpdateMessage("CXXX", "1234567890.123456", test.opt...)
})
}
}