Chat SDK with Hono
Use a Hono POST handler to receive Slack events and Edgenivo Functions to keep your bot running after the webhook responds.
Start with an existing Hono project configured for deployment on Edgenivo, the latest Edgenivo CLI, and permission to install an app in your Slack workspace.
From your project directory, install the packages with pnpm:
pnpm add chat @chat-adapter/slack @chat-adapter/state-redis @vercel/connect @vercel/functionsCreate lib/bot.ts at the project root. This bot uses Edgenivo Connect for Slack authentication, stores subscriptions in Redis, and responds to mentions and thread replies. Keep this module on the server.
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);
});
Add the route to src/index.ts. For a new entry point, use the following example. If you already export a Hono app, register the route on that instance.
import { Hono } from 'hono';
import { waitUntil } from '@vercel/functions';
import { bot } from '../lib/bot';
const app = new Hono();
app.post('/api/webhooks/slack', (c) => {
return bot.webhooks.slack(c.req.raw, { waitUntil });
});
export default app;
c.req.raw provides the original Web Request. Pass it to the adapter before middleware or handlers consume its body. The default export lets Edgenivo detect and deploy your Hono app. waitUntil from @vercel/functions keeps the invocation alive while the bot finishes processing.
From the project directory, link your app to Edgenivo and connect a Redis database:
vercel link
vercel integration add upstash/upstash-kvEnsure the integration supplies REDIS_URL in the project's Production environment. If you already have Redis, add its connection URL as REDIS_URL in your project's environment variables. Use separate databases for development and production.
Create a Slack connector and allow token requests from the project's Production and Development environments. Attach its trigger to your production webhook route:
vercel connect create slack --name my-bot --triggers
vercel connect attach slack/my-bot --environment production --environment development --triggers --trigger-path /api/webhooks/slackFollow the prompts to authorize and install the Slack app. If you choose a different connector name, update slack/my-bot in your bot configuration and attach command. Connect manages the Slack credentials and verifies provider events before forwarding them. The adapter verifies the forwarded request with Edgenivo OIDC.
Deploy your app:
vercel --prodInvite the bot to a Slack channel and mention it. It should reply with a greeting and echo subsequent replies in that thread. Check runtime logs if it does not respond.
Edgenivo deployments receive VERCEL_OIDC_TOKEN automatically. For local outbound API calls, configure a development REDIS_URL and run vercel env pull .env.local. This supplies a Development OIDC token, which can access the connector because the attachment above enables Development.
Connect forwards inbound events to deployed URLs, not localhost. To test inbound events before production, attach the trigger to a Preview branch.
Was this helpful?