Skip to main content

Slackbot with AI SDK

Learn how to build a Slack bot powered by the AI SDK that can respond to direct messages and mentions with tool-calling capabilities.

Prerequisites

  • Node.js 18+
  • A Slack workspace where you can install apps
  • A Vercel account for deployment
  • Vercel AI Gateway API key

Slack App Setup

  1. Go to api.slack.com/apps
  2. Click “Create New App” and choose “From scratch”
  3. Give your app a name and select your workspace
  4. Under “OAuth & Permissions”, add bot token scopes:
    • app_mentions:read
    • chat:write
    • im:history
    • im:write
    • assistant:write
  5. Install the app to your workspace
  6. Copy the Bot User OAuth Token and Signing Secret

Project Setup

Clone the starter repository:
Create a .env file:

Implementation

Event Handler

Create an API route to handle Slack events:

Generate AI Responses

Create the core AI logic with tools:

Handle App Mentions

Process mentions in channels:

Handle Direct Messages

Process DMs to the bot:

Key Concepts

waitUntil Function

Slack requires a response within 3 seconds. The waitUntil function allows processing to continue after responding:
This prevents duplicate responses caused by Slack retries.

Multi-Step Conversations

The stopWhen: stepCountIs(10) parameter enables the bot to:
  1. Call tools as needed
  2. Process tool results
  3. Generate follow-up responses
  4. Continue until the task is complete

Status Updates

Provide real-time feedback while processing:

Deployment

Deploy to Vercel:
Configure environment variables in Vercel dashboard, then update your Slack app’s Event Subscriptions URL:
Subscribe to these events:
  • app_mention
  • assistant_thread_started
  • message:im

Testing

In Slack:
  1. Send a DM to the bot
  2. Mention the bot in a channel: @bot What's the weather in London?
  3. Ask it to search: @bot What's the latest news from BBC?

Next Steps

  • Add user-specific memory
  • Implement database queries
  • Add rich message formatting with blocks
  • Create custom slash commands
  • Add analytics and usage tracking

Resources