Slack Tools
If you want to add the ability for your agents to send and/or retrieve messages for your Slack channel SlackSendTool
and SlackRetrieveTool
are easy to add.
If you haven’t had a chance to read about how AG2’s Communication Platform agents and tools work, read the overview first.
Installation
Install AG2 with the LLM model provider and Slack platform extra.
Capabilities
SlackSendTool
can:
- Construct and send a message to the configured channel. If a message is longer than the platforms permitted message length, they will split the message into multiple messages.
SlackRetrieveTool
can:
- Retrieve the latest X messages from a channel.
- Retrieve messages since a given date.
- Retrieve messages since a given message ID.
- Retrieve a message given its ID.
Consider SlackAgent
if you don’t want to build your agent with these tools, it is a ready-to-go AG2 agent that already incorporates the tools and messaging instructions.
Platform configuration
Each tool is configured for a specific channel. This configuration is applied when you create the tool.
The tools require authentication (OAuth token) and channel details in order to send/retrieve messages.
Here are the steps to get a token:
- Create an app, from scratch, at https://api.slack.com/apps
- In your app settings, go to Features > OAuth & Permissions:
- Under “Bot Token Scopes”, add these permissions:
- chat:write (to send messages)
- channels:history
- channels:read (to access channel info)
- groups:read (for private channels)
- im:read (for direct messages)
- users:read (to get user info)
- files:read (to access file attachments)
- groups:history
- im:history
- mpim:history
- Under “Bot Token Scopes”, add these permissions:
- With your app setup, now install it in your workspace, using the “Install App” menu, to create an OAuth Token.
To get the ID for your channel:
- Open Slack in a browser
- Navigate to your channel
- Get the channel ID from the URL (e.g., …/C12345678)
Finally you need to add the bot to your channel:
- In Slack, go to your channel
- Type
/invite @YourBotName
, e.g./invite @ag2commsagent
Now you should be good to go with your OAuth token, channel ID, and a bot on your channel ready to send and retrieve messages!
Code example
Here’s a simple example using the Slack tools with a ConversableAgent
-based agent to send a weather forecast to a Slack channel.
Here’s the message it sent:
If you want to retrieve the last 5 messages to give a picture of the weather for the last week.
Tool execution
In AG2 the tool execution is typically handled by a separate agent that will follow the agent in the conversation (unless its in a swarm whereby tools are executed automatically). So, you will need to register the tools with another agent for execution.
You will see an example of this is the example code above.