This guide takes you through building a Slack agent in Val Town, using the Vercel AI SDK. It is an updated version of Vercel’s own Slackbot Agent Guide, adapted for Val Town.
In 5 or 10 minutes, you’ll have an agent that can respond to DMs and mentions in Slack channels.
- Go to api.slack.com/apps to create a Slack app — choose “From scratch”
- Under OAuth & Permissions > Scopes > Bot Token Scopes, add the following:
app_mentions:readassistant:writechannels:historychat:writeim:historyim:write
- Under App Home > Show Tabs > Chat Tab, check “Allow users to send Slash commands and messages from the chat tab”
- Under OAuth & Permissions > OAuth Tokens, install the app to your workspace
- Remix this slack agent template val
- Add environment variables
in your val sidebar:
SLACK_BOT_TOKENin your Slack app: OAuth & Permissions > OAuth Tokens > Bot User OAuth TokenSLACK_SIGNING_SECRETin your Slack app: Basic Information > Signing SecretANTHROPIC_API_KEYfrom the Claude console
- Copy your HTTP url in
events.ts(note: you can add a custom subdomain), then in your Slack app paste it under Event Subscriptions > Request URL - In your Slack app under Events Subscription > Enable Events > Subscribe to
bot events, add
app_mention,assistant_thread_started, andmessage.im
Test it
Section titled “Test it”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.
How it works
Section titled “How it works”Directorylib
- generate-response.ts
- handle-app-mention.ts
- handle-message.ts
- handle-thread-started.ts
- slack-utils.ts
- events.ts
- prompt.md
- README.md
Dependencies
Section titled “Dependencies”- Slack’s Web API (v7.14.1)
- Hono (v4.12.0)
- Vercel’s AI SDK (v6.0.92)
Webhook handler
Section titled “Webhook handler”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 get routed based on their event type (@mention, thread started, or generic message).
The POST route verifies that webhooks are indeed coming from Slack then handles three types of events in its core try-catch loop:
app_mentionfires when you @mention your agent in a Slack channel or threadassistant_thread_startedfires when you create a new chat directly with the agent appmessagefires when you send a message in an agent app chat
App mentions
Section titled “App mentions”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:
- Checks if the message is from a bot to avoid infinite response loops
- Creates a status updater to show the bot is “thinking”
- If the mention is in a thread, it retrieves the thread history
- Calls the LLM with the message content
Assistant thread messages
Section titled “Assistant thread messages”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:
- Your agent sends a welcome message to the thread
- 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:
- Ignore messages from the agent (bot) itself
- Update the status ahead of the LLM call
- Retrieve the conversation history
- Call the LLM with the conversation context
- Post the LLM’s response to the thread
LLM calls
Section titled “LLM calls”The core AI parts of your agent live in generate-response.ts:
- Loads your system prompt
- Uses the Vercel AI SDK’s
generateTextfunction with Anthropic’sclaude-sonnet-4.6model - Passes tools to the LLM: web search
- Formats the response for Slack’s markdown format
Make it your own
Section titled “Make it your own”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, like we’ve done with this demo townie val. Or you could create a customer support bot, like this template for Pylon. Feel free to reach out in our Discord server for help or to share what you’ve made.