Skip to content

Commit 58af258

Browse files
isaacrowntreeclaude
andcommitted
feat: add file upload command with multi-file and thread support
Adds `slackbuzz file upload` supporting single/multi-file uploads to channels, DMs, and threads via the Slack V2 upload API. Also adds files:read and files:write to both bot and user token scopes in the app manifest. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 39f9d31 commit 58af258

3 files changed

Lines changed: 200 additions & 2 deletions

File tree

pkg/cmd/app/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ var appManifest = map[string]interface{}{
7474
"channels:history",
7575
"channels:read",
7676
"emoji:read",
77+
"files:read",
78+
"files:write",
7779
"groups:history",
7880
"groups:read",
7981
"im:history",
@@ -88,6 +90,8 @@ var appManifest = map[string]interface{}{
8890
"user": []string{
8991
"channels:read",
9092
"chat:write",
93+
"files:read",
94+
"files:write",
9195
"groups:read",
9296
"im:read",
9397
"im:write",

pkg/cmd/file/file.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
func NewCmdFile(f *cmdutil.Factory) *cobra.Command {
1010
cmd := &cobra.Command{
1111
Use: "file <command>",
12-
Short: "Search and manage files",
13-
Long: "Search files shared in Slack.",
12+
Short: "Search, upload, and manage files",
13+
Long: "Search files shared in Slack, or upload files to channels and DMs.",
1414
}
1515

1616
cmd.AddCommand(NewCmdSearch(f))
17+
cmd.AddCommand(NewCmdUpload(f))
1718

1819
return cmd
1920
}

pkg/cmd/file/upload.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package file
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/slack-go/slack"
9+
"github.com/spf13/cobra"
10+
"github.com/triptechtravel/slackbuzz-cli/internal/api"
11+
"github.com/triptechtravel/slackbuzz-cli/pkg/cmdutil"
12+
)
13+
14+
type uploadOptions struct {
15+
factory *cmdutil.Factory
16+
filePaths []string
17+
channel string
18+
title string
19+
comment string
20+
threadTS string
21+
json cmdutil.JSONFlags
22+
}
23+
24+
// NewCmdUpload returns the "file upload" command.
25+
func NewCmdUpload(f *cmdutil.Factory) *cobra.Command {
26+
opts := &uploadOptions{factory: f}
27+
28+
cmd := &cobra.Command{
29+
Use: "upload <file-path>... <channel|user>",
30+
Short: "Upload files to a channel or DM",
31+
Long: `Upload one or more files to a Slack channel or direct message.
32+
33+
The last argument is the channel or user target. All preceding arguments are
34+
file paths to upload. Each file is uploaded individually and shared to the
35+
target channel.
36+
37+
The channel argument accepts #channel-name, channel ID, @username, or user ID.`,
38+
Example: ` # Upload a single file to a channel
39+
slackbuzz file upload report.pdf #general
40+
41+
# Upload multiple files
42+
slackbuzz file upload chart1.png chart2.png chart3.png #analytics
43+
44+
# Upload with title and comment
45+
slackbuzz file upload data.csv #sales --title "Q4 Revenue" --comment "Updated data"
46+
47+
# Upload to a DM
48+
slackbuzz file upload notes.txt @alice
49+
50+
# Upload to a thread
51+
slackbuzz file upload results.png #dev --thread-ts 1706000000.000000`,
52+
Args: cobra.MinimumNArgs(2),
53+
PersistentPreRunE: cmdutil.NeedsAuth(f),
54+
RunE: func(cmd *cobra.Command, args []string) error {
55+
// Last arg is channel/user, rest are file paths
56+
opts.channel = args[len(args)-1]
57+
opts.filePaths = args[:len(args)-1]
58+
return uploadRun(opts)
59+
},
60+
}
61+
62+
cmd.Flags().StringVar(&opts.title, "title", "", "File title in Slack (applies to first file)")
63+
cmd.Flags().StringVar(&opts.comment, "comment", "", "Initial comment posted with the file(s)")
64+
cmd.Flags().StringVar(&opts.threadTS, "thread-ts", "", "Thread timestamp to upload into")
65+
cmdutil.AddJSONFlags(cmd, &opts.json)
66+
67+
return cmd
68+
}
69+
70+
type uploadResult struct {
71+
FileID string `json:"file_id"`
72+
Title string `json:"title"`
73+
Filename string `json:"filename"`
74+
Channel string `json:"channel"`
75+
}
76+
77+
func uploadRun(opts *uploadOptions) error {
78+
ios := opts.factory.IOStreams
79+
cs := ios.ColorScheme()
80+
81+
// Validate all files exist before starting uploads
82+
for _, fp := range opts.filePaths {
83+
info, err := os.Stat(fp)
84+
if err != nil {
85+
return fmt.Errorf("file not found: %s", fp)
86+
}
87+
if info.IsDir() {
88+
return fmt.Errorf("%s is a directory, not a file", fp)
89+
}
90+
if info.Size() == 0 {
91+
return fmt.Errorf("%s is empty", fp)
92+
}
93+
}
94+
95+
// File uploads need files:write scope. Prefer user token so uploads
96+
// appear from the user rather than the bot app.
97+
client, err := opts.factory.UserClient()
98+
if err != nil {
99+
client, err = opts.factory.BotClient()
100+
if err != nil {
101+
return err
102+
}
103+
}
104+
105+
// Resolve channel/DM
106+
resolver := api.NewResolver(client.Slack)
107+
var channelID string
108+
if api.LooksLikeUser(opts.channel) {
109+
channelID, err = resolver.ResolveDM(opts.channel)
110+
} else {
111+
channelID, err = resolver.ResolveChannel(opts.channel)
112+
}
113+
114+
// If resolution fails with missing scope, try the other token
115+
if err != nil && api.IsMissingScopeError(err) {
116+
altClient, altErr := opts.factory.UserClient()
117+
if altErr != nil {
118+
altClient, altErr = opts.factory.BotClient()
119+
}
120+
if altErr == nil {
121+
fmt.Fprintf(ios.ErrOut, "%s Retrying with alternate token\n", cs.Yellow("!"))
122+
altResolver := api.NewResolver(altClient.Slack)
123+
if api.LooksLikeUser(opts.channel) {
124+
channelID, err = altResolver.ResolveDM(opts.channel)
125+
} else {
126+
channelID, err = altResolver.ResolveChannel(opts.channel)
127+
}
128+
if err == nil {
129+
client = altClient
130+
}
131+
}
132+
}
133+
134+
if err != nil {
135+
return fmt.Errorf("%s", api.FormatResolveError(err, opts.channel))
136+
}
137+
138+
var results []uploadResult
139+
140+
for i, fp := range opts.filePaths {
141+
info, _ := os.Stat(fp) // already validated above
142+
fileName := filepath.Base(fp)
143+
144+
title := fileName
145+
if opts.title != "" && i == 0 {
146+
title = opts.title
147+
}
148+
149+
params := slack.UploadFileV2Parameters{
150+
File: fp,
151+
FileSize: int(info.Size()),
152+
Filename: fileName,
153+
Title: title,
154+
Channel: channelID,
155+
}
156+
157+
// Only attach the comment to the first file upload
158+
if opts.comment != "" && i == 0 {
159+
params.InitialComment = opts.comment
160+
}
161+
if opts.threadTS != "" {
162+
params.ThreadTimestamp = opts.threadTS
163+
}
164+
165+
summary, err := client.Slack.UploadFileV2(params)
166+
if err != nil {
167+
return fmt.Errorf("failed to upload %s: %s", fileName, api.FormatError(err))
168+
}
169+
170+
results = append(results, uploadResult{
171+
FileID: summary.ID,
172+
Title: summary.Title,
173+
Filename: fileName,
174+
Channel: channelID,
175+
})
176+
177+
if !opts.json.WantsJSON() {
178+
fmt.Fprintf(ios.Out, "%s Uploaded %s to %s\n",
179+
cs.Green("✓"), cs.Bold(fileName), cs.Bold(opts.channel))
180+
}
181+
}
182+
183+
if opts.json.WantsJSON() {
184+
return opts.json.OutputJSON(ios.Out, results)
185+
}
186+
187+
if len(results) > 1 {
188+
fmt.Fprintf(ios.Out, "\n%s %d files uploaded to %s\n",
189+
cs.Green("✓"), len(results), cs.Bold(opts.channel))
190+
}
191+
192+
return nil
193+
}

0 commit comments

Comments
 (0)