SlackAgent
If you need an agent to send messages to your Slack channel and/or retrieve Slack messages to action, SlackAgent
can help you.
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
SlackAgent
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.
- 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.
It will also automatically append Slack’s messaging requirements to the system message:
- 40,000 character limit
- Markdown
- bold/italic/code
- emojis
- notification formats
This is on by default, but you can turn these off by setting has_writing_instructions
to False
when creating the agent.
In-built Tools
The SlackAgent
has two in-built tools that it will call upon as it needs:
Find out more about these tools and how you can add them to your own AG2 agents in the Slack Tools documentation.
Platform configuration
Each agent is configured for a specific channel. This configuration is applied when you create the agent.
The SlackAgent
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 SlackAgent
that gets the current weather and sends a message to a Slack channel to give them a weather forecast.
Here’s the message it sent: