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
84 changes: 84 additions & 0 deletions cmd/slk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/gammons/slk/internal/ui/presencemenu"
"github.com/gammons/slk/internal/ui/reactionpicker"
"github.com/gammons/slk/internal/ui/sidebar"
"github.com/gammons/slk/internal/ui/slashpicker"
"github.com/gammons/slk/internal/ui/statusbar"
"github.com/gammons/slk/internal/ui/styles"
"github.com/gammons/slk/internal/ui/themeswitcher"
Expand Down Expand Up @@ -159,6 +160,7 @@ type WorkspaceContext struct {
UserID string
UnresolvedDMs []UnresolvedDM
CustomEmoji map[string]string // emoji name -> URL or "alias:target"
SlashCommands []slashpicker.Command
// Self presence and DND state for this workspace. Populated on connect
// and updated by manual_presence_change / dnd_updated WS events plus
// optimistic writes from the presence menu.
Expand Down Expand Up @@ -850,6 +852,13 @@ func run() error {
if err != nil && p != nil {
p.Send(ui.ToastMsg{Text: "Status change failed: " + err.Error()})
}
if p != nil {
fresh := make(map[string]string, len(wctx.UserNames))
for id, name := range wctx.UserNames {
fresh[id] = name
}
p.Send(ui.WorkspaceUserNamesUpdatedMsg{TeamID: wctx.TeamID, UserNames: fresh})
}
}()
})

Expand Down Expand Up @@ -1020,6 +1029,20 @@ func run() error {
}
})

app.SetSlashCommandRunner(func(channelID, text string) tea.Msg {
wctx := router.Active()
if wctx == nil {
return ui.ToastMsg{Text: "Command failed: no active workspace"}
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := wctx.Client.ExecuteSlashCommand(ctx, channelID, text); err != nil {
log.Printf("Warning: failed to execute slash command: %v", err)
return ui.ToastMsg{Text: "Command failed: " + err.Error()}
}
return ui.SlashCommandExecutedMsg{Text: text}
})

app.SetMessageEditor(func(channelID, ts, text string) tea.Msg {
wctx := router.Active()
if wctx == nil {
Expand Down Expand Up @@ -1337,6 +1360,7 @@ func run() error {
ExternalUsers: external,
UserID: wctx.UserID,
CustomEmoji: wctx.CustomEmoji,
SlashCommands: wctx.SlashCommands,
SectionsProvider: sectionsProviderAdapter{store: wctx.SectionStore},
}
})
Expand Down Expand Up @@ -1481,6 +1505,7 @@ func run() error {
ExternalUsers: external,
UserID: wctx.UserID,
CustomEmoji: wctx.CustomEmoji, // empty at this point; filled by the goroutine below
SlashCommands: wctx.SlashCommands,
SectionsProvider: sectionsProviderAdapter{store: wctx.SectionStore},
InitialActive: isInitial,
})
Expand Down Expand Up @@ -1556,6 +1581,59 @@ func run() error {
return err
}

func buildSlashPickerCommands(commands []slackclient.SlashCommand) []slashpicker.Command {
out := defaultSlashPickerCommands()
seen := make(map[string]int, len(out)+len(commands))
for i, cmd := range out {
seen[cmd.Name] = i
}
for _, cmd := range commands {
if cmd.Command == "" {
continue
}
candidate := slashpicker.Command{
Name: cmd.Command,
Description: cmd.Description,
UsageHint: cmd.UsageHint,
}
if idx, ok := seen[candidate.Name]; ok {
out[idx] = mergeSlashCommand(out[idx], candidate)
continue
}
seen[candidate.Name] = len(out)
out = append(out, candidate)
}
return out
}

func mergeSlashCommand(base, candidate slashpicker.Command) slashpicker.Command {
if candidate.Description != "" {
base.Description = candidate.Description
}
if candidate.UsageHint != "" {
base.UsageHint = candidate.UsageHint
}
return base
}

func defaultSlashPickerCommands() []slashpicker.Command {
return []slashpicker.Command{
{Name: "/away", Description: "Set yourself away"},
{Name: "/active", Description: "Set yourself active"},
{Name: "/dnd", Description: "Manage do not disturb", UsageHint: "[minutes]"},
{Name: "/invite", Description: "Invite people to this channel", UsageHint: "@user"},
{Name: "/leave", Description: "Leave the current channel"},
{Name: "/me", Description: "Post an emote message", UsageHint: "action"},
{Name: "/msg", Description: "Open a direct message", UsageHint: "@user message"},
{Name: "/remind", Description: "Create a reminder", UsageHint: "who what when"},
{Name: "/shrug", Description: "Append a shrug", UsageHint: "message"},
{Name: "/status", Description: "Set your status", UsageHint: "text"},
{Name: "/topic", Description: "Set the channel topic", UsageHint: "text"},
{Name: "/who", Description: "See who is in the channel"},
{Name: "/zoom", Description: "Start or share a Zoom meeting", UsageHint: "start | meeting topic"},
}
}

func connectWorkspace(ctx context.Context, token slackclient.Token, db *cache.DB, cfg config.Config, avatarCache *avatar.Cache, p *tea.Program) (*WorkspaceContext, error) {
client := slackclient.NewClient(token.AccessToken, token.Cookie)
if err := client.Connect(ctx); err != nil {
Expand All @@ -1572,6 +1650,7 @@ func connectWorkspace(ctx context.Context, token slackclient.Token, db *cache.DB
UserNamesByHandle: make(map[string]string),
BotUserIDs: make(map[string]bool),
CustomEmoji: make(map[string]string),
SlashCommands: defaultSlashPickerCommands(),
LastVisitedByChannel: make(map[string]int64),
}
wctx.SubscriptionsAvailable = true
Expand Down Expand Up @@ -1656,6 +1735,11 @@ func connectWorkspace(ctx context.Context, token slackclient.Token, db *cache.DB
} else {
wctx.LastVisitedByChannel = visits
}
if commands, err := client.ListSlashCommands(ctx); err == nil {
if pickerCommands := buildSlashPickerCommands(commands); len(pickerCommands) > 0 {
wctx.SlashCommands = pickerCommands
}
}

// Initialize Slack-native section store if enabled. Bootstrap is
// best-effort: failure is logged, the field stays nil, and the
Expand Down
164 changes: 164 additions & 0 deletions internal/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -528,6 +529,15 @@ type HistorySinceResult struct {
Capped bool
}

type SlashCommand struct {
Command string
Description string
UsageHint string
Type string
AppID string
AppName string
}

// GetHistorySince fetches all messages newer than `oldest` for the
// given channel, paginating through next_cursor up to a hard ceiling
// of maxTotal messages. Slack returns messages newest-first per page;
Expand Down Expand Up @@ -642,6 +652,160 @@ func (c *Client) SendMessage(ctx context.Context, channelID, text string) (strin
return ts, mr, nil
}

func (c *Client) ExecuteSlashCommand(ctx context.Context, channelID, text string) error {
command, args, ok := splitSlashCommand(text)
if channelID == "" {
return fmt.Errorf("executing slash command: missing channel")
}
if !ok {
return fmt.Errorf("executing slash command: invalid command")
}

form := url.Values{
"channel": {channelID},
"command": {command},
"text": {args},
}
if meta, err := c.lookupSlashCommand(ctx, command); err == nil {
if meta.AppID != "" {
form.Set("app", meta.AppID)
}
if meta.Type != "" {
form.Set("type", meta.Type)
}
}

body, err := c.postForm(ctx, "chat.command", form)
if err != nil {
return fmt.Errorf("executing slash command: %w", err)
}

var resp struct {
OK bool `json:"ok"`
Error string `json:"error"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return fmt.Errorf("parsing chat.command response: %w (body=%q)", err, truncateForLog(body))
}
if !resp.OK {
if resp.Error == "" {
resp.Error = "unknown_error"
}
return fmt.Errorf("chat.command: %s", resp.Error)
}
return nil
}

func (c *Client) lookupSlashCommand(ctx context.Context, command string) (SlashCommand, error) {
commands, err := c.ListSlashCommands(ctx)
if err != nil {
return SlashCommand{}, err
}
for _, item := range commands {
if item.Command == command {
return item, nil
}
}
return SlashCommand{}, fmt.Errorf("command not found")
}

func splitSlashCommand(text string) (command, args string, ok bool) {
trimmed := strings.TrimSpace(text)
if !strings.HasPrefix(trimmed, "/") || len(trimmed) == 1 {
return "", "", false
}
for i, r := range trimmed {
if i == 0 {
continue
}
if r == ' ' || r == '\t' || r == '\n' {
return trimmed[:i], strings.TrimSpace(trimmed[i:]), true
}
}
return trimmed, "", true
}

func (c *Client) ListSlashCommands(ctx context.Context) ([]SlashCommand, error) {
body, err := c.postForm(ctx, "commands.list", nil)
if err != nil {
return nil, fmt.Errorf("listing slash commands: %w", err)
}

var resp struct {
OK bool `json:"ok"`
Error string `json:"error"`
Commands map[string]json.RawMessage `json:"commands"`
CacheTS json.RawMessage `json:"cache_ts"`
}
if err := json.Unmarshal(body, &resp); err != nil {
return nil, fmt.Errorf("parsing commands.list response: %w (body=%q)", err, truncateForLog(body))
}
if !resp.OK {
if resp.Error == "" {
resp.Error = "unknown_error"
}
return nil, fmt.Errorf("commands.list: %s", resp.Error)
}

type commandEntry struct {
Command string `json:"command"`
Name string `json:"name"`
Description string `json:"description"`
UsageHint string `json:"usage_hint"`
ArgHint string `json:"arg_hint"`
Hint string `json:"hint"`
Desc string `json:"desc"`
Usage string `json:"usage"`
Type string `json:"type"`
AppID string `json:"app"`
AppName string `json:"app_name"`
}

commands := make([]SlashCommand, 0, len(resp.Commands))
for key, raw := range resp.Commands {
entry := commandEntry{}
if len(raw) > 0 && string(raw) != "null" {
if err := json.Unmarshal(raw, &entry); err != nil {
entry.Command = key
}
}
name := entry.Command
if name == "" {
name = entry.Name
}
if name == "" {
name = key
}
if name == "" {
continue
}
hint := entry.UsageHint
if hint == "" {
hint = entry.ArgHint
}
if hint == "" {
hint = entry.Hint
}
if hint == "" {
hint = entry.Usage
}
description := entry.Description
if description == "" {
description = entry.Desc
}
commands = append(commands, SlashCommand{
Command: name,
Description: description,
UsageHint: hint,
Type: entry.Type,
AppID: entry.AppID,
AppName: entry.AppName,
})
}
sort.Slice(commands, func(i, j int) bool { return commands[i].Command < commands[j].Command })
return commands, nil
}

// UploadFile uploads a single file to a channel (and optional thread)
// using Slack's V2 external-upload flow. The slack-go library's
// UploadFileContext (named for the underlying file.upload.v2 API)
Expand Down
Loading