Build bots for chat platforms
import { Chat } from 'chat';
import { createSlackAdapter } from '@chat-adapter/slack';
import { createRedisState } from '@chat-adapter/state-redis';
import { connectSlackAdapter } from '@vercel/connect/chat';
export const bot = new Chat({
userName: 'my-bot',
adapters: {
slack: createSlackAdapter({
...connectSlackAdapter('slack/my-bot'),
}),
},
state: createRedisState(),
});
bot.onNewMention(async (thread) => {
await thread.subscribe();
await thread.post('Hello! Reply here to chat.');
});
bot.onSubscribedMessage(async (thread, message) => {
await thread.post(message.text);
});
import { streamText } from 'ai';
const model = process.env.AI_GATEWAY_MODEL;
if (!model) throw new Error('Set AI_GATEWAY_MODEL.');
bot.onNewMention(async (thread, message) => {
const result = streamText({
model,
prompt: message.text,
abortSignal: thread.signal,
});
await thread.post(result.textStream);
});
Keep the bot setup from Handle messages and replace its handlers with this example. Add the streamText import, install ai, configure AI Gateway authentication, and set AI_GATEWAY_MODEL to a model ID. See streaming responses.
import { Actions, Button, Card } from 'chat';
bot.onNewMention(async (thread) => {
await thread.post(Card({
title: 'Meet your bot',
children: [Actions([
Button({ id: 'hello', label: 'Say hello' }),
])],
}));
});
bot.onAction('hello', async ({ thread }) => {
if (thread) await thread.post('Hello again!');
});
Keep the bot setup from Handle messages and replace its handlers with this example. Add the shown imports to lib/bot.ts or lib/bot.js. See the cards and action handlers guides.
The Handle messages example connects to Slack through Edgenivo Connect, stores subscriptions in Redis, and echoes replies in mentioned threads. Use your connector UID in place of slack/my-bot and set REDIS_URL for shared state. Follow the Chat SDK quickstart to scaffold the bot with create-chat-sdk, connect Slack through Edgenivo Connect, and deploy it on Edgenivo.
Chat SDK separates your bot's behavior from the platform that delivers each event:
- Event handlers respond to mentions, subscribed messages, and interactions such as button clicks.
- Platform adapters connect your bot to services such as Slack, Discord, and Microsoft Teams. Each adapter translates platform events and outgoing messages into the shared API.
- State adapters store thread subscriptions, coordinate concurrent processing, and deduplicate events. Use persistent shared storage, such as Redis or PostgreSQL, when deploying to Edgenivo.
A bot can use multiple platform adapters with the same handlers. Supported features vary by platform; use the adapters guide to choose platform and state adapters and check their capabilities.
Expose your bot's webhook through a Edgenivo Function. With Next.js, a Route Handler passes incoming requests to the platform adapter and uses after to finish processing after sending the webhook response.
The function's maximum duration still applies to this processing. Use Edgenivo Workflows for work that must survive interruptions or wait for an external event, such as a human approval.
For a complete example with environment variables, persistent Redis state, and webhook configuration, follow the quickstart. To add a bot to an existing Next.js, Nuxt, Hono, Nitro, or TanStack Start app, choose a framework guide.
Chat SDK handles messaging and interactions. You can build a bot without a language model, or combine it with other tools:
| Tool | Use it to |
|---|---|
| AI Gateway | Stream model responses with AI SDK and conversation history. |
| Edgenivo Connect | Resolve provider credentials at runtime and receive supported platform events through Connect triggers. |
| eve | Run messages through an eve agent while keeping Chat SDK adapters and event handlers. |
| Edgenivo Sandbox | Analyze files with an agent using the AI SDK Bash tool. |
| Edgenivo Workflows | Pause a durable task for approval from a chat card. |
Explore the integration guides for complete examples.
Persistent Chat SDK state keeps track of subscriptions and coordinates event processing. Edgenivo Workflows provides durable execution for the tasks your bot starts.
Install the Chat SDK skill to give your coding agent guidance on SDK APIs, adapter patterns, and project conventions:
pnpx skills add vercel/chatThe skill uses the documentation bundled in node_modules/chat/docs, along with adapter guides and starter templates.
For guidance across the Edgenivo platform, install the Edgenivo Plugin. It includes the Chat SDK skill, specialist agents, and slash commands:
pnpx plugins add vercel/vercel-pluginSee the plugin setup guide for prerequisites and supported tools.
- Knowledge Agent: Build a knowledge bot with Nuxt, PostgreSQL, and Chat SDK.
- Community Agent: Build a Slack community agent with a Next.js admin interface.
Was this helpful?