Back to templates

Template details

OpenAI Slackbot with Node.js

Build an AI-powered Slackbot with Node.js and Vercel Functions that answers questions using OpenAI's GPT model.

About this template

This template creates an AI-powered Slackbot built with Node.js and deployed as a serverless function on Vercel, letting users ask questions to OpenAI's GPT model directly from Slack.

What is the OpenAI Slackbot with Node.js template?

This is a boilerplate project that connects Slack to the OpenAI API. It listens for Slack Events API events (app_mention and channel_created) via a serverless endpoint at /api/events, sends the message text to OpenAI, and posts the model's answer back to the Slack channel. It comes from a curated examples repository of robust and scalable application patterns, and requires four environment variables (OPENAI_API_KEY, SLACK_BOT_TOKEN, SLACK_SIGNING_SECRET, SLACK_ADMIN_MEMBER_ID) to run locally or on Vercel.

Key Features

  • Serverless endpoint — a single Vercel serverless function at /api/events handles Slack Event Subscriptions and is verified against the SLACK_SIGNING_SECRET.
  • OpenAI integration — uses the OPENAI_API_KEY to send the user's message to GPT and return the answer, with no additional backend servers.
  • Slack OAuth scopes — requires app_mentions:read, channels:history, chat:write, and commands so the bot can read mentions, see channel history, and write messages.
  • Admin member ID — includes SLACK_ADMIN_MEMBER_ID as an environment variable so the bot can identify a specific Slack admin user, enabling access control.
  • Vercel deployment — secrets are stored in Vercel environment variables; the app is deployed by connecting the repository and setting the Request URL to https://your-vercel-app.vercel.app/api/events.
  • Local development — supports running with vercel dev --listen 3000 and exposing a public URL through localtunnel for Slack event delivery.
  • Troubleshooting via logs — when the Request URL fails verification, the README points to Vercel runtime logs for debugging.

Who is it for?

  • Node.js developers who want a minimal starter for building Slack apps with serverless functions.
  • Slack workspace admins who need an AI assistant that answers questions in channels using their own OpenAI API key.
  • Vercel users looking for an example of an event-driven bot that uses environment variables and API routes.

What can you do with it?

  • Create a Q&A assistant: Set up the bot in a Slack workspace and have it respond to @-mentions with GPT-generated answers.
  • Automate channel monitoring: Subscribe to channel_created events to trigger actions when new channels are created.
  • Prototype GPT workflows: Use the bot as a starting point for adding more commands (the commands scope is already configured).

How does it work?

  1. Create a Slack app and add the required OAuth scopes.
  2. Install the app to your workspace and copy the bot token, signing secret, and your member ID into environment variables.
  3. Deploy to Vercel and set the Request URL in Slack Event Subscriptions to the /api/events endpoint.
  4. Subscribe to app_mention and channel_created bot events, then reinstall the app.
  5. When someone mentions the bot, Slack sends an event to the endpoint, which calls OpenAI and replies with the generated text.

FAQ

What environment variables does this Slackbot require?

The template needs four variables: OPENAI_API_KEY for the OpenAI API, SLACK_BOT_TOKEN for Slack authentication, SLACK_SIGNING_SECRET to verify incoming requests, and SLACK_ADMIN_MEMBER_ID to identify a specific admin user. All are added in Vercel's environment settings.

How do I get the Slack bot token?

In Slack's app configuration, go to OAuth & Permissions, add the bot scopes app_mentions:read, channels:history, chat:write, and commands, then click "Install to Workplace". The OAuth flow returns a Bot User OAuth Token that starts with xoxb-.

How do I enable Slack events for this bot?

After deploying, open your Slack app's Event Subscriptions page, turn on events, and set the Request URL to https://your-vercel-app.vercel.app/api/events. Subscribe to the bot events app_mention and channel_created, save changes, and reinstall the app.

Can I run this Slackbot locally?

Yes. Install the Vercel CLI, run vercel dev --listen 3000, then use localtunnel to expose that port as a public URL. Update the Slack Request URL to the localtunnel address. Be aware that Slack will verify the URL, so the tunnel must be active while testing.