Can I get User IDs and Bot IDs of bots used in Slack channels for personal projects

I wanted to learn more about slack messages processing, lang graph etc for personal project of mine.

Please let me know if I can for #documentation or any one Slack channel.

Thanks

Shobha

Hi @shobhavijay

Here’s everything you need, I created a simple example with instructions that might help you as a starting point:


Step 1: Create Your Slack App

  1. Go to api.slack.com/apps
  2. Click “Create New App” → choose “From scratch”
  3. Give it a name (e.g. LangChain Bot) and pick your workspace
  4. Click “Create App”

Step 2: Configure App Settings

Enable Socket Mode

  • Go to Settings → Socket Mode
  • Toggle “Enable Socket Mode” to ON
  • It will ask you to create an App-Level Token → name it (e.g. socket-token), add scope connections:write, click “Generate”
  • Copy the token: this is your SLACK_APP_TOKEN (xapp-...)

Add Bot Scopes

  • Go to Features → OAuth & Permissions
  • Scroll to “Bot Token Scopes” and add:
Scope Purpose
app_mentions:read Detect when bot is @mentionedmentioned
chat:write Send messages
channels:history Read channel messages
users:read Look up user info / detect bots

Subscribe to Events

  • Go to Features → Event Subscriptions
  • Toggle “Enable Events” to ON
  • Under “Subscribe to bot events”, click “Add Bot User Event” and add:
    • app_mention
  • Click “Save Changes”

Install App to Workspace

  • Go to Features → OAuth & Permissions
  • Click “Install to Workspace” → Allow
  • Copy the Bot User OAuth Token — this is your SLACK_BOT_TOKEN (xoxb-...)

Add Bot to a Channel

  • In Slack, open the channel you want the bot in
  • Type /invite @YourBotName and hit Enter

Step 3: Get Your Tokens & IDs

# .env file
SLACK_BOT_TOKEN=xoxb-...       # From OAuth & Permissions page
SLACK_APP_TOKEN=xapp-...       # From Socket Mode page
ANTHROPIC_API_KEY=sk-ant-...   # From console.anthropic.com , you can use other models as well

Step 4: Install Dependencies

pip install langchain slack-bolt langchain-anthropic python-dotenv

Step 5: The Agent Code

import os
from dotenv import load_dotenv
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain.agents import create_agent

load_dotenv()

slack_app = App(token=os.environ["SLACK_BOT_TOKEN"])


@tool
def send_slack_reply(channel: str, thread_ts: str, message: str) -> str:
    """Send a reply to a Slack thread.

    Args:
        channel: The Slack channel ID.
        thread_ts: The thread timestamp to reply into.
        message: The message text to send.

    Returns:
        Confirmation string.
    """
    slack_app.client.chat_postMessage(
        channel=channel,
        thread_ts=thread_ts,
        text=message,
    )
    return f"Replied in channel {channel}"


@tool
def get_channel_history(channel: str, limit: int = 10) -> str:
    """Fetch recent messages from a Slack channel.

    Args:
        channel: The Slack channel ID.
        limit: Number of recent messages to fetch.

    Returns:
        Formatted string of recent messages.
    """
    result = slack_app.client.conversations_history(channel=channel, limit=limit)
    messages = result["messages"]
    return "\n".join(
        f"{m.get('user', 'bot')}: {m.get('text', '')}" for m in messages
    )


llm = ChatAnthropic(model="claude-3-5-sonnet-latest") # you can use whatever you like!!!

agent = create_agent(
    model=llm,
    tools=[send_slack_reply, get_channel_history],
    system_prompt=(
        "You are a helpful Slack assistant. "
        "When given a message, understand the user's intent and reply helpfully in the thread."
    ),
)


@slack_app.event("app_mention")
def handle_mention(event: dict, say: callable) -> None:
    user_message = event["text"]
    channel = event["channel"]
    thread_ts = event.get("thread_ts", event["ts"])

    agent.invoke({
        "messages": [
            {
                "role": "user",
                "content": (
                    f"A user mentioned you in Slack channel {channel} "
                    f"(thread: {thread_ts}). Their message: '{user_message}'. "
                    "Please reply helpfully in the thread."
                ),
            }
        ]
    })


if __name__ == "__main__":
    handler = SocketModeHandler(slack_app, os.environ["SLACK_APP_TOKEN"])
    handler.start()

Step 6: Run It

python bot.py

Then in Slack, go to the channel where you invited your bot and type:

@YourBotName what is the weather like today?

The bot will respond in the thread automatically.


Full Flow Summary

1. Create Slack App at api.slack.com/apps
2. Enable Socket Mode  →  get SLACK_APP_TOKEN (xapp-...)
3. Add bot scopes  →  install to workspace  →  get SLACK_BOT_TOKEN (xoxb-...)
4. Subscribe to app_mention event
5. Invite bot to channel with /invite @BotName
6. Run python bot.py
7. @mention the bot → agent processes → replies in thread

Docs

Slack App Setup

Topic Link
Create a new Slack App Creating an app | Slack Developer Docs
Socket Mode setup api.slack.com/apis/socket-mode
OAuth & Bot Token Scopes api.slack.com/authentication/oauth-v2
Event Subscriptions api.slack.com/events-api

Slack Bolt for Python (SDK used in code)

Topic Link
Getting Started slack.dev/bolt-python/getting-started

Langchain - Create Agent

Topic Link
Agent Agents - Docs by LangChain

Hope this helps you get started, Shobha! Feel free to ask any follow-up questions.

NOTE: I am not sure what you mean by `Can I get User IDs and Bot IDs of bots used in Slack channels` as reflected by the title of this thread, therefore I have structured my answer depending on what you have asked in the main content of the thread, as the main title of the thread have little similarity to what you have actually asked.

1 Like