
Capabilities
- WhatsApp messaging
- Timed sequences
- Number validation
- QR session
Backend
WhatsApp Bot
CompletedWhen someone leaves their details on the lead capture landing, this bot messages them on WhatsApp within seconds with a welcome sequence: introduction, material and call to action, spaced out so the conversation does not read as an automated dump. It is a NestJS API with no frontend that drives WhatsApp Web through whatsapp-web.js on a headless Chromium, keeping the paired session on disk so it survives container restarts.
The challenge
The sequence takes over half a minute to send because every message waits ten seconds, and the caller is a web form that cannot sit there waiting. The HTTP response and the sending had to stop being the same operation.
Implementation
Answer before sending
The endpoint validates, checks the number and returns a 202 with what it queued; the sequence starts afterwards, outside the request cycle. The form gets its confirmation immediately and nobody watches a spinner while the messages go out.
Check the number exists on WhatsApp
Before queueing anything it asks WhatsApp whether that phone is registered, retrying once after a second before giving up: the first attempt sometimes fails while the session is still coming up. A mistyped number gets a 404 instead of vanishing into nothing.
One phone number, many ways to type it
The data comes from a form, so the number may arrive with spaces, dashes or a duplicated country code. It is normalised down to digits, the repeated prefix is stripped and the chat identifier WhatsApp expects is rebuilt.
A session that survives deployment
Pairing happens once by reading a QR from the logs and is then stored on disk, so restarting the container does not force a rescan. The client starts and shuts down with the Nest module lifecycle.