Prerequisites

The following example requires the slack-sdk library.

pip install openai slack-sdk

Get a Slack token from here.

export SLACK_TOKEN=***

Example

The following agent will use Slack to send a message to a channel, list all channels, and get the message history of a specific channel.

cookbook/tools/slack_tools.py
import os

from phi.agent import Agent
from phi.tools.slack import SlackTools


slack_token = os.getenv("SLACK_TOKEN")
if not slack_token:
    raise ValueError("SLACK_TOKEN not set")
slack_tools = SlackTools(token=slack_token)

agent = Agent(tools=[slack_tools], show_tool_calls=True)

# Example 1: Send a message to a Slack channel
agent.print_response("Send a message 'Hello from Phi!' to the channel #general", markdown=True)

# Example 2: List all channels in the Slack workspace
agent.print_response("List all channels in our Slack workspace", markdown=True)

# Example 3: Get the message history of a specific channel
agent.print_response("Get the last 10 messages from the channel #random_junk", markdown=True)

Toolkit Params

ParameterTypeDefaultDescription
tokenstr-Slack API token for authentication
send_messageboolTrueEnables the functionality to send messages to Slack channels
list_channelsboolTrueEnables the functionality to list available Slack channels
get_channel_historyboolTrueEnables the functionality to retrieve message history from channels

Toolkit Functions

FunctionDescription
send_messageSends a message to a specified Slack channel
list_channelsLists all available channels in the Slack workspace
get_channel_historyRetrieves message history from a specified channel

Information