Support Chat
A self-hosted live chat in place of Crisp or Intercom: a bubble on every page, an inbox at /admin/support, email and Telegram alerts, and replies straight from Telegram.
Why this matters
Crisp and Intercom charge per seat and keep your visitors' messages on their servers. The boilerplate ships its own chat instead: messages live in your Supabase database, and there is no third-party account to sign up for.
- A floating bubble on every page. Signed-in users chat as themselves. Everyone else gets an anonymous session and is asked for an email after their first message, so a reply can still reach them once they close the tab.
- An inbox at
/admin/support. Conversations on the left, the thread on the right, live over Supabase Realtime, with typing indicators, image attachments, and edit or delete on your own replies. - Email and Telegram alerts when a visitor is waiting.
- Reply from Telegram. Swipe-reply to an alert and your message lands in that visitor's chat.
- An optional rating prompt (1–5 stars) that points happy visitors at your review pages.
An admin never sees the bubble. They get a shortcut to the inbox with a live unread badge, and their presence on the site is what makes the visitor's widget say "Online now".
Turn it on
Added in 3.9.0. Nothing to sign up for.
1. Make sure the database has the tables.
- New project (created from
supabase/schema.sql): already done. - Existing project: open the Supabase SQL Editor, paste the whole of
supabase/migrations/0018_add_support_chat.sql, and Run. It answers Success. No rows returned. The file is idempotent, so running it twice is harmless, andpnpm db:statustells you if it is still pending.
If Storage has never been enabled on the project, the bucket part prints a notice instead of failing: enable Dashboard → Storage and run the migration again. Skip the migration altogether and the inbox shows an amber panel naming the file to run, not an empty list.
2. Check the flag.
// config/features.config.ts
supportChat: true,A fresh install ships with it on. If you upgraded an existing project, your own features.config.ts survived the merge without the flag, which means off, so add the line yourself. See Features Config.
That is all it needs. The widget appears, Support appears in the admin sidebar, and every visitor message is emailed to siteConfig.contact.supportEmail as it lands. Everything below is optional.
The upstream default stays false for existing projects, so updating the codebase never puts a chat bubble on your live site pointed at tables your database doesn't have yet.
Anonymous visitors
For visitors who aren't signed in, turn on Supabase → Authentication → Sign In / Providers → Allow anonymous sign-ins.
The anonymous session lives on its own Supabase client with its own storage key, so the rest of the app never mistakes it for a real login.
Anonymous users take the authenticated role. The boilerplate's own policies are all scoped by auth.uid() or is_admin_user(), but if you added policies of your own, check none of them is USING (true).
Turn on a captcha under Authentication → Attack Protection before real traffic arrives. Without one, a bot can mint sessions and inflate your monthly active users.
Rather not allow it? Switch off Chat without signing in in the settings (or set allowAnonymous: false in config/support.config.ts) and the widget asks visitors to sign in instead.
Settings — no deploy needed
Admin → Support → Settings is where whoever answers support changes the chat, without a developer:
- Avatar — your logo or a photo of the person answering. Without one the chat shows the first letter of the name.
- Name and tagline — the widget header. While an admin has the site open, the tagline reads "Online now".
- Greeting and away notice — the empty-thread copy, and what shows if nobody picks the conversation up.
- Notification email — empty means the site's own support address.
- Notification delay — see How alerts are timed.
- Chat without signing in, ratings, and the review links offered after a 4–5 star rating.
Saved values win over config/support.config.ts, which is only what a fresh install starts from. That file is protected by .gitattributes, so your version survives every upgrade. The few technical limits not worth a form field (offlineThresholdMs, visitorEmailDebounceMs, attachments per message) live only there.
The inbox
Admin → Support → Inbox. Conversations on the left with the last message, time and an unread badge; the thread on the right; a reply box underneath. A visitor writing while you have it open appears without a refresh, and so does their "typing…".
Hover your own reply to edit or delete it. Deleting removes it from the visitor's chat too, and says so if it already went out by email.
Request rating drops a star card into the conversation. The answer shows in the thread, on the conversation header and in the list.
Reply from Telegram
Worth ten minutes: it lets you answer a visitor from your phone. It uses the same bot as the Telegram Admin Bot, so if you already set that up, you are done.
1. Create the bot. Open @BotFather, press Open next to the message box to bring up its menu, and choose Create a New Bot. It answers with a token like 123456:AA….
Use the menu rather than typing /newbot: the command flow leaves the token in the chat history for good. Treat the token like a password.
2. Find your chat id. Send your bot any message, then open https://api.telegram.org/bot<TOKEN>/getUpdates and read result[0].message.chat.id. Several admins go in comma-separated; each gets the alerts and each can reply.
3. Set the variables in your host and redeploy:
TELEGRAM_BOT_TOKEN=123456:AA…
TELEGRAM_ADMIN_CHAT_ID=123456789
TELEGRAM_WEBHOOK_SECRET=… # openssl rand -hex 324. Register the webhook once, with your own values:
curl -X POST "https://api.telegram.org/bot<TOKEN>/setWebhook" \
-H "Content-Type: application/json" \
-d '{"url":"https://yourdomain.com/api/telegram/webhook","secret_token":"<TELEGRAM_WEBHOOK_SECRET>"}'It answers {"ok":true,...}. The URL must be your deployed site; Telegram cannot reach localhost.
Each waiting conversation gets one alert, carrying only what you haven't read yet:
💬 New support message — marcus@webbplumbing.com
Marcus Webb: I paid for the Premium listing but the badge hasn't appeared yet
Reply to this message to answer Marcus Webb.Swipe-reply and type. The bot confirms what happened, for example "Sent to Marcus Webb — they're away, so it also went out by email." Replying to that confirmation continues the same conversation. Text only; images go through the admin panel.
A reply is matched to a conversation by the id of the alert you replied to. A reply the bot can't map is refused rather than guessed at, because guessing would eventually send one visitor's answer to another.
How alerts are timed
Out of the box: immediately. The notification delay ships as 0, so a visitor message becomes an email (and a Telegram alert, if configured) the moment it lands. No cron involved. Emails for one conversation keep one subject and thread together in your mail client.
Optional: batch them. Set a delay and a message is held instead. If you reply or open the conversation inside that window, nothing is sent at all. This mode is driven by /api/cron/support-notify, which is not scheduled by default. To use it, add it to vercel.json:
{ "path": "/api/cron/support-notify", "schedule": "* * * * *" }Vercel Hobby runs crons once a day and rejects a per-minute schedule. A delay whose cron never fires means you never hear that a visitor is waiting, which is why the boilerplate doesn't ship this entry. CRON_SECRET must be set too. See Cron Jobs.
Replies to the visitor are emailed only once they have left the chat, and at most one email per visitorEmailDebounceMs. While the chat is open they just see the reply appear.
Check it works
- Open your site in a private window, so you aren't signed in as the admin. The bubble is bottom-right.
- Send a message and leave your email in the card that follows.
- In your normal window, open Admin → Support: the conversation is there with an unread badge. Check your email and Telegram for the alert.
- Reply, and watch it appear in the private window without a refresh.
- Close the private window and reply again. This time the visitor gets an email.
Troubleshooting
| What you see | Fix |
|---|---|
| No bubble anywhere | Set supportChat: true. An admin never sees the bubble, so check in a private window. |
| Amber "tables aren't in your database yet" panel | Run migration 0018. |
| "Please sign in to start a chat" for a logged-out visitor | Turn on anonymous sign-ins in Supabase, or Chat without signing in in the settings. |
| Messages only appear after a refresh | In Supabase → Database → Replication, support_messages and support_conversations must be in the supabase_realtime publication. If you edited the Content Security Policy, connect-src needs the wss:// Supabase origin. |
| No email when a visitor writes | Set RESEND_API_KEY. If the notification delay is above 0, you need the cron, or set it back to 0. |
| Emails arrive, Telegram doesn't | TELEGRAM_BOT_TOKEN or TELEGRAM_ADMIN_CHAT_ID is unset. |
| Alerts arrive but replying does nothing | The webhook isn't registered, or its secret_token doesn't match TELEGRAM_WEBHOOK_SECRET. |
| Attachments show "Image unavailable" | Storage wasn't enabled when the migration ran. Enable it and run 0018 again. |
Security
- Clients can only read.
support_messagesandsupport_conversationshave no insert, update or delete policy. Every write goes through/api/support/sendor aSECURITY DEFINERfunction, so a visitor cannot pose as the admin or write into someone else's conversation. - A visitor reads only their own conversation;
is_admin_user()opens the rest. - Attachments live in a private bucket behind short-lived signed URLs.
/api/support/sendis rate limited to 30 messages a minute.- The Telegram webhook requires the shared secret and a chat on the
TELEGRAM_ADMIN_CHAT_IDallowlist.
Open the chat from your own UI
import { openSupportChat } from "@/lib/support/open-chat"
<button onClick={openSupportChat}>Talk to us</button>If a page has a sticky bottom bar, lift the bubble above it with useSupportBubbleOffset(barElement) from hooks/useSupportBubbleOffset.ts.
Key files
| Path | What it is |
|---|---|
config/support.config.ts | Defaults for a fresh install |
lib/support/settings.ts | Admin settings layered over those defaults |
supabase/migrations/0018_add_support_chat.sql | Tables, RPCs, RLS policies, private bucket |
app/api/support/send/route.ts | The one write endpoint |
app/api/cron/support-notify/route.ts | The batching notifier (opt-in) |
app/api/telegram/webhook/route.ts | Telegram commands and support replies |
components/support/ | Widget, composer, thread, rating cards |
app/(admin)/admin/support/page.tsx | The Inbox and Settings tabs |