In today’s collaborative workplace, messaging platforms like Slack have become central hubs for team communication. By integrating AI capabilities into these platforms, organizations can enhance productivity, streamline workflows, and create more engaging user experiences. This is where AI-powered chatbots come into play, offering intelligent assistance directly within the communication channels teams already use.
Whether you’re a developer looking to enhance your team’s Slack workspace or a product manager seeking to streamline communication processes, DigitalOcean’s GenAI Platform provides an elegant solution for building sophisticated AI chatbots without complex infrastructure. By leveraging GenAI Platform, you can focus on creating meaningful interactions rather than managing the underlying AI infrastructure.
In this tutorial, we’ll walk through the process of creating a Slack AI chatbot that can summarize conversations, answer questions, and interact with your team using DigitalOcean’s GenAI Platform. The bot will respond when mentioned in channels, accept direct messages, and even provide AI-generated summaries of Slack threads on demand.
What makes this approach particularly compelling is how it brings the capabilities of large language models directly into your team’s everyday communication channels. This integration opens up possibilities for more efficient collaboration and information processing within Slack.
AI-powered Slack bots are particularly useful for a wide range of use cases, including but not limited to:
By integrating AI chatbots with Slack, we create a powerful combination that enhances team productivity through instant access to AI capabilities. However, like any technology solution, there are trade-offs to consider when implementing AI chatbots in Slack.
First, we’ll create a Slack app that will serve as the foundation for our AI chatbot:
Open https://api.slack.com/apps/new and choose “From an app manifest”.
Select your workspace
Use this manifest template (you can find the full version in the project repository):
{
"display_information": {
"name": "Sailor",
"description": "Your AI assistant powered by DigitalOcean GenAI",
"background_color": "#0069ff"
},
"features": {
"bot_user": {
"display_name": "Sailor",
"always_online": true
},
"slash_commands": [
{
"command": "/ask-sailor",
"description": "Ask a question to Sailor AI",
"usage_hint": "[your question]",
"should_escape": false
},
{
"command": "/sailor-summary",
"description": "Have Sailor summarize this thread",
"usage_hint": "[optional focus area]",
"should_escape": false
}
]
},
"oauth_config": {
"scopes": {
"bot": [
"app_mentions:read",
"channels:history",
"chat:write",
"commands",
"groups:history",
"im:history",
"mpim:history"
]
}
},
"settings": {
"event_subscriptions": {
"bot_events": [
"app_mention",
"message.im"
]
},
"interactivity": {
"is_enabled": true
},
"org_deploy_enabled": false,
"socket_mode_enabled": true
}
}
Review the configuration and click “Create”.
Install the app to your workspace by clicking “Install to Workspace” and “Allow”.
After installation, you’ll need to collect some important credentials:
SLACK_BOT_TOKEN
)connections:write
scope (SLACK_APP_TOKEN
)With your Slack app created, you’ll need to set up the development environment:
Clone the project repository:
git clone https://github.com/DO-Solutions/slack-digitalocean-genai-agent.git
cd slack-digitalocean-genai-agent
Set up a Python virtual environment:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Configure your environment variables:
export SLACK_BOT_TOKEN=<your-bot-token>
export SLACK_APP_TOKEN=<your-app-token>
export GENAI_API_KEY=<your-genai-api-key>
export GENAI_API_URL=<your-genai-api-url> # Append /api/v1
The GENAI_API_KEY
and GENAI_API_URL
can be obtained from the DigitalOcean GenAI Platform dashboard.
The application is structured to provide a clean separation of concerns between AI functionality and Slack integration:
The /ai
directory contains the core AI functionality:
ai_constants.py
: Defines constants used throughout the AI module/providers/__init__.py
: Contains utility functions for handling API responses and available providersThe GenAI provider enables communication with DigitalOcean’s GenAI Platform through an OpenAI-compatible API.
The application needs to store user preferences, such as their preferred AI model. Two options are provided:
/data
directoryFor local development, the file-based store works well. For production deployments, Redis provides better reliability and scalability.
Let’s look at how the chatbot implements its key features:
The bot listens for mentions using Slack’s event API and responds with AI-generated content:
@app.event("app_mention")
def handle_app_mention(event, say):
user_id = event["user"]
thread_ts = event.get("thread_ts", event["ts"])
# Get user preferred model from state store
model = state_store.get_user_preference(user_id, "model") or DEFAULT_MODEL
# Extract the text without the bot mention
text = re.sub(f"<@{app.client.auth_test()['user_id']}>", "", event["text"]).strip()
# Send request to AI provider
response = ai_provider.generate_response(text, model)
# Reply in thread
say(text=response, thread_ts=thread_ts)
The /sailor-summary
command triggers an AI-powered summary of a Slack thread:
@app.command("/sailor-summary")
def handle_summary_command(ack, command, say, client):
ack()
channel_id = command["channel_id"]
thread_ts = command["thread_ts"] if "thread_ts" in command else command["ts"]
user_id = command["user_id"]
# Get thread messages
messages = client.conversations_replies(
channel=channel_id,
ts=thread_ts
)["messages"]
# Format thread for AI
thread_text = format_thread(messages)
# Generate summary with AI
summary_prompt = f"Summarize this Slack thread: {thread_text}"
summary = ai_provider.generate_response(summary_prompt, DEFAULT_MODEL)
# Send summary as DM to user
client.chat_postMessage(
channel=user_id,
text=f"*Summary of thread:*\n\n{summary}"
)
The bot provides a home tab interface where users can select their preferred AI model:
@app.event("app_home_opened")
def update_home_tab(client, event):
user_id = event["user"]
current_model = state_store.get_user_preference(user_id, "model") or DEFAULT_MODEL
# Create home tab view with model selection
home_view = {
"type": "home",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Sailor AI Settings"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Choose your preferred AI model:"
}
},
{
"type": "actions",
"elements": [
{
"type": "radio_buttons",
"action_id": "model_selection",
"options": [
{"text": {"type": "plain_text", "text": "DigitalOcean GenAI"}, "value": "genai"},
{"text": {"type": "plain_text", "text": "OpenAI GPT-4"}, "value": "gpt-4"},
{"text": {"type": "plain_text", "text": "Anthropic Claude"}, "value": "claude"}
],
"initial_option": {"text": {"type": "plain_text", "text": model_display_name(current_model)}, "value": current_model}
}
]
}
]
}
client.views_publish(user_id=user_id, view=home_view)
Before deploying to production, it’s important to implement appropriate security measures, especially for a bot that will be processing workplace communications. DigitalOcean’s GenAI Platform offers Guardrails, a powerful security feature that helps protect sensitive information:
Guardrails act as a security layer that overrides an agent’s output when it detects sensitive or inappropriate content, adding crucial protection for enterprise environments.
While local development is great for testing, a production chatbot needs reliable hosting. DigitalOcean’s App Platform provides a managed environment that’s perfect for running your Slack bot:
SLACK_BOT_TOKEN
, SLACK_APP_TOKEN
, GENAI_API_KEY
, GENAI_API_URL
)REDIS_URL
environment variable to the connection stringOnce deployed, your bot will be available 24/7 in your Slack workspace, ready to assist your team.
With everything set up, you can now interact with your chatbot in several ways:
@Sailor
in any channel it’s been added to/ask-sailor
to interact with the bot in channels it hasn’t been added to/sailor-summary
in a thread to get an AI-generated summaryThe base implementation (found at https://github.com/DO-Solutions/slack-digitalocean-genai-agent) provides a solid foundation, but there are many ways to extend it:
While DigitalOcean GenAI is the primary focus, the template supports other AI providers:
export OPENAI_API_KEY=<your-api-key>
export ANTHROPIC_API_KEY=<your-api-key>
You can extend the bot with additional features:
You can add support for other AI providers by:
ai/providers/base_api.py
ai/providers/__init__.py
to include your implementationThe primary benefit is the ability to create AI-powered interactions within Slack without managing complex AI infrastructure. DigitalOcean’s GenAI Platform provides a streamlined path to building intelligent chatbots, making advanced AI capabilities accessible to developers of all skill levels. Additionally, it offers robust tools and integrations that simplify the development process. This allows teams to focus on creating meaningful interactions rather than dealing with the complexities of AI deployment.
The bot processes messages only when explicitly mentioned or directly messaged, and it’s configured to operate within the permissions granted during installation. For enhanced security, DigitalOcean’s GenAI Platform offers a Guardrails feature, which acts as a security component that you can attach to GenAI Platform agents.
A guardrail overrides an agent’s output when it detects sensitive or inappropriate content in the agent’s input or output. For example, it can prevent an agent from sharing login credentials or credit card information. This adds an additional layer of protection for sensitive workplace communications.
You can learn more about implementing Guardrails in the DigitalOcean documentation. Organizations concerned about sensitive data should also review the AI provider’s data processing policies and consider using private agents where possible.
Yes, the chatbot is highly customizable. You can modify the AI prompts, add new commands, integrate with additional services, or even implement custom logic for specific types of requests. The modular structure makes it easy to extend the functionality without compromising the core features.
When deployed to DigitalOcean App Platform with Redis state storage, the solution scales well for large workspaces. The architecture efficiently handles concurrent requests, and both Slack’s APIs and DigitalOcean’s infrastructure are designed for scalability. For very large deployments, consider implementing additional caching or upgrading to higher-tier App Platform plans.
Building an AI-powered Slack chatbot with DigitalOcean’s GenAI Platform provides a powerful way to enhance team collaboration and productivity. By bringing AI capabilities directly into your team’s communication channels, you create opportunities for more efficient information sharing, automated summaries, and intelligent assistance.
The example we’ve walked through demonstrates how to create a fully-functional chatbot that responds to mentions, summarizes threads, and adapts to user preferences. This foundation can be extended in countless ways to address your team’s specific needs and workflows.
By combining the accessibility of Slack with the power of DigitalOcean’s GenAI Platform, you can create AI experiences that feel like a natural extension of your team’s everyday communication.
To further enhance your understanding and capabilities with the DigitalOcean GenAI Platform, consider following the below tutorials:
Continue building with DigitalOcean Gen AI Platform and unlock the full potential of AI in your applications!
Continue building with DigitalOcean Gen AI Platform.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!