Skip to content

Commit eef4b6c

Browse files
authored
feat: add ability to add file ids when updating a message (#1451)
Closes #1450.
2 parents fc0ba6c + 91d3258 commit eef4b6c

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

chat.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type PostMessageParameters struct {
6868

6969
// chat metadata support
7070
MetaData SlackMetadata `json:"metadata"`
71+
72+
// file_ids support
73+
FileIDs []string `json:"file_ids,omitempty"`
7174
}
7275

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

721+
// MsgOptionFileIDs sets file IDs for the message
722+
func MsgOptionFileIDs(fileIDs []string) MsgOption {
723+
return func(config *sendConfig) error {
724+
if len(fileIDs) == 0 {
725+
return nil
726+
}
727+
728+
fileIDsBytes, err := json.Marshal(fileIDs)
729+
if err != nil {
730+
return err
731+
}
732+
733+
config.values.Set("file_ids", string(fileIDsBytes))
734+
return nil
735+
}
736+
}
737+
718738
// UnsafeMsgOptionEndpoint deliver the message to the specified endpoint.
719739
// NOTE: USE AT YOUR OWN RISK: No issues relating to the use of this Option
720740
// will be supported by the library, it is subject to change without notice that
@@ -778,6 +798,10 @@ func MsgOptionPostMessageParameters(params PostMessageParameters) MsgOption {
778798
config.values.Set("reply_broadcast", "true")
779799
}
780800

801+
if len(params.FileIDs) > 0 {
802+
return MsgOptionFileIDs(params.FileIDs)(config)
803+
}
804+
781805
return nil
782806
}
783807
}

chat_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,61 @@ func TestSendMessageContextRedactsTokenInDebugLog(t *testing.T) {
367367
})
368368
}
369369
}
370+
371+
func TestUpdateMessage(t *testing.T) {
372+
type messageTest struct {
373+
endpoint string
374+
opt []MsgOption
375+
expected url.Values
376+
}
377+
tests := map[string]messageTest{
378+
"empty file_ids": {
379+
endpoint: "/chat.update",
380+
opt: []MsgOption{},
381+
expected: url.Values{
382+
"channel": []string{"CXXX"},
383+
"token": []string{"testing-token"},
384+
"ts": []string{"1234567890.123456"},
385+
},
386+
},
387+
"with file_ids": {
388+
endpoint: "/chat.update",
389+
opt: []MsgOption{
390+
MsgOptionFileIDs([]string{"F123", "F456"}),
391+
},
392+
expected: url.Values{
393+
"channel": []string{"CXXX"},
394+
"token": []string{"testing-token"},
395+
"ts": []string{"1234567890.123456"},
396+
"file_ids": []string{`["F123","F456"]`},
397+
},
398+
},
399+
}
400+
401+
once.Do(startServer)
402+
api := New(validToken, OptionAPIURL("http://"+serverAddr+"/"))
403+
404+
for name, test := range tests {
405+
t.Run(name, func(t *testing.T) {
406+
http.DefaultServeMux = new(http.ServeMux)
407+
http.HandleFunc(test.endpoint, func(rw http.ResponseWriter, r *http.Request) {
408+
body, err := io.ReadAll(r.Body)
409+
if err != nil {
410+
t.Errorf("unexpected error: %v", err)
411+
return
412+
}
413+
actual, err := url.ParseQuery(string(body))
414+
if err != nil {
415+
t.Errorf("unexpected error: %v", err)
416+
return
417+
}
418+
if !reflect.DeepEqual(actual, test.expected) {
419+
t.Errorf("\nexpected: %s\n actual: %s", test.expected, actual)
420+
return
421+
}
422+
})
423+
424+
_, _, _, _ = api.UpdateMessage("CXXX", "1234567890.123456", test.opt...)
425+
})
426+
}
427+
}

0 commit comments

Comments
 (0)