Skip to content

Commit 61734c3

Browse files
authored
feat!: expose UploadToURL (from uploadToURL) (#1422)
I've also added an example on how to do this. You can find it in [./examples/files/multiple_files](./examples/files/multiple_files). BREAKING CHANGE: renamed `AltText` to `AltTxt` and `SnippetText` to `SnippetType`.
2 parents d0ef804 + edb966b commit 61734c3

File tree

6 files changed

+131
-26
lines changed

6 files changed

+131
-26
lines changed

examples/files/files.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
6+
"os"
57

68
"github.com/slack-go/slack"
79
)
810

911
func main() {
10-
api := slack.New("YOUR_TOKEN_HERE")
11-
params := slack.FileUploadParameters{
12-
Title: "Batman Example",
13-
//Filetype: "txt",
14-
File: "example.txt",
15-
//Content: "Nan Nan Nan Nan Nan Nan Nan Nan Batman",
12+
token, ok := os.LookupEnv("SLACK_BOT_TOKEN")
13+
if !ok {
14+
fmt.Println("Missing SLACK_BOT_TOKEN in environment")
15+
os.Exit(1)
1616
}
17-
file, err := api.UploadFile(params)
17+
api := slack.New(token, slack.OptionDebug(true))
18+
19+
ctx := context.Background()
20+
21+
// Upload a file
22+
params := slack.UploadFileV2Parameters{
23+
Title: "Batman Example",
24+
Filename: "example.txt",
25+
File: "example.txt",
26+
FileSize: 38,
27+
}
28+
file, err := api.UploadFileV2Context(ctx, params)
1829
if err != nil {
1930
fmt.Printf("%s\n", err)
2031
return
2132
}
22-
fmt.Printf("Name: %s, URL: %s\n", file.Name, file.URL)
33+
fmt.Printf("ID: %s, title: %s\n", file.ID, file.Title)
2334

2435
err = api.DeleteFile(file.ID)
2536
if err != nil {
2637
fmt.Printf("%s\n", err)
2738
return
2839
}
29-
fmt.Printf("File %s deleted successfully.\n", file.Name)
40+
fmt.Printf("File %s deleted successfully.\n", file.ID)
3041
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Nan Nan Nan Nan Nan Nan Nan Nan Batman
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/slack-go/slack"
9+
)
10+
11+
func main() {
12+
token, ok := os.LookupEnv("SLACK_BOT_TOKEN")
13+
if !ok {
14+
fmt.Println("Missing SLACK_BOT_TOKEN in environment")
15+
os.Exit(1)
16+
}
17+
channelID := "CXXXXXXXX" // Replace with your channel ID
18+
19+
api := slack.New(token, slack.OptionDebug(true))
20+
ctx := context.Background()
21+
22+
files := []slack.UploadFileV2Parameters{
23+
{
24+
Title: "Batman Example",
25+
Filename: "batman.txt",
26+
File: "batman.txt",
27+
FileSize: 39,
28+
},
29+
{
30+
Title: "Superman Example",
31+
Filename: "superman.txt",
32+
File: "superman.txt",
33+
FileSize: 37,
34+
},
35+
}
36+
37+
uploads := []*slack.GetUploadURLExternalResponse{}
38+
filesToComplete := []slack.FileSummary{}
39+
40+
for _, file := range files {
41+
u, err := api.GetUploadURLExternalContext(ctx, slack.GetUploadURLExternalParameters{
42+
AltTxt: "An alt text for superheroes",
43+
FileName: file.Filename,
44+
FileSize: file.FileSize,
45+
})
46+
if err != nil {
47+
fmt.Printf("%s\n", err)
48+
return
49+
}
50+
uploads = append(uploads, u)
51+
}
52+
53+
for i, file := range files {
54+
fmt.Printf("Uploading file %s to %s\n", file.Filename, uploads[i].UploadURL)
55+
56+
err := api.UploadToURL(ctx, slack.UploadToURLParameters{
57+
UploadURL: uploads[i].UploadURL,
58+
Filename: file.Filename,
59+
File: file.File,
60+
})
61+
if err != nil {
62+
fmt.Printf("%s\n", err)
63+
return
64+
}
65+
filesToComplete = append(filesToComplete, slack.FileSummary{
66+
ID: uploads[i].FileID,
67+
Title: file.Title,
68+
})
69+
}
70+
71+
c, err := api.CompleteUploadExternalContext(ctx, slack.CompleteUploadExternalParameters{
72+
Files: filesToComplete,
73+
Channel: channelID,
74+
})
75+
76+
if err != nil {
77+
fmt.Printf("%s\n", err)
78+
return
79+
}
80+
81+
fmt.Printf("Files uploaded successfully: %+v\n", c.Files)
82+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Whoosh Whoosh Whoosh Whoosh Superman

files.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ type UploadFileV2Parameters struct {
178178
Channel string
179179
ThreadTimestamp string
180180
AltTxt string
181-
SnippetText string
181+
SnippetType string
182182
}
183183

184184
type GetUploadURLExternalParameters struct {
185-
AltText string
185+
AltTxt string
186186
FileSize int
187187
FileName string
188-
SnippetText string
188+
SnippetType string
189189
}
190190

191191
type GetUploadURLExternalResponse struct {
@@ -194,7 +194,7 @@ type GetUploadURLExternalResponse struct {
194194
SlackResponse
195195
}
196196

197-
type uploadToURLParameters struct {
197+
type UploadToURLParameters struct {
198198
UploadURL string
199199
Reader io.Reader
200200
File string
@@ -375,15 +375,23 @@ func (api *Client) ListFilesContext(ctx context.Context, params ListFilesParamet
375375

376376
// UploadFile uploads a file.
377377
//
378-
// Deprecated: Use [Client.UploadFileV2] instead. This will stop functioning on March 11, 2025.
378+
// Deprecated: Use [Client.UploadFileV2] instead.
379+
//
380+
// Per Slack Changelog, specifically [https://api.slack.com/changelog#entry-march_2025_1](this entry),
381+
// this will stop functioning on November 12, 2025.
382+
//
379383
// For more details, see: https://api.slack.com/methods/files.upload#markdown
380384
func (api *Client) UploadFile(params FileUploadParameters) (file *File, err error) {
381385
return api.UploadFileContext(context.Background(), params)
382386
}
383387

384388
// UploadFileContext uploads a file and setting a custom context.
385389
//
386-
// Deprecated: Use [Client.UploadFileV2Context] instead. This will stop functioning on March 11, 2025.
390+
// Deprecated: Use [Client.UploadFileV2Context] instead.
391+
//
392+
// Per Slack Changelog, specifically [https://api.slack.com/changelog#entry-march_2025_1](this entry),
393+
// this will stop functioning on November 12, 2025.
394+
//
387395
// For more details, see: https://api.slack.com/methods/files.upload#markdown
388396
func (api *Client) UploadFileContext(ctx context.Context, params FileUploadParameters) (file *File, err error) {
389397
// Test if user token is valid. This helps because client.Do doesn't like this for some reason. XXX: More
@@ -529,11 +537,11 @@ func (api *Client) GetUploadURLExternalContext(ctx context.Context, params GetUp
529537
"filename": {params.FileName},
530538
"length": {strconv.Itoa(params.FileSize)},
531539
}
532-
if params.AltText != "" {
533-
values.Add("initial_comment", params.AltText)
540+
if params.AltTxt != "" {
541+
values.Add("alt_txt", params.AltTxt)
534542
}
535-
if params.SnippetText != "" {
536-
values.Add("thread_ts", params.SnippetText)
543+
if params.SnippetType != "" {
544+
values.Add("snippet_type", params.SnippetType)
537545
}
538546
response := &GetUploadURLExternalResponse{}
539547
err := api.postMethod(ctx, "files.getUploadURLExternal", values, response)
@@ -544,8 +552,9 @@ func (api *Client) GetUploadURLExternalContext(ctx context.Context, params GetUp
544552
return response, response.Err()
545553
}
546554

547-
// uploadToURL uploads the file to the provided URL using post method
548-
func (api *Client) uploadToURL(ctx context.Context, params uploadToURLParameters) (err error) {
555+
// UploadToURL uploads the file to the provided URL using post method
556+
// This is not a Slack API method, but a helper function to upload files to the URL
557+
func (api *Client) UploadToURL(ctx context.Context, params UploadToURLParameters) (err error) {
549558
values := url.Values{}
550559
if params.Content != "" {
551560
contentReader := strings.NewReader(params.Content)
@@ -565,6 +574,7 @@ func (api *Client) CompleteUploadExternalContext(ctx context.Context, params Com
565574
if err != nil {
566575
return nil, err
567576
}
577+
568578
values := url.Values{
569579
"token": {api.token},
570580
"files": {string(filesBytes)},
@@ -611,16 +621,16 @@ func (api *Client) UploadFileV2Context(ctx context.Context, params UploadFileV2P
611621
}
612622

613623
u, err := api.GetUploadURLExternalContext(ctx, GetUploadURLExternalParameters{
614-
AltText: params.AltTxt,
624+
AltTxt: params.AltTxt,
615625
FileName: params.Filename,
616626
FileSize: params.FileSize,
617-
SnippetText: params.SnippetText,
627+
SnippetType: params.SnippetType,
618628
})
619629
if err != nil {
620630
return nil, err
621631
}
622632

623-
err = api.uploadToURL(ctx, uploadToURLParameters{
633+
err = api.UploadToURL(ctx, UploadToURLParameters{
624634
UploadURL: u.UploadURL,
625635
Reader: params.Reader,
626636
File: params.File,

files_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ func TestGetUploadURLExternalContext(t *testing.T) {
330330
params: GetUploadURLExternalParameters{
331331
FileSize: 10,
332332
FileName: "test.txt",
333-
AltText: "test-alt-text",
334-
SnippetText: "test-snippet-text",
333+
AltTxt: "test-alt-text",
334+
SnippetType: "test-snippet-type",
335335
},
336336
wantSlackResponse: []byte(`{"ok":true,"file_id":"RandomID","upload_url":"http://test-server/abc"}`),
337337
wantResponse: GetUploadURLExternalResponse{

0 commit comments

Comments
 (0)