slack

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Slack Web API Skill

Slack Web API Skill

Interact with Slack: read, write, search, react, pin, and manage conversations via the Web API.
通过Web API与Slack交互:支持读取、写入、搜索消息,添加表情反应、固定消息以及管理对话。

API Wrapper

API 封装器

All calls go through the script at
scripts/slack-api.sh
relative to this skill's base directory:
{SKILL_DIR}/scripts/slack-api.sh <method> [key=value ...]
Where
{SKILL_DIR}
is the base directory provided when this skill is loaded (e.g.,
~/.agents/skills/slack
).
For the full method reference, see references/api-methods.md.
所有调用都通过该技能基础目录下的
scripts/slack-api.sh
脚本执行:
{SKILL_DIR}/scripts/slack-api.sh <method> [key=value ...]
其中
{SKILL_DIR}
是加载该技能时的基础目录(例如:
~/.agents/skills/slack
)。
完整的方法参考请查看references/api-methods.md

Parsing Slack URLs

解析Slack链接

Extract channel and timestamp from Slack URLs:
https://{WORKSPACE}.slack.com/archives/{CHANNEL_ID}/p{TIMESTAMP_WITHOUT_DOT}
Insert a dot before the last 6 digits of the timestamp:
  • URL: p1770725748342899 -> ts: 1770725748.342899
For threaded messages, the URL may include ?thread_ts= parameter.
从Slack链接中提取频道ID和时间戳:
https://{WORKSPACE}.slack.com/archives/{CHANNEL_ID}/p{TIMESTAMP_WITHOUT_DOT}
在时间戳的最后6位数字前添加一个点:
  • 链接中的格式:p1770725748342899 -> 转换为ts:1770725748.342899
对于线程消息,链接中可能包含?thread_ts=参数。

Read Operations

读取操作

Read a thread

读取线程消息

slack-api.sh conversations.replies channel=CHANNEL_ID ts=THREAD_TS limit=100
slack-api.sh conversations.replies channel=CHANNEL_ID ts=THREAD_TS limit=100

Read channel history

读取频道历史消息

slack-api.sh conversations.history channel=CHANNEL_ID limit=20
Optional:
oldest
/
latest
(Unix timestamps) to bound the time range.
slack-api.sh conversations.history channel=CHANNEL_ID limit=20
可选参数:
oldest
/
latest
(Unix时间戳)用于限定时间范围。

Search messages

搜索消息

slack-api.sh search.messages query="search terms" count=10
Modifiers:
in:#channel
,
from:@user
,
before:YYYY-MM-DD
,
after:YYYY-MM-DD
,
has:link
,
has:reaction
,
has:pin
.
slack-api.sh search.messages query="search terms" count=10
修饰符:
in:#channel
(指定频道)、
from:@user
(指定用户)、
before:YYYY-MM-DD
(指定日期之前)、
after:YYYY-MM-DD
(指定日期之后)、
has:link
(包含链接)、
has:reaction
(包含反应)、
has:pin
(已固定)。

Find a channel by name

通过名称查找频道

slack-api.sh conversations.list types=public_channel limit=200 | python3 -c "import sys,json; channels=json.load(sys.stdin).get('channels',[]); matches=[c for c in channels if 'TARGET' in c['name']]; [print(f"{c['id']} #{c['name']}") for c in matches]"
slack-api.sh conversations.list types=public_channel limit=200 | python3 -c "import sys,json; channels=json.load(sys.stdin).get('channels',[]); matches=[c for c in channels if 'TARGET' in c['name']]; [print(f"{c['id']} #{c['name']}") for c in matches]"

Look up a user

查询用户信息

slack-api.sh users.info user=USER_ID
Name:
.user.real_name
or
.user.profile.display_name
.
slack-api.sh users.info user=USER_ID
用户名称:
.user.real_name
.user.profile.display_name

List pinned items

列出固定的消息

slack-api.sh pins.list channel=CHANNEL_ID
slack-api.sh pins.list channel=CHANNEL_ID

List channel members

列出频道成员

slack-api.sh conversations.members channel=CHANNEL_ID limit=100
slack-api.sh conversations.members channel=CHANNEL_ID limit=100

Write Operations

写入操作

Send a message

发送消息

slack-api.sh chat.postMessage channel=CHANNEL_ID text="Hello world"
Thread reply: add
thread_ts=PARENT_TS
. Broadcast to channel: add
reply_broadcast=true
.
slack-api.sh chat.postMessage channel=CHANNEL_ID text="Hello world"
回复线程:添加
thread_ts=PARENT_TS
参数。广播到频道:添加
reply_broadcast=true
参数。

Edit a message

编辑消息

slack-api.sh chat.update channel=CHANNEL_ID ts=MESSAGE_TS text="Updated text"
slack-api.sh chat.update channel=CHANNEL_ID ts=MESSAGE_TS text="Updated text"

Delete a message

删除消息

slack-api.sh chat.delete channel=CHANNEL_ID ts=MESSAGE_TS
slack-api.sh chat.delete channel=CHANNEL_ID ts=MESSAGE_TS

Add a reaction

添加表情反应

slack-api.sh reactions.add channel=CHANNEL_ID timestamp=MESSAGE_TS name=thumbsup
Emoji name without colons. Supports skin tones:
thumbsup::skin-tone-3
.
slack-api.sh reactions.add channel=CHANNEL_ID timestamp=MESSAGE_TS name=thumbsup
表情名称无需加冒号。支持肤色设置:
thumbsup::skin-tone-3

Remove a reaction

移除表情反应

slack-api.sh reactions.remove channel=CHANNEL_ID timestamp=MESSAGE_TS name=thumbsup
slack-api.sh reactions.remove channel=CHANNEL_ID timestamp=MESSAGE_TS name=thumbsup

Pin a message

固定消息

slack-api.sh pins.add channel=CHANNEL_ID timestamp=MESSAGE_TS
slack-api.sh pins.add channel=CHANNEL_ID timestamp=MESSAGE_TS

Unpin a message

取消固定消息

slack-api.sh pins.remove channel=CHANNEL_ID timestamp=MESSAGE_TS
slack-api.sh pins.remove channel=CHANNEL_ID timestamp=MESSAGE_TS

Image and File Handling

图片与文件处理

Messages may contain files. For images in the JSON response:
source ~/.claude/slack-tokens.env
curl -s "FILE_URL_PRIVATE" \
  -H "Authorization: Bearer ${SLACK_XOXC}" \
  -H "Cookie: d=${SLACK_XOXD}" \
  -o /tmp/slack-image-N.png
Then use the Read tool to view the downloaded image.
消息中可能包含文件。对于JSON响应中的图片,可通过以下方式下载:
source ~/.claude/slack-tokens.env
curl -s "FILE_URL_PRIVATE" \
  -H "Authorization: Bearer ${SLACK_XOXC}" \
  -H "Cookie: d=${SLACK_XOXD}" \
  -o /tmp/slack-image-N.png
然后使用读取工具查看下载后的图片。

Output Formatting

输出格式化

  1. Parse JSON with python3 -c "import sys,json; ..."
  2. Resolve user IDs (U...) to real names via users.info (cache lookups)
  3. Present messages with timestamps and names
  4. Replace <@U...> mentions with resolved real names
  5. Decode Slack markup (entities, link syntax, channel references) to plain text
  1. 使用python3 -c "import sys,json; ..."解析JSON
  2. 通过users.info将用户ID(U...)解析为真实姓名(使用缓存查询)
  3. 展示消息时包含时间戳和用户名称
  4. 将<@U...>提及替换为解析后的真实姓名
  5. 将Slack标记(实体、链接语法、频道引用)解码为纯文本

Rate Limiting

速率限制

  • Add sleep 1 between consecutive API calls
  • Never bulk-paginate large datasets (users.list), it kills the session
  • Prefer targeted queries over bulk fetches
  • 连续API调用之间添加sleep 1延迟
  • 不要批量分页大型数据集(如users.list),这会导致会话中断
  • 优先使用针对性查询而非批量获取

Token Refresh (Automatic)

令牌刷新(自动)

Token refresh is fully automatic. The API wrapper (
slack-api.sh
):
  1. Auto-refreshes if
    ~/.claude/slack-tokens.env
    is missing
  2. Auto-refreshes on
    invalid_auth
    errors and retries the call
Tokens are extracted from the running Chrome browser:
  • xoxc: from Slack's localStorage via AppleScript
  • xoxd: from Chrome's cookie database via
    lsof
    +
    pycookiecheat
Prerequisites (one-time):
  • Chrome > View > Developer > Allow JavaScript from Apple Events (must stay enabled)
  • Slack open in a Chrome tab at app.slack.com
  • uvx
    installed
令牌刷新完全自动化。API封装器(
slack-api.sh
):
  1. 如果缺少
    ~/.claude/slack-tokens.env
    文件,则自动刷新令牌
  2. 遇到
    invalid_auth
    错误时自动刷新令牌并重试调用
令牌从运行中的Chrome浏览器中提取:
  • xoxc:通过AppleScript从Slack的localStorage中获取
  • xoxd:通过
    lsof
    +
    pycookiecheat
    从Chrome的cookie数据库中获取
前置条件(一次性配置):
  • Chrome > 视图 > 开发者 > 允许来自Apple事件的JavaScript(必须保持启用状态)
  • Slack在Chrome标签页中打开,地址为app.slack.com
  • 已安装
    uvx

Setup

设置

No manual setup needed. On first API call, tokens are extracted automatically from Chrome. If extraction fails, ensure the prerequisites above are met.
无需手动设置。首次调用API时,将自动从Chrome中提取令牌。 如果提取失败,请确保满足上述前置条件。

Full API Reference

完整API参考

For additional methods (bookmarks, user groups, reminders, emoji, files, user profiles, etc.), see references/api-methods.md.
如需了解更多方法(书签、用户组、提醒、表情、文件、用户资料等),请查看references/api-methods.md

Error Handling

错误处理

  • not_in_channel: User does not have access to this channel
  • channel_not_found: Invalid channel ID
  • invalid_auth: Token expired, auto-refresh attempted (see Token Refresh)
  • ratelimited: Wait and retry with sleep 5
  • cant_update_message / cant_delete_message: Can only modify own messages
  • not_in_channel:用户无权访问该频道
  • channel_not_found:无效的频道ID
  • invalid_auth:令牌已过期,将尝试自动刷新(请查看令牌刷新部分)
  • ratelimited:等待并重试,添加sleep 5延迟
  • cant_update_message / cant_delete_message:只能修改自己发送的消息