An automated, session-based SMS conversational bot for scheduling fitness and wellness appointments (Gym, Personal Trainer, and Massage), built with Node.js, Express, and the Twilio Messaging API.
- State Machine Conversation Flow: Guides users through service selection, date picking, time selection, and booking confirmation.
- Session-Based State Management: Tracks appointment progress per user across multiple SMS exchanges using
express-session. - Session Reset / Cancel Keywords: Texting
reset,restart,cancel, orstart overresets the booking state at any time. - Interactive Terminal Chat Simulator: Test the complete conversation flow locally in your terminal without needing Twilio credits or cellular delivery.
- Automated Test Suite: Built-in unit tests verifying matching logic and step progression with zero external test dependencies (
node --test). - Localtunnel Support: Expose your local development server to the internet to receive live webhooks from Twilio.
graph TD
A[Incoming SMS] --> B{Active Session?}
B -->|No / Step 1| C[Show Service Options: Gym, Personal Trainer, Massage]
B -->|Yes / Step 1| D[Parse Service Type]
D -->|Match| E[Step 2: Ask for Date/Day]
D -->|No Match| C
B -->|Yes / Step 2| F[Parse Day of Week]
F -->|Match| G[Step 3: Ask for Time: 10am, 11am, 1pm, 4pm]
F -->|Invalid / Multiple| E
B -->|Yes / Step 3| H[Parse Time]
H -->|Match| I[Step 4: Confirm Booking Details]
H -->|No Match| G
B -->|Yes / Step 4| J[Display Existing Booking Summary]
twilio-app-2/
โโโ .env.example # Template for environment variables
โโโ .gitignore # Git ignore rules for node_modules, .env, and logs
โโโ package.json # Dependencies and scripts
โโโ scripts/
โ โโโ chatSimulator.js # Terminal-based chat simulator
โโโ src/
โ โโโ config.js # Centralized environment config and defaults
โ โโโ twilioClient.js # Twilio REST client & outbound SMS sender
โ โโโ bookingHelper.js # Parsing algorithms (type, day, time, confirmation)
โ โโโ controllers/
โ โ โโโ smsController.js # Webhook handlers, session machine & health check
โ โโโ routes/
โ โ โโโ smsRoutes.js # Express route definitions (/test, /health, /receive-sms)
โ โโโ main.js # Application bootstrap & middleware setup
โโโ test/
โโโ bookingHelper.test.js # Unit test suite
Copy .env.example to .env and populate your credentials:
cp .env.example .env
| Variable | Description | Default / Example |
|---|---|---|
PORT |
Local server port | 3000 |
SESSION_SECRET |
Secret key for express-session cookie encryption | twilio-course-secret |
ACCOUNT_SID |
Twilio Account SID | ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
AUTH_TOKEN |
Twilio Auth Token | your_auth_token_here |
PHONE_NUMBER |
Your Twilio phone number (E.164 format) | +1234567890 |
MY_NUMBER |
Your personal phone number for outbound test SMS | +380687866398 |
WEEKDAYS |
Comma-separated list of valid weekdays | monday,tuesday,... |
npm install
npm run dev
npm testYou can test the entire conversation flow locally without making real phone calls or using external tools:
- In one terminal, start the server:
npm run dev
- In a second terminal, launch the chat simulator:
npm run chat
- Chat with the bot interactively:
- Type
Hi - Type
I want the gym - Type
Wednesday - Type
11am - Type
resetto restart the conversation at any point.
- Type
Simulate incoming Twilio webhooks directly against your local server:
# Step 1: Initialize curl.exe -X POST http://localhost:3000/receive-sms ` -H "Content-Type: application/x-www-form-urlencoded" ` -c cookies.txt -b cookies.txt ` -d "Body=Hi" # Step 2: Choose Service curl.exe -X POST http://localhost:3000/receive-sms ` -H "Content-Type: application/x-www-form-urlencoded" ` -c cookies.txt -b cookies.txt ` -d "Body=I want the gym" # Step 3: Choose Day curl.exe -X POST http://localhost:3000/receive-sms ` -H "Content-Type: application/x-www-form-urlencoded" ` -c cookies.txt -b cookies.txt ` -d "Body=Wednesday" # Step 4: Choose Time curl.exe -X POST http://localhost:3000/receive-sms ` -H "Content-Type: application/x-www-form-urlencoded" ` -c cookies.txt -b cookies.txt ` -d "Body=11am"
-
Verify your personal phone number:
- On a Twilio Trial account, destination numbers must be verified under Twilio Console > Phone Numbers > Verified Caller IDs.
-
Start the server and Localtunnel:
# Terminal 1 npm run dev # Terminal 2 npm run tunnel
-
Configure the Twilio Webhook:
- Go to Twilio Console > Active Numbers and select your number.
- Under Messaging Configuration -> A MESSAGE COMES IN:
- Webhook URL:
https://<YOUR-LOCALTUNNEL-URL>/receive-sms - HTTP Method:
HTTP POST
- Webhook URL:
- Save changes.
-
Send an SMS from your verified mobile phone to your Twilio number to start booking!
GET /healthโ Health check endpoint returning server status and uptime.GET /testโ Smoke test route to verify server connectivity.POST /receive-smsโ Twilio inbound messaging webhook (accepts form URL-encoded body, returns TwiML XML).