Edit on val.town ↗

Build your own Slack agent

This guide takes you through building a Slack agent in Val Town, using the Vercel AI SDK. In 5 or 10 minutes, you'll have an agent that can respond to DMs and mentions in Slack channels.

  1. Remix our ai-slackbot template
  2. Fill out your Slack app name and bot name on the remixed val, click Create Slack App, and follow the steps to install in your Slack workspace
  3. Add environment variables in your remixed val:

At this point, you should already be able to chat with your agent in Slack via @mention in channels, or directly via the Slack app's chat interface. Next, we'll walk through how the code works so you can make it your own.

Our ai-slackbot template uses a Slack app manifest to speed up configuration. You can also create a Slack app from scratch and manually select which scopes your bot has access to and which events it subscribes to. The template adds the following scopes:

  1. app_mentions:read
  2. assistant:write
  3. channels:history
  4. groups:history
  5. chat:write
  6. im:history
  7. im:write

And subscribes to these events:

  1. app_mention
  2. assistant_thread_started
  3. message.im

You may want to edit your remixed val's lib/slack.manifest.ts file, which also includes configuration like suggested prompts for Slack users of your app.

The ai-slackbot template is built using Hono for routing, Vercel's AI SDK for chatbot functionality, and Slack's Web API.

The events.ts file exposes an API from the val's HTTP URL using Hono. The POST /events route is where Slack webhooks come in, and the POST /process route handles each based on its type (@mention, thread started, or generic message). It also verifies that webhooks are indeed coming from Slack. The event types are:

  1. app_mention: fires when you @mention your agent in a Slack channel or thread
  2. assistant_thread_started: fires when you create a new chat directly with the agent app
  3. message: fires when you send a message in an agent app chat

Note Slack expects a response within 3 seconds to confirm the request is being handled. It'll retry automatically if a response takes too long. Since LLM inference can take longer, we use a fire-and-forget pattern that responds to the HTTP request immediately then continues with the LLM call and sends it back to Slack when done.

When you mention your bot in a channel or channel thread, the app_mention event is triggered, which we handle in handle-app-mention.ts:

  1. Checks if the message is from a bot to avoid infinite response loops
  2. Creates a status updater to show the bot is "thinking"
  3. If the mention is in a thread, it retrieves the thread history
  4. Calls the LLM with the message content

When you start a thread with your assistant in its dedicated Slack app chat interface, the assistant_thread_started event is triggered, which we handle in handle-thread-started.ts:

  1. Your agent sends a welcome message to the thread
  2. Sets up suggested prompts to help users get started

You should tailor these, of course, based on your needs.

For direct messages to your bot in its dedicated Slack app chat interface, the message event is triggered, which we handle in handle-message.ts:

  1. Ignore messages from the agent (bot) itself
  2. Update the status ahead of the LLM call
  3. Retrieve the conversation history
  4. Call the LLM with the conversation context
  5. Post the LLM's response to the thread

The core AI parts of your agent live in generate-response.ts:

  1. Loads your system prompt
  2. Uses the Vercel AI SDK's generateText function with Anthropic's claude-sonnet-5 model
  3. Passes tools to the LLM: web search
  4. Formats the response for Slack's markdown format

We've set up this val template and guide with web search as its only tool, but the goal is for you to make it your own! You could connect to the Val Town MCP server so that your agent can build and run your vals from Slack. Or you could create a customer support bot, like this template for Pylon. You could refactor the val to use other language models, either swapping the model id to stay in the Anthropic family (e.g. claude-fable-5-1), or using another provider or gateway like OpenRouter.

If you're building a Slack agent for your team in Val Town, we'd be happy to set up a Slack connect channel to demo and lend a hand!

Last updated