Slack Bot Integration
Build an AI-powered Slack bot using Antbase and the Bolt framework. Add intelligent AI responses to channels and DMs with automatic model routing and failover.
What this enables
- AI assistant in Slack channels and direct messages
- Thread-based conversations with context memory
- Slash commands for specific AI tasks
- App mentions (@bot) for team-wide AI access
Quick Start
1. Create a Slack app
Go to api.slack.com/apps, create a new app, enable Socket Mode, and add these bot token scopes: app_mentions:read, chat:write, im:history.
2. Install dependencies
npm init -y
npm install @slack/bolt openai3. Create the bot
// bot.js
import { App } from '@slack/bolt';
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.ANTBASE_API_KEY,
baseURL: 'https://antbase.ai/v1',
});
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
});
// Respond to app mentions
app.event('app_mention', async ({ event, say }) => {
const text = event.text.replace(/<@[A-Z0-9]+>/g, '').trim();
try {
const response = await openai.chat.completions.create({
model: 'ant:auto',
messages: [
{ role: 'system', content: 'You are a helpful Slack bot. Keep responses concise and use Slack markdown formatting.' },
{ role: 'user', content: text },
],
max_tokens: 1024,
});
await say({
text: response.choices[0].message.content,
thread_ts: event.thread_ts || event.ts,
});
} catch (error) {
await say({
text: 'Sorry, I encountered an error. Please try again.',
thread_ts: event.thread_ts || event.ts,
});
}
});
// Respond to DMs
app.message(async ({ message, say }) => {
if (message.channel_type !== 'im') return;
if (message.bot_id) return;
const response = await openai.chat.completions.create({
model: 'ant:auto',
messages: [
{ role: 'system', content: 'You are a helpful Slack assistant.' },
{ role: 'user', content: message.text },
],
max_tokens: 1024,
});
await say(response.choices[0].message.content);
});
(async () => {
await app.start();
console.log('Slack bot is running!');
})();4. Run it
export ANTBASE_API_KEY=your-ant-api-key
export SLACK_BOT_TOKEN=xoxb-your-bot-token
export SLACK_SIGNING_SECRET=your-signing-secret
export SLACK_APP_TOKEN=xapp-your-app-token
node bot.jsConfiguration
Add slash commands for specific AI tasks:
// /summarize command
app.command('/summarize', async ({ command, ack, respond }) => {
await ack();
const response = await openai.chat.completions.create({
model: 'ant:fast',
messages: [
{ role: 'system', content: 'Summarize the following text in 2-3 bullet points.' },
{ role: 'user', content: command.text },
],
max_tokens: 512,
});
await respond(response.choices[0].message.content);
});
// /code command
app.command('/code', async ({ command, ack, respond }) => {
await ack();
const response = await openai.chat.completions.create({
model: 'ant:coder',
messages: [
{ role: 'system', content: 'Generate code based on the request. Use code blocks with language tags.' },
{ role: 'user', content: command.text },
],
max_tokens: 2048,
});
await respond(response.choices[0].message.content);
});Virtual Models
| Model | Best for | Notes |
|---|---|---|
| ant:auto | General chat | Best default for Slack bots |
| ant:fast | Summarization, triage | Quick responses for busy channels |
| ant:best | Complex analysis | Frontier models for detailed answers |
| ant:free | Development | Zero cost while building |
| ant:reasoning | Problem solving | Chain-of-thought reasoning |
| ant:coder | Code generation | For /code slash command |
See Virtual Models for the full list.
Troubleshooting
Bot not responding to mentions
Make sure app_mentions:read scope is added and the app is subscribed to the app_mention event in the Event Subscriptions page.
Slash command timeout
Slack requires a response within 3 seconds. Call ack() immediately, then use respond() for the AI response (which has a 30-minute window).