A multi-tenant WhatsApp automation platform for service businesses, combining conversational AI, appointment scheduling, payment initiation, and human handoff.
ELJIKA AI connects Meta's WhatsApp Cloud API to a tenant-aware backend that can understand customer requests, check real availability, hold appointments, request M-Pesa payments through Paystack, persist conversations, and escalate uncertain or sensitive cases to a human team.
- Multi-tenant architecture: routes each webhook by Meta's
phone_number_idinstead of assuming one shared WhatsApp account. - Conversational booking: Groq tool calling resolves services, appointment types, dates, times, and customer details.
- Conflict-safe scheduling: availability checks account for business hours, open days, notice periods, technicians, service duration, house-call travel buffers, active holds, and confirmed appointments.
- Temporary booking holds: unpaid slots can be held for a configurable period and are replaced if the customer creates another hold.
- M-Pesa payment initiation: creates a Paystack mobile-money charge and confirms the booking only after a signed
charge.successwebhook. - Persistent memory: PostgreSQL stores businesses, WhatsApp accounts, customers, conversations, messages, services, and bookings.
- Human escalation: pauses AI responses, records the reason and summary, alerts a configured owner on WhatsApp, and automatically resumes after three hours.
- Business-specific AI instructions: each tenant can override its assistant's rules without changing application code.
- Onboarding workflow: an interactive CLI provisions a business, services, WhatsApp credentials, hours, pricing, staff capacity, and escalation settings.
- Operational endpoints: health check and tenant-scoped booking administration.
Customer WhatsApp
|
v
Meta WhatsApp Cloud API webhook
|
v
Express webhook server
|
+--> Resolve tenant by phone_number_id
+--> Load customer and conversation history
+--> Build a tenant-specific Groq system prompt
+--> Execute AI function tools
|
+--> Check availability
+--> Create/hold a booking
+--> Escalate to a human
|
v
PostgreSQL / Prisma
|
+--> Paystack creates an M-Pesa charge
+--> Signed Paystack webhook confirms the booking
+--> WhatsApp confirmation is sent to the customer
- Groq-hosted
openai/gpt-oss-20bmodel - Structured function/tool calling
- Tenants receive a system prompt containing their services, prices, location, hours, calendar, booking policy, and custom instructions
- The latest 16 conversation messages are loaded for context
- Both user and assistant messages are persisted
- Rate-limit responses are parsed and retried with an appropriate delay
- Safe fallback responses are sent when the AI provider is unavailable
- Date and time validation
- Business timezone handling using Africa/Nairobi without DST ambiguity
- Configurable open days, opening/closing times, slot increments, and minimum notice
- Multiple simultaneous technicians
- Combined duration calculations for multi-service appointments
- House-call fees and travel-time buffers
- Concurrent active booking and hold awareness
- Automatic replacement of an existing unpaid hold for the same customer
- Tenant-scoped booking queries and availability checks
- A WhatsApp account belongs to exactly one business
- Meta webhook payloads map to businesses through
phone_number_id - Customers and conversations are unique per
(businessId, phone)or(businessId, customerId) - Every booking, customer, conversation, and WhatsApp lookup is scoped to a business
- A short in-memory tenant configuration cache reduces database reads
- Credentials are resolved by the WhatsApp service and are not passed to the AI
- Paystack M-Pesa Charge API integration
- Paystack's signed raw webhook payload is validated with HMAC-SHA512
- Successful payments confirm the booking and notify the customer on WhatsApp
- Booking metadata ties payment events to the correct tenant and booking
- Test-mode support allows charging Paystack's designated test MSISDN
- Escalations stop automatic replies for the affected conversation
- Reasons and summaries are stored with escalation timestamps
- The business owner receives a WhatsApp alert when
ownerPhoneis configured - Escalated conversations automatically resume AI handling after three hours
- Human-resolution behavior is available for administrative workflows
| Layer | Technology |
|---|---|
| Runtime | Node.js, ES modules |
| API server | Express 4 |
| Database | PostgreSQL |
| ORM | Prisma 7 with @prisma/adapter-pg |
| AI | Groq OpenAI-compatible chat completions API |
| Messaging | Meta WhatsApp Cloud API |
| Payments | Paystack Charge API and webhooks |
| Environment | dotenv |
| HTTP | Axios |
| Logging | Morgan |
| Time handling | Nairobi timezone helpers with UTC date calculations |
src/
index.js Express app, middleware, health and admin routes
db.js Prisma/PostgreSQL connection
config/business.js Tenant-specific Groq system prompt builder
routes/
webhook.js Meta WhatsApp verification and message webhook
paystackWebhook.js Signed payment confirmation webhook
services/
businesses.js Tenant resolution and credential lookup
bookings.js Availability, holds, totals and booking lifecycle
customers.js Customer and conversation persistence
escalation.js Human handoff and auto-resume
groq.js AI loop, tool calling and message context
payments.js Paystack M-Pesa charge initiation
tools.js AI tool definitions and execution
whatsapp.js WhatsApp Cloud API delivery
prisma/
schema.prisma Multi-tenant relational schema
migrations/ Versioned database migrations
seed.js Example tenant seed data
scripts/
onboard-business.js Interactive tenant onboarding CLI
- Node.js 18 or newer
- PostgreSQL database
- A Meta app with WhatsApp Cloud API enabled
- A WhatsApp test or production phone number
- Groq API key
- Paystack account and secret key (for payment confirmation)
- Public HTTPS URL for webhook endpoints in production, such as Render, Railway, Fly.io, or a reverse proxy
Copy .env.example to .env, then configure:
DATABASE_URL=postgresql://user:password@host:5432/database
GROQ_API_KEY=your_groq_api_key
WHATSAPP_TOKEN=your_default_or_admin_meta_token
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id
WEBHOOK_VERIFY_TOKEN=a_private_random_verification_string
PAYSTACK_SECRET_KEY=your_paystack_secret_key
PAYSTACK_TEST_MODE=false
ADMIN_KEY=a_long_random_admin_secret
PORT=3000
# Optional seed/onboarding example values
GLOW_NAILS_PHONE_NUMBER_ID=your_phone_number_id
GLOW_NAILS_WHATSAPP_TOKEN=your_meta_access_token
TEST_BEAUTY_PHONE_NUMBER_ID=another_phone_number_id
TEST_BEAUTY_WHATSAPP_TOKEN=another_meta_access_tokenWHATSAPP_TOKEN and WHATSAPP_PHONE_NUMBER_ID are retained for compatibility and configuration examples. Live tenant messages resolve each number's credentials from PostgreSQL.
npm install
cp .env.example .envOn Windows PowerShell, the copy command is:
Copy-Item .env.example .envConfigure PostgreSQL and the required environment variables before starting the application.
Generate the Prisma client and apply migrations:
npx prisma generate
npx prisma migrate deployFor local schema development:
npx prisma migrate devExplore the data with:
npx prisma studioAn optional seed creates two demonstration businesses:
npx prisma db seednpm startDevelopment mode with Node watch:
npm run devThe server defaults to port 3000.
The interactive onboarding CLI creates a complete tenant record:
node scripts/onboard-business.jsIt collects:
- Business name, location, currency, timezone, and hours
- Open days and time range
- Slot increments, minimum notice, and staff capacity
- House-call fee and travel buffer
- Booking hold duration
- Service names, prices, and durations
- Business-specific assistant instructions
- Human escalation phone number
- Meta phone number ID and access token
The new tenant is resolved immediately by phone_number_id; no server restart or redeployment is required.
- Open the WhatsApp configuration page for your Meta app.
- Set the callback URL to
https://your-domain/webhook. - Set
WEBHOOK_VERIFY_TOKENto the same private value used in.env. - Subscribe to the
messagesfield. - Confirm that the WhatsApp Business Account's phone number ID is stored in the matching
WhatsAppAccountrow.
Meta deliveries are acknowledged with HTTP 200 before AI and database processing so the platform does not retry slow requests.
- Register
https://your-domain/webhook/paystackas the Paystack webhook URL. - Set
PAYSTACK_SECRET_KEYin.env. - Keep
PAYSTACK_TEST_MODE=falsefor live M-Pesa charges. - The endpoint validates HMAC-SHA512 signatures using the raw request body before confirming a charge.
| Method | Endpoint | Purpose |
|---|---|---|
GET |
/ |
Application status message |
GET |
/health |
Lightweight health check, returns 200 OK |
GET |
/webhook |
Meta webhook verification |
POST |
/webhook |
Incoming WhatsApp messages and status updates |
POST |
/webhook/paystack |
Signed Paystack payment notifications |
GET |
/admin/bookings?key=...&businessId=... |
List bookings for one tenant |
The admin endpoint requires the tenant's businessId and the server's ADMIN_KEY. Do not expose ADMIN_KEY in client applications.
- The assistant asks for services, booking type, date, and time.
- It calls
check_availabilitybefore proposing or confirming a slot. - The customer reviews and confirms a summary.
create_bookingperforms a fresh availability check.- A booking is created with
pending_paymentstatus and a configured hold expiry. - Paystack starts an M-Pesa charge.
- A signed
charge.successwebhook verifies the payment event and confirms the booking. - The customer receives a WhatsApp confirmation.
If payment cannot be initiated, the booking remains temporarily held so a retry or operational recovery can be performed without immediately releasing the slot.
The relational schema models:
- Business: operations, location, timezone, hours, capacity, fees, hold settings, and custom AI instructions
- WhatsAppAccount: tenant-to-
phone_number_idrouting and outbound credentials - Service: tenant-specific prices and durations
- Customer: phone identity scoped to one business
- Conversation: persistent customer conversation and escalation state
- Message: ordered user, assistant, or tool messages
- Booking: service snapshot, address, date, time, duration, price, hold expiry, and confirmation state
Full field definitions are in prisma/schema.prisma.
- Store environment secrets in the deployment platform's secret manager
- Use distinct WhatsApp tokens per tenant and rotate compromised credentials
- Restrict admin endpoints with strong authentication rather than exposing a key-based query parameter
- Configure Paystack webhook allowlisting and signature validation
- Put the public webhook behind an HTTPS reverse proxy
- Add database, payment, and provider rate limiting for public traffic
- Monitor failed webhook, Groq, WhatsApp, and Paystack operations
- Implement payment retries and reconciliation before relying on the system for revenue
- Move WhatsApp credentials and other provider secrets to a dedicated secret store for higher-scale deployments
- Add authentication and authorization for an operations dashboard
- Add automated migrations and backups to the deployment pipeline
- Restrict webhook processing logs to avoid exposing customer phone numbers and message content
This repository demonstrates:
- Backend API and webhook development
- Multi-tenant SaaS architecture and tenant isolation
- Relational data modeling and Prisma/PostgreSQL usage
- AI tool/function calling and prompt engineering
- Third-party API integrations
- Payment event verification
- Background-safe webhook acknowledgment patterns
- Scheduling and concurrency-aware business logic
- Conversation persistence and human handoff workflows
- Environment-based configuration and operational scripting
- CLI-driven tenant provisioning
- Non-text WhatsApp messages are currently logged and skipped
- No complete admin UI or role-based dashboard is included yet
- Paystack's test mode uses a designated test phone number
- Failed payment-start operations do not yet have an automatic retry worker
- Conversation history is limited to the latest 16 persisted messages per request
- Token and secret storage is database/environment based rather than a dedicated secrets manager
- Meta and Paystack events do not yet have a separate persisted event/reconciliation log
- Confirm the payload
phone_number_idexists inWhatsAppAccount.phoneNumberId - Confirm the phone number belongs to the intended business
- Restart the process if you need tenant cache data to invalidate faster
- Confirm the callback URL ends in
/webhook - Confirm
WEBHOOK_VERIFY_TOKENexactly matches the value entered in Meta - Confirm the server is publicly reachable over HTTPS
- Confirm
PAYSTACK_SECRET_KEYis correct - Confirm
/webhook/paystackis registered publicly - Check that the request contains the
x-paystack-signatureheader - Confirm booking metadata contains both
businessIdandbookingId - Confirm the server is returning HTTP 200 promptly
- Confirm
DATABASE_URLuses a valid PostgreSQL connection string - Run
npx prisma generate - Apply migrations with
npx prisma migrate deploy
Add your preferred license before publishing this repository publicly.