Skip to content

Repository files navigation

SMG Electric Scooters Chatbot - Backend API

A scalable, production-ready Node.js/Express backend for the SMG Electric Scooters Chatbot application with MongoDB, JWT authentication, and comprehensive admin panel APIs.

πŸš€ Features

  • 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

πŸ“‹ Tech Stack

  • Runtime: Node.js (v18+)
  • Framework: Express.js
  • Database: MongoDB (Mongoose ODM)
  • Authentication: JWT (JSON Web Tokens)
  • Password Hashing: bcryptjs
  • Environment: dotenv

πŸ“ Project Structure

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

πŸ› οΈ Setup Instructions

1. Prerequisites

  • Node.js (v18 or higher)
  • MongoDB (local or MongoDB Atlas)
  • npm or yarn

2. Install Dependencies

npm install

3. Environment Configuration

Copy .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=admin123

4. Start MongoDB

Local MongoDB:

# Windows
net start MongoDB

# macOS/Linux
sudo systemctl start mongod
# or
mongod

MongoDB Atlas:

  • Create account at MongoDB Atlas
  • Create cluster and get connection string
  • Update MONGODB_URI in .env

5. Seed Admin User (Optional)

Create default admin user:

npm run seed

This creates an admin with:

  • Email: admin@smg.com (or from ADMIN_EMAIL)
  • Password: admin123 (or from ADMIN_PASSWORD)

⚠️ Change default password after first login!

6. Start Server

Development (with auto-reload):

npm run dev

Production:

npm start

Server will start on http://localhost:3000

πŸ“‘ API Endpoints

Public Endpoints

  • POST /api/chat - Process chat message
  • GET /api/chat/history/:sessionId - Get conversation history
  • POST /api/leads - Create lead
  • GET /api/programs - Get all programs
  • GET /api/programs/:type - Get specific program

Admin Endpoints (Protected)

  • POST /api/admin/login - Admin login
  • GET /api/admin/profile - Get admin profile
  • GET /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.

🎯 Chatbot Intents

The chatbot recognizes the following intents:

  1. greeting - Greetings and salutations
  2. about_smg - About SMG company
  3. products - Product information
  4. services - Services and support
  5. internships - SMG Nirmaan Programme
  6. scholarships - SMG Scholarships
  7. industrial_visit - SMG Bhraman visits
  8. financing_insurance - Financing and insurance
  9. contact_social - Contact and social media
  10. unknown - Unrecognized intent

πŸ” Authentication

Admin endpoints require JWT authentication:

  1. Login via POST /api/admin/login to get token
  2. Include token in request headers:
    Authorization: Bearer <token>
    

πŸ“Š Database Schemas

Conversation

  • userId - Optional user identifier
  • message - User message
  • intent - Detected intent
  • response - Bot response
  • confidenceScore - Intent confidence (0-1)
  • sessionId - Session identifier
  • createdAt - Timestamp

Lead

  • name - Lead name
  • phone - Phone number
  • email - Email address
  • interest - Interest type (product/internship/scholarship/dealership)
  • city - City
  • status - Lead status (new/contacted/qualified/converted/closed)
  • createdAt - Timestamp

Admin

  • email - Admin email (unique)
  • password - Hashed password
  • role - Admin role
  • name - Admin name
  • isActive - Active status
  • lastLogin - Last login timestamp

πŸ§ͺ Testing API

Using cURL

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>"

Using Postman/Insomnia

  1. Import endpoints from API_DOCUMENTATION.md
  2. For protected routes, add Authorization: Bearer <token> header
  3. Set Content-Type: application/json for POST requests

πŸš€ Deployment

Environment Variables for Production

NODE_ENV=production
PORT=3000
MONGODB_URI=mongodb+srv://...
JWT_SECRET=<strong-random-secret-32-chars-min>

Recommended Platforms

  • 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)

πŸ“ Scripts

  • npm start - Start production server
  • npm run dev - Start development server with nodemon
  • npm run seed - Seed default admin user

πŸ”’ Security Best Practices

  1. Change default admin credentials after first login
  2. Use strong JWT_SECRET (minimum 32 characters)
  3. Enable HTTPS in production
  4. Use MongoDB Atlas with IP whitelisting
  5. Implement rate limiting (recommended: express-rate-limit)
  6. Validate and sanitize all inputs
  7. Keep dependencies updated

πŸ“š Additional Documentation

🀝 Contributing

  1. Follow MVC architecture
  2. Add proper error handling
  3. Include input validation
  4. Write clear comments
  5. Test endpoints before committing

πŸ“„ License

ISC

πŸ†˜ Support

For issues or questions, please contact the development team.


Built with ❀️ for SMG Electric Scooters

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages