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
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:
LangChain Bot) and pick your workspacesocket-token), add scope connections:write, click “Generate”SLACK_APP_TOKEN (xapp-...)| 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 |
app_mentionSLACK_BOT_TOKEN (xoxb-...)/invite @YourBotName and hit Enter# .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
pip install langchain slack-bolt langchain-anthropic python-dotenv
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()
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.
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
| 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 |
| Topic | Link |
|---|---|
| Getting Started | slack.dev/bolt-python/getting-started |
| 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.