Skip to main content

Slack Toolkit

This will help you getting started with the Slack toolkit. For detailed documentation of all SlackToolkit features and configurations head to the API reference.

Setup

To use this toolkit, you will need to get a token as explained in the Slack API docs. Once you've received a SLACK_USER_TOKEN, you can input it as an environment variable below.

import getpass
import os

if not os.getenv("SLACK_USER_TOKEN"):
os.environ["SLACK_USER_TOKEN"] = getpass.getpass("Enter your Slack user token: ")

If you want to get automated tracing from runs of individual tools, you can also set your LangSmith API key by uncommenting below:

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

Installation

This toolkit lives in the langchain-community package. We will also need the Slack SDK:

%pip install -qU langchain-community slack_sdk

Optionally, we can install beautifulsoup4 to assist in parsing HTML messages:

%pip install -qU beautifulsoup4 # This is optional but is useful for parsing HTML messages

Instantiation

Now we can instantiate our toolkit:

from langchain_community.agent_toolkits import SlackToolkit

toolkit = SlackToolkit()
API Reference:SlackToolkit

Tools

View available tools:

tools = toolkit.get_tools()

tools
[SlackGetChannel(client=<slack_sdk.web.client.WebClient object at 0x113caa8c0>),
SlackGetMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa4d0>),
SlackScheduleMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa440>),
SlackSendMessage(client=<slack_sdk.web.client.WebClient object at 0x113caa410>)]

This toolkit loads:

Use within an agent

Let's equip an agent with the Slack toolkit and query for information about a channel.

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-3.5-turbo-0125")

agent_executor = create_react_agent(llm, tools)
API Reference:ChatOpenAI
example_query = "When was the #general channel created?"

events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
message = event["messages"][-1]
if message.type != "tool": # mask sensitive information
event["messages"][-1].pretty_print()
================================ Human Message =================================

When was the #general channel created?
================================== Ai Message ==================================
Tool Calls:
get_channelid_name_dict (call_NXDkALjoOx97uF1v0CoZTqtJ)
Call ID: call_NXDkALjoOx97uF1v0CoZTqtJ
Args:
================================== Ai Message ==================================

The #general channel was created on timestamp 1671043305.
example_query = "Send a friendly greeting to channel C072Q1LP4QM."

events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
message = event["messages"][-1]
if message.type != "tool": # mask sensitive information
event["messages"][-1].pretty_print()
================================ Human Message =================================

Send a friendly greeting to channel C072Q1LP4QM.
================================== Ai Message ==================================
Tool Calls:
send_message (call_xQxpv4wFeAZNZgSBJRIuaizi)
Call ID: call_xQxpv4wFeAZNZgSBJRIuaizi
Args:
message: Hello! Have a great day!
channel: C072Q1LP4QM
================================== Ai Message ==================================

I have sent a friendly greeting to the channel C072Q1LP4QM.

API reference

For detailed documentation of all SlackToolkit features and configurations head to the API reference.


Was this page helpful?


You can also leave detailed feedback on GitHub.