mcpField
S
slack

slack

github.com/i9wa4生产力1260 次安装1 天前更新
代码仓库

关于 slack

这是通过直接调用 Slack API 获取频道历史、消息线程和搜索消息的 Skill。它使用 Slack 令牌,无需 MCP 服务。

主要功能

  1. 01获取频道历史
  2. 02读取消息线程
  3. 03按时间戳获取单条消息
  4. 04使用筛选条件搜索消息
  5. 05解析 Slack 消息和频道网址

适用场景

  1. 01查看频道近期消息
  2. 02根据消息网址查看线程
  3. 03按关键词、用户或日期查找消息

MCP 相关

此 Skill 直接调用 Slack API,并明确无需 MCP 服务。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/i9wa4/dotfiles --skill slack

安装后在 Claude Code 中输入/slack确认已加载。

详细介绍

Slack API 技能

使用直接 API 调用获取线程和搜索消息(无需 MCP 服务)。

1. 前提条件

1.1. 环境变量
export SLACK_MCP_XOXC_TOKEN="xoxc-..."
export SLACK_MCP_XOXD_TOKEN="xoxd-..."

2. URL 格式

2.1. 消息 URL(archives 格式)
https://xxx.slack.com/archives/C1234567890/p1234567890123456
  • C1234567890 = 频道 ID
  • p1234567890123456 = 时间戳(移除 p,并在最后 6 位数字前插入 .)
    • p1234567890123456 → 1234567890.123456
2.2. 频道 URL(客户端格式)
https://app.slack.com/client/E1234567890/C1234567890
https://app.slack.com/client/E1234567890/D1234567890
  • 第一个 ID(E...)= Enterprise/Workspace ID
  • 第二个 ID = 频道 ID(C = 频道,D = 私信)

3. 获取频道历史记录

获取频道中的最新消息(适用于 Google Calendar 等机器人的私信)。

3.1. 从客户端 URL 提取频道 ID
URL="https://app.slack.com/client/E05CUN3JKJN/D07UW8G7C9H"
CHANNEL=$(echo "$URL" | sed -n 's|.*/client/[^/]*/\([^/]*\).*|\1|p')
echo "$CHANNEL"  # D07UW8G7C9H
3.2. 获取最新消息
CHANNEL="D07UW8G7C9H"
FILE=$(mkoutput --dir tmp --label output)
curl -s -X POST "https://slack.com/api/conversations.history" \
  -H "Authorization: Bearer $SLACK_MCP_XOXC_TOKEN" \
  -H "Cookie: d=$SLACK_MCP_XOXD_TOKEN" \
  -d "channel=${CHANNEL}" \
  -d "limit=20" > "$FILE"

# Extract calendar events (for Google Calendar DM)
jq '.messages[] | select(.attachments) | .attachments[] | select(.title) | {pretext, title, text}' "$FILE"

4. 获取线程

4.1. 解析 URL 并获取
# Example URL: https://xxx.slack.com/archives/C1234567890/p1234567890123456
CHANNEL="C1234567890"
TS="1234567890.123456"

curl -s "https://slack.com/api/conversations.replies?channel=${CHANNEL}&ts=${TS}" \
  -H "Authorization: Bearer ${SLACK_MCP_XOXC_TOKEN}" \
  -H "Cookie: d=${SLACK_MCP_XOXD_TOKEN}" | jq '.messages[] | {user, text, ts}'
4.2. 使用 URL 解析的单行命令
URL="https://xxx.slack.com/archives/C08V9MYDQ1C/p1736929199388759"
CHANNEL=$(echo "$URL" | sed -n 's|.*/archives/\([^/]*\)/.*|\1|p')
TS_RAW=$(echo "$URL" | sed -n 's|.*/p\([0-9]*\).*|\1|p')
TS="${TS_RAW:0:10}.${TS_RAW:10}"
curl -s "https://slack.com/api/conversations.replies?channel=${CHANNEL}&ts=${TS}" \
  -H "Authorization: Bearer ${SLACK_MCP_XOXC_TOKEN}" \
  -H "Cookie: d=${SLACK_MCP_XOXD_TOKEN}" | jq '.messages[] | {user, text, ts}'
4.3. 获取单条消息(conversations.history)
CHANNEL="C1234567890"
TS="1234567890.123456"

curl -s -X POST "https://slack.com/api/conversations.history" \
  -H "Authorization: Bearer ${SLACK_MCP_XOXC_TOKEN}" \
  -H "Cookie: d=${SLACK_MCP_XOXD_TOKEN}" \
  -d "channel=${CHANNEL}" -d "latest=${TS}" -d "limit=1" -d "inclusive=true" | jq '.messages[0]'

5. 输出格式

显示每条消息的以下信息:

  • 用户 ID(或在需要时解析为姓名)
  • 消息文本
  • 时间戳

6. 搜索消息

注意:Enterprise Grid 可能会因 enterprise_is_restricted 错误而限制 search.messages API。

6.1. 基本搜索
QUERY="keyword"
curl -s "https://slack.com/api/search.messages?query=$(echo "$QUERY" | jq -sRr @uri)&count=20" \
  -H "Authorization: Bearer ${SLACK_MCP_XOXC_TOKEN}" \
  -H "Cookie: d=${SLACK_MCP_XOXD_TOKEN}" | jq '.messages.matches[] | {channel: .channel.name, user, text, ts, permalink}'
6.2. 使用筛选条件搜索
# Search in specific channel
QUERY="in:#channel-name keyword"

# Search from specific user
QUERY="from:@username keyword"

# Search with date range
QUERY="after:2026-01-01 before:2026-01-15 keyword"

# Combined filters
QUERY="in:#general from:@john after:2026-01-01 terraform"

curl -s "https://slack.com/api/search.messages?query=$(echo "$QUERY" | jq -sRr @uri)&count=20&sort=timestamp&sort_dir=desc" \
  -H "Authorization: Bearer ${SLACK_MCP_XOXC_TOKEN}" \
  -H "Cookie: d=${SLACK_MCP_XOXD_TOKEN}" | jq '.messages.matches[] | {channel: .channel.name, user, text, ts, permalink}'
6.3. 搜索选项
  • count:结果数量(默认值为 20,最大值为 100)
  • sort:score(相关性)或 timestamp
  • sort_dir:asc 或 desc

7. 错误处理

  • 如果响应中的 ok: false,显示错误消息
  • 常见错误:
    • channel_not_found - 频道 ID 无效
    • thread_not_found - 时间戳无效
    • invalid_auth - 令牌已过期或无效
    • enterprise_is_restricted - Enterprise Grid API 限制

评论

支持 Markdown