A complete, beginner-friendly guide to collecting data from Slack using the official API.
This guide will walk you through setting up Slack API access and collecting data like:
- User presence status (online/away/do not disturb)
- Messages from channels
- User information
- Search results
- How This Works
- Prerequisites
- Step 1: Create a Slack App
- Step 2: Configure Permissions
- Step 3: Install and Get Tokens
- Step 4: Set Up Python
- Step 5: Run the Scripts
- Troubleshooting
- API Reference
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Your Code │ ──API── │ Slack │ ──────> │ Data │
│ (Python) │ Calls │ Servers │ │ (JSON) │
└─────────────┘ └─────────────┘ └─────────────┘
- You create a "Slack App" (gives you permission to access data)
- Slack gives you a "token" (like a password)
- Your Python code uses this token to request data
- Slack returns the data in JSON format
- You process/store the data however you want
Is this allowed? Yes! Slack provides an official API for this purpose.
| Item | Why You Need It |
|---|---|
| Slack account | Must be member of the workspace |
| Permission to create apps | Ask your workspace admin if needed |
| Python 3.8+ | To run the scripts |
Check Python installation:
python --version
# Should show: Python 3.x.xOpen your browser: https://api.slack.com/apps
Click the green "Create New App" button.
Select "From scratch" (not "From an app manifest").
- App Name:
Data Collector(or any name) - Workspace: Select your workspace
Click "Create App"
| Token Type | Prefix | Best For |
|---|---|---|
| Bot Token | xoxb- |
Presence monitoring, user info |
| User Token | xoxp- |
Messages, search, DMs |
In the left sidebar, click "OAuth & Permissions"
Under "Bot Token Scopes", click "Add an OAuth Scope" and add:
| Scope | Purpose |
|---|---|
users:read |
Read user information |
users:read.email |
Read user emails |
Under "User Token Scopes", add the scopes you need:
| Scope | Purpose |
|---|---|
search:read |
Search messages |
channels:history |
Read public channel messages |
channels:read |
List public channels |
groups:history |
Read private channel messages |
im:history |
Read direct messages |
users:read |
Read user info |
Tip: Start with search:read, channels:history, channels:read, and users:read.
At the top of "OAuth & Permissions", click "Install to Workspace"
Review permissions and click "Allow"
You'll see:
- Bot User OAuth Token:
xoxb-... - User OAuth Token:
xoxp-...
Copy both tokens and save them securely!
Copy the example file:
cp .env.example .envEdit .env and paste your tokens:
SLACK_BOT_TOKEN=xoxb-your-bot-token-here
SLACK_USER_TOKEN=xoxp-your-user-token-hereNEVER commit .env to git!
git clone https://github.com/thiagotbx123/slack-api-guide.git
cd slack-api-guidepython -m venv venvWindows:
venv\Scripts\activateMac/Linux:
source venv/bin/activatepip install -r requirements.txtEdit config.py and add user IDs:
MONITOR_USERS = [
"U01ABC123DE", # Get this from Slack
"U02XYZ456FG",
]How to find User IDs:
- Click on someone's profile in Slack
- Click "View full profile"
- Click "..." (more)
- Click "Copy member ID"
python slack_scraper.py --presenceOutput:
============================================================
PRESENCE CHECK - 2024-01-15 14:30:00
============================================================
[ONLINE] John Smith
[AWAY] Jane Doe
[ONLINE] Bob Wilson
python slack_scraper.py --channelspython slack_scraper.py --messages C01ABC123 --limit 50python slack_scraper.py --search "quarterly report" --limit 20Problem: App doesn't have the required permission.
Solution:
- Go to api.slack.com/apps
- Select your app → OAuth & Permissions
- Add the missing scope
- Click "Reinstall to Workspace"
- Copy new tokens to .env
Problem: Token is wrong or expired.
Solution:
- Check .env file for typos
- Verify you're using the right token type
- Reinstall app and get fresh tokens
Problem: Wrong channel ID or not a member.
Solution:
- Use channel ID (
C01ABC123), not name (#general) - Make sure you're a member of the channel
Problem: Too many requests.
Solution:
- Add delays between requests (180+ seconds for presence)
- See rate limits section below
| Endpoint | Token | Description |
|---|---|---|
users.list |
Bot | Get all workspace users |
users.info |
Bot | Get one user's details |
users.getPresence |
Bot | Get online/away status |
conversations.list |
User | List channels |
conversations.history |
User | Get channel messages |
search.messages |
User | Search messages |
| Tier | Limit | Use Case |
|---|---|---|
| Tier 1 | 1/sec | Posting messages |
| Tier 2 | 20+/min | Reading data |
| Tier 3 | 50+/min | Heavy reads |
Safe interval for presence monitoring: 180 seconds (3 minutes)
| Status | Meaning |
|---|---|
active |
User is online (green dot) |
away |
User inactive 10+ minutes |
slack-api-guide/
├── README.md # This file
├── .env.example # Template for tokens
├── .gitignore # Ignores .env and venv
├── requirements.txt # Python dependencies
├── config.py # Configuration
└── slack_scraper.py # Main script
Found an issue or want to improve the guide? Open a PR!
MIT License - Use freely for any purpose.
Author: Thiago (TestBox TSA) Based on: Production system monitoring 39 users