IntegrationsIntermediate

How to Build a Slack AI Bot With Bolt for Node

Use Slack Bolt and Socket Mode to answer app mentions with AI, no public URL required.

10 minIntermediate

Slack's Bolt framework plus Socket Mode lets you build a bot that responds to mentions without exposing a public endpoint. That makes it perfect for testing from your laptop. Here you will create a Slack app, grab the two tokens it needs, and answer @-mentions with an LLM.

What you need

  • A Slack workspace where you can install apps
  • Node 18+ installed
  • An AI API key
  • Permission to create a Slack app at api.slack.com/apps

Step 1: Create the Slack app and enable Socket Mode

At api.slack.com/apps click Create New App, choose From scratch, and pick your workspace. Under Socket Mode, flip it on and generate an app-level token with the connections:write scope. This token starts with xapp-.

Slack API - Basic Information
Socket Mode .......... Enabled
App-Level Token xapp-1-A0..... scope: connections:write
Bot User OAuth Token xoxb-............
Socket Mode means you do not need a public request URL.

Step 2: Add scopes and subscribe to events

Under OAuth & Permissions add the bot scopes app_mentions:read and chat:write. Under Event Subscriptions, turn events on and subscribe the bot to app_mention. Then click Install to Workspace and copy the Bot User OAuth token that starts with xoxb-.

Two tokens, two jobs
The xapp- token opens the Socket Mode connection. The xoxb- token authorizes API calls like posting messages. You need both.

Step 3: Install Bolt and write the app

zsh - slack-ai-bot
$npm install @slack/bolt openai dotenv
added 53 packages in 3s
$
app.js
import "dotenv/config";
import pkg from "@slack/bolt";
import OpenAI from "openai";

const { App } = pkg;
const ai = new OpenAI();

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  appToken: process.env.SLACK_APP_TOKEN,
  socketMode: true,
});

app.event("app_mention", async ({ event, say }) => {
  const prompt = event.text.replace(/<@[^>]+>/g, "").trim();

  const res = await ai.chat.completions.create({
    model: "gpt-5-mini",
    messages: [{ role: "user", content: prompt }],
  });

  await say({
    text: res.choices[0].message.content,
    thread_ts: event.ts,
  });
});

await app.start();
console.log("Slack AI bot is running");
Reply in a thread
Passing thread_ts keeps the answer attached to the original message so busy channels stay readable.

Step 4: Run and mention it

zsh - slack-ai-bot
$node app.js
Slack AI bot is running
$
#help
You
@aihelper summarize our refund policy in one line
Agent
Refunds are available within 30 days of purchase for unused items with proof of order.
The bot replies in a thread under your mention.

Result

Your Slack workspace now has an AI teammate that answers when mentioned, all without a single inbound webhook. To go to production, deploy the same file to a server and keep the process alive with pm2 or a systemd unit.

Watch related tutorials

Tags
#slack#bolt#socket-mode#bot#ai