AgentSkillsCN

Slack Reader

Slack阅读器

SKILL.md
skill
---
name: slack-reader
description: 'Read Slack messages by permalink URL. Fetches message content, thread replies, channel context, and resolves user mentions. Read-only access to Slack workspace data.'
---

# Slack Reader

Read Slack messages and context by permalink URL. This skill provides read-only access to Slack workspace data.

## Overview

Fetches Slack message content given a message permalink, including:
- The target message with resolved @mentions
- Thread replies (if the message is a thread parent)
- Surrounding channel context (messages before/after)
- Channel metadata (name, topic, purpose)
- Resolved user profiles

## Setup

### Prerequisites
- Node.js 20+
- `@slack/web-api` package: `npm install @slack/web-api`

### Environment Variables

#### Single Workspace (Simple Setup)

```bash
export SLACK_BOT_TOKEN=xoxb-your-bot-token

Multiple Workspaces

For accessing multiple Slack workspaces, use SLACK_WORKSPACES with a JSON object mapping aliases to tokens:

bash
export SLACK_WORKSPACES='{"personal": "xoxb-personal-token", "company": "xoxb-company-token"}'

Workspace aliases are used for:

  • The --workspace flag to explicitly select a workspace
  • Error messages when workspace selection is ambiguous
  • Auto-matching against URL domains (e.g., URL domain "company" matches alias "company")

Required Slack Scopes

Your Slack app must have these OAuth scopes:

ScopePurpose
channels:historyRead messages from public channels
channels:readView basic channel info
groups:historyRead messages from private channels
groups:readView basic private channel info
im:historyRead direct messages
im:readView basic DM info
mpim:historyRead group direct messages
mpim:readView basic group DM info
users:readView user profiles

Creating a Slack Token

Option 1: Create from Manifest (Recommended)

  1. Go to api.slack.com/apps
  2. Click Create New AppFrom a manifest
  3. Select your workspace
  4. Choose JSON and paste:
json
{
  "display_information": {
    "name": "Slack Reader",
    "description": "Read-only access for agent skills",
    "background_color": "#0040ff"
  },
  "features": {
    "bot_user": {
      "display_name": "Slack Reader",
      "always_online": false
    }
  },
  "oauth_config": {
    "scopes": {
      "user": [
        "canvases:read",
        "channels:history",
        "channels:read",
        "groups:history",
        "groups:read",
        "im:history",
        "im:read",
        "mpim:history",
        "mpim:read",
        "users:read"
      ],
      "bot": [
        "canvases:read",
        "channels:history",
        "channels:read",
        "groups:history",
        "groups:read",
        "im:history",
        "im:read",
        "mpim:history",
        "mpim:read",
        "users:read"
      ]
    }
  },
  "settings": {
    "org_deploy_enabled": false,
    "socket_mode_enabled": false,
    "token_rotation_enabled": false
  }
}
  1. Click CreateInstall to Workspace
  2. Copy the User OAuth Token (starts with xoxp-)

User tokens (xoxp-) can access all channels you have access to. Bot tokens (xoxb-) require the bot to be invited to each channel.

Option 2: Manual Setup

  1. Go to api.slack.com/apps
  2. Create or select your app
  3. Navigate to OAuth & Permissions
  4. Add the required scopes under User Token Scopes (or Bot Token Scopes)
  5. Install the app to your workspace
  6. Copy the token (xoxp- for user, xoxb- for bot)

Available Scripts

read-message.js

Read a Slack message by permalink URL.

bash
node scripts/read-message.js --url <permalink> [options]

Options:

OptionDescription
--url <url>Slack message permalink (required)
--workspace <name>Workspace alias when using multiple workspaces
--context-size <n>Number of messages before/after for context (default: 5)
--helpShow help message

Workspace Resolution:

When multiple workspaces are configured, the script resolves which token to use:

  1. If --workspace is specified, uses that workspace's token
  2. If the URL domain matches a workspace alias, uses that token
  3. If ambiguous, returns a SLACK_WORKSPACE_AMBIGUOUS error listing available workspaces

Example with workspace flag:

bash
node scripts/read-message.js \
  --url "https://myworkspace.slack.com/archives/C0123456789/p1706554800123456" \
  --workspace company

Output:

JSON object containing:

  • metadata: URL, fetch timestamp, workspace
  • channel: Channel info (name, topic, purpose)
  • targetMessage: The requested message with resolved mentions
  • thread: Thread replies if the message is a thread parent
  • context: Messages before and after the target

Slack URL Format

Message permalinks follow this format:

code
https://workspace.slack.com/archives/CHANNEL_ID/pTIMESTAMP

Where:

  • workspace - Your Slack workspace subdomain
  • CHANNEL_ID - Channel ID (e.g., C01234567)
  • TIMESTAMP - Message timestamp without decimal (e.g., p1706554800123456)

You can get a message permalink by clicking the message menu (⋮) and selecting "Copy link".

Examples

Read a message with default context

bash
node scripts/read-message.js \
  --url "https://myworkspace.slack.com/archives/C0123456789/p1706554800123456"

Read with more surrounding context

bash
node scripts/read-message.js \
  --url "https://myworkspace.slack.com/archives/C0123456789/p1706554800123456" \
  --context-size 10

Pipe output to jq for formatting

bash
node scripts/read-message.js --url "https://..." | jq '.targetMessage'

Error Codes

CodeDescriptionRemediation
SLACK_SDK_MISSING@slack/web-api package is not installedRun: npm install @slack/web-api
SLACK_AUTH_MISSINGNo tokens configuredSet SLACK_BOT_TOKEN or SLACK_WORKSPACES
SLACK_AUTH_INVALIDToken is invalid or expiredVerify token and app installation
SLACK_WORKSPACES_INVALIDSLACK_WORKSPACES is not valid JSONCheck JSON format
SLACK_WORKSPACE_NOT_FOUNDSpecified workspace alias not foundUse --workspace with valid alias
SLACK_WORKSPACE_AMBIGUOUSMultiple workspaces configured but none specifiedUse --workspace flag to select
SLACK_URL_INVALIDInvalid message URL formatUse a valid permalink
SLACK_CHANNEL_NOT_FOUNDChannel not found or bot lacks accessInvite bot to channel
SLACK_MESSAGE_NOT_FOUNDMessage not foundVerify URL and message exists
SLACK_PERMISSION_DENIEDMissing required scopesAdd required OAuth scopes
SLACK_RATE_LIMITEDAPI rate limit exceededWait and retry
SLACK_API_ERRORGeneral API errorCheck error details

Library Files

lib/client.js

Slack API client wrapper with methods for:

  • createClient(WebClient, token) - Create authenticated client
  • getMessage(client, channelId, ts) - Fetch a single message
  • getThreadReplies(client, channelId, threadTs) - Fetch thread replies
  • getChannelContext(client, channelId, ts, size) - Fetch surrounding messages
  • getChannelInfo(client, channelId) - Fetch channel metadata
  • resolveUsers(client, userIds) - Fetch user profiles

lib/url-parser.js

Parse Slack permalinks to extract workspace, channel ID, and message timestamp.

lib/normalizer.js

Format API responses into consistent output structure.

lib/errors.js

Structured error handling with codes and remediation guidance.

lib/workspaces.js

Multi-workspace token management:

  • parseWorkspaces(envValue) - Parse SLACK_WORKSPACES JSON
  • getConfiguredWorkspaces() - Get all configured workspaces
  • resolveWorkspace(workspaces, specifiedName, urlDomain) - Resolve which workspace to use
  • getAvailableWorkspaces() - Get comma-separated list of workspace aliases

Official Documentation

code