slack-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Slack CLI

Slack CLI

Interact with Slack workspaces from the command line using
slack-cli
and direct Slack API calls.
Assume
slack-cli
is already installed and configured.
If the
slack
command is not found or returns
not_inited
, refer to references/INSTALLATION.md for setup instructions.
使用
slack-cli
和直接调用Slack API从命令行与Slack工作区交互。
假设
slack-cli
已安装并配置完成。
如果
slack
命令未找到或返回
not_inited
,请参考references/INSTALLATION.md获取设置说明。

When to Use

适用场景

  • User wants to send, update, or delete Slack messages
  • User wants to read channel history or search messages
  • User wants to list channels, users, or conversations
  • User wants to upload files to Slack
  • User wants to manage reminders, snooze, or presence
  • User wants to automate Slack workflows
  • 用户想要发送、更新或删除Slack消息
  • 用户想要查看频道历史或搜索消息
  • 用户想要列出频道、用户或会话
  • 用户想要向Slack上传文件
  • 用户想要管理提醒、免打扰状态或在线状态
  • 用户想要自动化Slack工作流

Setup

设置步骤

If
slack
is not available or not configured, see references/INSTALLATION.md for full setup instructions.
The token is stored at the path configured in the
slack
binary's
etcdir
variable (typically
/opt/homebrew/etc/slack-cli/.slack
on macOS). You can also use the
SLACK_CLI_TOKEN
environment variable.
如果
slack
命令不可用或未配置,请查看references/INSTALLATION.md获取完整设置说明。
令牌存储在
slack
二进制文件
etcdir
变量配置的路径下(macOS上通常为
/opt/homebrew/etc/slack-cli/.slack
)。您也可以使用
SLACK_CLI_TOKEN
环境变量。

Credential Management with psst

使用psst管理凭证

Store the Slack token in the global psst vault:
bash
psst --global set SLACK_CLI_TOKEN --tag slack
For built-in commands, inject the token via psst:
bash
psst --global SLACK_CLI_TOKEN -- slack chat send 'Hello!' '#channel'
For direct API calls, use inline
psst get
:
bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" "https://slack.com/api/..."
将Slack令牌存储在全局psst密钥库中:
bash
psst --global set SLACK_CLI_TOKEN --tag slack
对于内置命令,通过psst注入令牌:
bash
psst --global SLACK_CLI_TOKEN -- slack chat send 'Hello!' '#channel'
对于直接API调用,使用内联
psst get
bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" "https://slack.com/api/..."

Built-in Commands

内置命令

The
slack
CLI supports these commands natively:
slack
CLI原生支持以下命令:

Chat

聊天

bash
undefined
bash
undefined

Send a message

发送消息

slack chat send 'Hello world!' '#channel'
slack chat send 'Hello world!' '#channel'

Send with rich formatting

发送富格式消息

slack chat send --text 'text' --channel '#channel' --color good --title 'Title' --pretext 'Pretext'
slack chat send --text 'text' --channel '#channel' --color good --title 'Title' --pretext 'Pretext'

Pipe content as a message

将内容通过管道作为消息发送

echo "some output" | slack chat send --channel '#channel'
echo "some output" | slack chat send --channel '#channel'

Update a message (requires timestamp and channel)

更新消息(需要时间戳和频道)

slack chat update 'Updated text' 1405894322.002768 '#channel'
slack chat update 'Updated text' 1405894322.002768 '#channel'

Delete a message

删除消息

slack chat delete 1405894322.002768 '#channel'
slack chat delete 1405894322.002768 '#channel'

Chain: send, capture ts+channel, then update

链式操作:发送、捕获时间戳+频道,然后更新

slack chat send 'hello' '#general' --filter '.ts + "\n" + .channel' |
xargs -n2 slack chat update 'goodbye'
undefined
slack chat send 'hello' '#general' --filter '.ts + "\n" + .channel' |
xargs -n2 slack chat update 'goodbye'
undefined

Files

文件

bash
undefined
bash
undefined

Upload a file

上传文件

slack file upload README.md '#channel'
slack file upload README.md '#channel'

Upload with metadata

带元数据上传

slack file upload README.md '#channel' --comment 'See attached' --title 'README'
slack file upload README.md '#channel' --comment 'See attached' --title 'README'

Create a Slack post from markdown

从markdown创建Slack帖子

slack file upload --file post.md --filetype post --title 'Post Title' --channels '#channel'
slack file upload --file post.md --filetype post --title 'Post Title' --channels '#channel'

List files

列出文件

slack file list slack file list --filter '[.files[] | {id, name, size}]'
slack file list slack file list --filter '[.files[] | {id, name, size}]'

File info / delete

文件信息 / 删除

slack file info F2147483862 slack file delete F2147483862
undefined
slack file info F2147483862 slack file delete F2147483862
undefined

Reminders

提醒

bash
undefined
bash
undefined

Add a reminder

添加提醒

slack reminder add 'lunch' $(date -v +30M "+%s")
slack reminder add 'lunch' $(date -v +30M "+%s")

List / complete / delete

列出 / 标记完成 / 删除

slack reminder list slack reminder complete Rm7MGABKT6 slack reminder delete Rm7MGABKT6
undefined
slack reminder list slack reminder complete Rm7MGABKT6 slack reminder delete Rm7MGABKT6
undefined

Snooze (Do Not Disturb)

免打扰(Snooze)

bash
slack snooze start 60          # Start snooze for 60 minutes
slack snooze info              # Check your snooze status
slack snooze end               # End snooze
bash
slack snooze start 60          # 开启60分钟免打扰
slack snooze info              # 查看免打扰状态
slack snooze end               # 结束免打扰

Presence

在线状态

bash
slack presence active
slack presence away
bash
slack presence active
slack presence away

Global Options

全局选项

All commands support:
  • --filter|-f <jq-filter>
    — Apply a jq filter to the JSON response
  • --compact|-cp
    — Compact JSON output
  • --monochrome|-m
    — No color in jq output
  • --trace|-x
    — Enable bash trace for debugging
所有命令均支持:
  • --filter|-f <jq-filter>
    — 对JSON响应应用jq过滤器
  • --compact|-cp
    — 压缩JSON输出
  • --monochrome|-m
    — jq输出无颜色
  • --trace|-x
    — 启用bash跟踪以调试

Known Limitations

已知限制

  • DMs via built-in CLI don't work reliably.
    slack chat send 'msg' '@user'
    often fails with
    channel_not_found
    . Use the direct API pattern below instead.
  • Messages sent via
    chat:write
    appear as the Slack App
    , not as your user. This is a Slack API limitation — even with a user token, the app identity is used.
  • 通过内置CLI发送私信不可靠。
    slack chat send 'msg' '@user'
    经常会因
    channel_not_found
    失败。请改用下面的直接API调用方式。
  • 通过
    chat:write
    发送的消息显示为Slack应用身份
    ,而非您的用户身份。这是Slack API的限制——即使使用用户令牌,仍会使用应用身份。

Direct API Calls

直接API调用

The
slack
CLI doesn't cover all Slack API methods. For anything beyond the built-in commands, call the Slack API directly using
curl
. Read the token from the CLI's config file.
slack
CLI并未覆盖所有Slack API方法。对于内置命令之外的操作,请使用
curl
直接调用Slack API。从CLI配置文件中读取令牌。

Sending Direct Messages

发送私信

The built-in
slack chat send
doesn't handle DMs well. Use the API directly:
bash
undefined
内置的
slack chat send
无法很好地处理私信。请直接使用API:
bash
undefined

Step 1: Open (or find) the DM channel with a user

步骤1:打开(或查找)与用户的私信频道

DM_CHANNEL=$(curl -s -X POST
-H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
-H "Content-Type: application/json"
-d '{"users":"USER_ID"}'
"https://slack.com/api/conversations.open" | jq -r '.channel.id')
DM_CHANNEL=$(curl -s -X POST
-H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
-H "Content-Type: application/json"
-d '{"users":"USER_ID"}'
"https://slack.com/api/conversations.open" | jq -r '.channel.id')

Step 2: Send the message

步骤2:发送消息

curl -s -X POST
-H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
-H "Content-Type: application/json"
-d "{"channel":"${DM_CHANNEL}","text":"Hello!"}"
"https://slack.com/api/chat.postMessage"

> **Requires `im:write` scope.** Without it, `conversations.open` will fail. If you don't have `im:write`, you can work around it by finding an existing DM channel:
> ```bash
> DM_CHANNEL=$(curl -s \
>   -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
>   "https://slack.com/api/conversations.list?types=im&limit=200" | \
>   jq -r '.channels[] | select(.user == "USER_ID") | .id')
> ```
curl -s -X POST
-H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
-H "Content-Type: application/json"
-d "{"channel":"${DM_CHANNEL}","text":"Hello!"}"
"https://slack.com/api/chat.postMessage"

> **需要`im:write`权限范围。** 没有该权限的话,`conversations.open`会失败。如果您没有`im:write`权限,可以通过查找现有私信频道来解决:
> ```bash
> DM_CHANNEL=$(curl -s \
>   -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
>   "https://slack.com/api/conversations.list?types=im&limit=200" | \
>   jq -r '.channels[] | select(.user == "USER_ID") | .id')
> ```

List Channels

列出频道

bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  "https://slack.com/api/conversations.list?types=public_channel,private_channel&limit=200" | \
  jq '[.channels[] | {name, id, is_private}]'
bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  "https://slack.com/api/conversations.list?types=public_channel,private_channel&limit=200" | \
  jq '[.channels[] | {name, id, is_private}]'

Read Channel History

查看频道历史

bash
undefined
bash
undefined

Get recent messages from a channel (use channel ID)

获取频道最近消息(使用频道ID)

curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.history?channel=CHANNEL_ID&limit=20" |
jq '[.messages[] | {user, text, ts}]'
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.history?channel=CHANNEL_ID&limit=20" |
jq '[.messages[] | {user, text, ts}]'

Get messages from last 7 days

获取过去7天的消息

OLDEST=$(date -v -7d +%s) curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.history?channel=CHANNEL_ID&oldest=${OLDEST}&limit=100" |
jq '[.messages[] | {user, text, ts}]'
OLDEST=$(date -v -7d +%s) curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.history?channel=CHANNEL_ID&oldest=${OLDEST}&limit=100" |
jq '[.messages[] | {user, text, ts}]'

Get messages within a specific date range (oldest and latest are unix timestamps)

获取特定日期范围内的消息(oldest和latest为Unix时间戳)

curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.history?channel=CHANNEL_ID&oldest=${START_TS}&latest=${END_TS}&limit=100" |
jq '[.messages[] | {user, text, ts}]'
undefined
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.history?channel=CHANNEL_ID&oldest=${START_TS}&latest=${END_TS}&limit=100" |
jq '[.messages[] | {user, text, ts}]'
undefined

List Users

列出用户

bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  "https://slack.com/api/users.list" | \
  jq '[.members[] | {name, id, real_name}]'
bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  "https://slack.com/api/users.list" | \
  jq '[.members[] | {name, id, real_name}]'

Resolve multiple user IDs (no batch endpoint — fetch all and filter)

解析多个用户ID(无批量端点——获取全部后过滤)

curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/users.list" |
jq '[.members[] | select(.id == "U1234" or .id == "U5678") | {id, name: .real_name}]'
undefined
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/users.list" |
jq '[.members[] | select(.id == "U1234" or .id == "U5678") | {id, name: .real_name}]'
undefined

Workspace Info

工作区信息

bash
undefined
bash
undefined

Use auth.test (not team.info — team.info requires a scope we don't have)

使用auth.test(不要用team.info——team.info需要我们没有的权限范围)

curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/auth.test" |
jq '{team, team_id, url}'
undefined
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/auth.test" |
jq '{team, team_id, url}'
undefined

Search Messages

搜索消息

bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  "https://slack.com/api/search.messages?query=keyword&count=10" | \
  jq '[.messages.matches[] | {channel: .channel.name, text, ts}]'
bash
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  "https://slack.com/api/search.messages?query=keyword&count=10" | \
  jq '[.messages.matches[] | {channel: .channel.name, text, ts}]'

Reactions

表情反应

bash
undefined
bash
undefined

Add a reaction

添加表情反应

curl -s -X POST
-H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
-H "Content-Type: application/json"
-d '{"channel":"CHANNEL_ID","timestamp":"1234567890.123456","name":"thumbsup"}'
"https://slack.com/api/reactions.add"
curl -s -X POST
-H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
-H "Content-Type: application/json"
-d '{"channel":"CHANNEL_ID","timestamp":"1234567890.123456","name":"thumbsup"}'
"https://slack.com/api/reactions.add"

Get reactions on a message

获取消息的表情反应

curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/reactions.get?channel=CHANNEL_ID&timestamp=1234567890.123456"
undefined
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/reactions.get?channel=CHANNEL_ID&timestamp=1234567890.123456"
undefined

Thread Replies

线程回复

bash
undefined
bash
undefined

Get replies in a thread (use the parent message ts)

获取线程中的回复(使用父消息的时间戳)

curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.replies?channel=CHANNEL_ID&ts=1234567890.123456" |
jq '[.messages[] | {user, text, ts}]'
undefined
curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)"
"https://slack.com/api/conversations.replies?channel=CHANNEL_ID&ts=1234567890.123456" |
jq '[.messages[] | {user, text, ts}]'
undefined

Post to a Thread

回复到线程

bash
curl -s -X POST \
  -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  -H "Content-Type: application/json" \
  -d '{"channel":"CHANNEL_ID","text":"reply text","thread_ts":"1234567890.123456"}' \
  "https://slack.com/api/chat.postMessage"
bash
curl -s -X POST \
  -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" \
  -H "Content-Type: application/json" \
  -d '{"channel":"CHANNEL_ID","text":"reply text","thread_ts":"1234567890.123456"}' \
  "https://slack.com/api/chat.postMessage"

Tips

小贴士

  • Channel arguments accept
    #name
    format for built-in commands, but direct API calls require channel IDs (e.g.
    CHANNEL_ID
    )
  • Use
    --filter
    with any built-in command to extract specific fields via jq
  • Pipe commands together using
    --filter '.ts + "\n" + .channel'
    to chain send/update/delete
  • For direct API calls, always quote the URL to prevent shell glob expansion
  • Pagination: most list endpoints support
    cursor
    and
    limit
    parameters — check
    response_metadata.next_cursor
    in the response
  • To look up a user ID by name:
    curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" "https://slack.com/api/users.list" | jq -r '.members[] | select(.real_name | test("Name"; "i")) | {name, id, real_name}'
  • Message permalinks:
    https://<workspace>.slack.com/archives/<channel_id>/p<ts_without_dot>
    — remove the dot from the message timestamp (e.g. ts
    1769935497.539749
    becomes
    p1769935497539749
    )
  • 内置命令的频道参数接受
    #name
    格式,但直接API调用需要频道ID(例如
    CHANNEL_ID
  • 对任何内置命令使用
    --filter
    参数,通过jq提取特定字段
  • 使用
    --filter '.ts + "\n" + .channel'
    将命令通过管道连接,实现发送/更新/删除的链式操作
  • 对于直接API调用,务必给URL加引号,防止shell通配符展开
  • 分页:大多数列表端点支持
    cursor
    limit
    参数——请查看响应中的
    response_metadata.next_cursor
  • 通过用户名查找用户ID:
    curl -s -H "Authorization: Bearer $(psst --global get SLACK_CLI_TOKEN)" "https://slack.com/api/users.list" | jq -r '.members[] | select(.real_name | test("Name"; "i")) | {name, id, real_name}'
  • 消息永久链接:
    https://<workspace>.slack.com/archives/<channel_id>/p<ts_without_dot>
    —— 移除消息时间戳中的点(例如时间戳
    1769935497.539749
    变为
    p1769935497539749