A scalable, production-ready Node.js/Express backend for the SMG Electric Scooters Chatbot application with MongoDB, JWT authentication, and comprehensive admin panel APIs.
- Chatbot Engine - Keyword-based intent detection with 10+ intents
- User Conversation Logging - MongoDB storage with session management
- Lead Management - Capture and manage customer inquiries
- Programs API - SMG Nirmaan, Scholarships, and Industrial Visits
- Admin Panel APIs - Protected endpoints with JWT authentication
- Analytics Dashboard - Conversation and lead statistics
- MVC Architecture - Clean, scalable, maintainable code structure
- Error Handling - Comprehensive error handling middleware
- Input Validation - Request validation and sanitization
- Runtime: Node.js (v18+)
- Framework: Express.js
- Database: MongoDB (Mongoose ODM)
- Authentication: JWT (JSON Web Tokens)
- Password Hashing: bcryptjs
- Environment: dotenv
smg/
βββ src/
β βββ config/
β β βββ database.js # MongoDB connection
β β βββ jwt.js # JWT configuration
β βββ controllers/
β β βββ chatController.js # Chat endpoints
β β βββ leadController.js # Lead management
β β βββ programController.js # Programs info
β β βββ adminController.js # Admin operations
β βββ models/
β β βββ Conversation.js # Conversation schema
β β βββ Lead.js # Lead schema
β β βββ Admin.js # Admin schema
β βββ routes/
β β βββ chatRoutes.js # Chat routes
β β βββ leadRoutes.js # Lead routes
β β βββ programRoutes.js # Program routes
β β βββ adminRoutes.js # Admin routes
β βββ services/
β β βββ chatbotService.js # Intent detection logic
β βββ middlewares/
β β βββ auth.js # JWT authentication
β β βββ errorHandler.js # Error handling
β β βββ asyncHandler.js # Async wrapper
β βββ utils/
β β βββ validators.js # Validation utilities
β β βββ seedAdmin.js # Admin seeding script
β βββ app.js # Express app configuration
βββ server.js # Server entry point
βββ package.json
βββ .env.example
βββ README.md
- Node.js (v18 or higher)
- MongoDB (local or MongoDB Atlas)
- npm or yarn
npm installCopy .env.example to .env and configure:
# Server
PORT=3000
NODE_ENV=development
# MongoDB
MONGODB_URI=mongodb://localhost:27017/smg_chatbot
# Or for MongoDB Atlas:
# MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/smg_chatbot
# JWT
JWT_SECRET=your-super-secret-jwt-key-change-in-production-min-32-chars
JWT_EXPIRES_IN=7d
# Admin (for seeding)
ADMIN_EMAIL=admin@smg.com
ADMIN_PASSWORD=admin123Local MongoDB:
# Windows
net start MongoDB
# macOS/Linux
sudo systemctl start mongod
# or
mongodMongoDB Atlas:
- Create account at MongoDB Atlas
- Create cluster and get connection string
- Update
MONGODB_URIin.env
Create default admin user:
npm run seedThis creates an admin with:
- Email:
admin@smg.com(or fromADMIN_EMAIL) - Password:
admin123(or fromADMIN_PASSWORD)
Development (with auto-reload):
npm run devProduction:
npm startServer will start on http://localhost:3000
POST /api/chat- Process chat messageGET /api/chat/history/:sessionId- Get conversation historyPOST /api/leads- Create leadGET /api/programs- Get all programsGET /api/programs/:type- Get specific program
POST /api/admin/login- Admin loginGET /api/admin/profile- Get admin profileGET /api/admin/conversations- Get conversations (with filters)GET /api/admin/leads- Get leads (with filters)GET /api/admin/analytics- Get analytics data
See API_DOCUMENTATION.md for detailed API documentation.
The chatbot recognizes the following intents:
- greeting - Greetings and salutations
- about_smg - About SMG company
- products - Product information
- services - Services and support
- internships - SMG Nirmaan Programme
- scholarships - SMG Scholarships
- industrial_visit - SMG Bhraman visits
- financing_insurance - Financing and insurance
- contact_social - Contact and social media
- unknown - Unrecognized intent
Admin endpoints require JWT authentication:
- Login via
POST /api/admin/loginto get token - Include token in request headers:
Authorization: Bearer <token>
userId- Optional user identifiermessage- User messageintent- Detected intentresponse- Bot responseconfidenceScore- Intent confidence (0-1)sessionId- Session identifiercreatedAt- Timestamp
name- Lead namephone- Phone numberemail- Email addressinterest- Interest type (product/internship/scholarship/dealership)city- Citystatus- Lead status (new/contacted/qualified/converted/closed)createdAt- Timestamp
email- Admin email (unique)password- Hashed passwordrole- Admin rolename- Admin nameisActive- Active statuslastLogin- Last login timestamp
Chat API:
curl -X POST http://localhost:3000/api/chat \
-H "Content-Type: application/json" \
-d '{"userMessage": "Tell me about internships"}'Admin Login:
curl -X POST http://localhost:3000/api/admin/login \
-H "Content-Type: application/json" \
-d '{"email": "admin@smg.com", "password": "admin123"}'Get Conversations (with auth):
curl http://localhost:3000/api/admin/conversations \
-H "Authorization: Bearer <your-token>"- Import endpoints from
API_DOCUMENTATION.md - For protected routes, add
Authorization: Bearer <token>header - Set
Content-Type: application/jsonfor POST requests
NODE_ENV=production
PORT=3000
MONGODB_URI=mongodb+srv://...
JWT_SECRET=<strong-random-secret-32-chars-min>- Heroku - Easy deployment with MongoDB Atlas
- Railway - Simple MongoDB integration
- DigitalOcean - App Platform with managed MongoDB
- AWS - EC2 + DocumentDB or MongoDB Atlas
- Vercel/Netlify - Serverless (requires adjustments)
npm start- Start production servernpm run dev- Start development server with nodemonnpm run seed- Seed default admin user
- Change default admin credentials after first login
- Use strong JWT_SECRET (minimum 32 characters)
- Enable HTTPS in production
- Use MongoDB Atlas with IP whitelisting
- Implement rate limiting (recommended: express-rate-limit)
- Validate and sanitize all inputs
- Keep dependencies updated
- API_DOCUMENTATION.md - Complete API reference
- ARCHITECTURE.md - Architecture overview (if exists)
- Follow MVC architecture
- Add proper error handling
- Include input validation
- Write clear comments
- Test endpoints before committing
ISC
For issues or questions, please contact the development team.
Built with β€οΈ for SMG Electric Scooters