The SmartCare Hospital Chatbot is an intelligent AI assistant that helps patients and visitors with appointments, queries, and navigation through the hospital system.
The chatbot uses pattern-based NLP to detect user intents:
- Greeting: Welcome messages and initial interaction
- Book Appointment: Help with scheduling appointments
- Check Status: Track appointment status via phone number
- Find Doctor: Search doctors by specialty
- Wait Time: Get current wait time estimates
- Departments: View available departments
- Crowd Info: Get crowd predictions and best visit times
- Help: Show available commands
- Thanks/Bye: Polite conversation endings
- Maintains conversation context
- Remembers user information during session
- Provides personalized suggestions based on conversation flow
- Quick-reply buttons for common actions
- Dynamic suggestions based on conversation context
- One-click actions for booking, checking status, etc.
- Connects with existing ML models (Crowd Predictor, No-Show Predictor)
- Fetches live data from database (doctors, departments, appointments)
- Provides accurate wait time estimates
class HospitalChatbot:
- process_message(message, user_context) β dict
- _detect_intent(message) β str
- _handle_greeting() β dict
- _handle_book_appointment(message) β dict
- _handle_check_status(message) β dict
- _handle_find_doctor(message) β dict
- _handle_wait_time(message) β dict
- _handle_departments() β dict
- _handle_crowd_info(message) β dict
- _handle_help() β dictResponse Format:
{
"response": "Bot message text",
"suggestions": ["Quick reply 1", "Quick reply 2"],
"type": "intent_type",
"data": {
"additional": "context data"
}
}POST /chatbot/message- Process user messagePOST /chatbot/reset- Reset conversation context
- Location: Bottom-right corner of all pages
- Design: Modern gradient purple theme
- Animations: Smooth slide-up, fade-in effects
- Responsive: Adapts to mobile screens
- Header: Bot name with close button
- Messages Area: Scrollable chat history
- Input Area: Text input with send button
- Suggestions: Quick-reply buttons below bot messages
- initChatbot() - Initialize chatbot widget
- sendMessage(message) - Send message to backend
- addChatMessage(text, sender, suggestions) - Add message to UI
- showTypingIndicator() - Show bot typing animation
- hideTypingIndicator() - Remove typing animation- Chatbot container and button styles
- Chat window layout and animations
- Message bubbles (user vs bot)
- Suggestion buttons
- Typing indicator animation
- Mobile responsive design
User: "I want to book an appointment"
Bot: "I'd be happy to help you book an appointment! π
Which department would you like to visit?
β’ Cardiology
β’ Neurology
β’ Pediatrics
..."
User: "Cardiology"
Bot: "Great! I can help you book an appointment in Cardiology.
Available doctors:
β’ Dr. John Smith - Cardiology (15 years exp.)
β’ Dr. Sarah Johnson - Cardiology (12 years exp.)
..."
User: "Check my appointment status"
Bot: "I can help you check your appointment status! π
Please provide your 10-digit phone number."
User: "9876543210"
Bot: "Found your appointments! π
β’ APT-001 - 2024-03-15 at 10:00 (confirmed)
β’ APT-002 - 2024-03-20 at 14:30 (pending)
..."
User: "Find me a cardiologist"
Bot: "Found 3 cardiology specialist(s):
β’ Dr. John Smith - Cardiology
15 years exp. | Rating: 4.8β
β’ Dr. Sarah Johnson - Cardiology
12 years exp. | Rating: 4.7β
..."
User: "What's the wait time?"
Bot: "Current estimated wait times:
β’ Cardiology: ~20-30 minutes (medium crowd)
β’ Neurology: ~10-15 minutes (low crowd)
β’ Pediatrics: ~35-50 minutes (high crowd)
...
π‘ Tip: Book an appointment to reduce your wait time!"
User: "When is the best time to visit?"
Bot: "π Crowd predictions for tomorrow:
β’ 8:00 AM: Low crowd
β’ 12:00 PM: Medium crowd
β’ 4:00 PM: Low crowd
π‘ Best times to visit: Early morning (8-9 AM) or late afternoon (5-6 PM)"
python run.py- Open any page in the application
- Look for the purple chat button in the bottom-right corner
- Click to open the chat window
Try these test messages:
- "Hello"
- "Book an appointment"
- "Find a doctor"
- "What's the wait time?"
- "Check status for 1234567890"
- "Show departments"
- "When should I visit?"
- "Help"
- β Intent detection accuracy
- β Response relevance
- β Suggestion buttons functionality
- β Typing indicator animation
- β Message scrolling
- β Mobile responsiveness
- β Error handling
Department- Fetch department informationDoctor- Search and list doctorsAppointment- Check appointment statusPatient- Retrieve patient data
CrowdPredictor- Get crowd level predictionsSlotOptimizer- Find available appointment slotsNoShowPredictor- (Future) Predict no-show probability
- Can redirect users to booking pages
- Can link to status check pages
- Can navigate to doctor listings
- Add Pattern to
chatbot_service.py:
self.intents = {
'new_intent': [
r'\bkeyword1\b',
r'\bkeyword2\b',
],
}- Create Handler Method:
def _handle_new_intent(self, message: str) -> dict:
return {
'response': "Your response text",
'suggestions': ["Option 1", "Option 2"],
'type': 'new_intent'
}- Add to Process Logic:
elif intent == 'new_intent':
return self._handle_new_intent(message_lower)Change Colors:
/* In style.css */
.chatbot-button {
background: linear-gradient(135deg, #YOUR_COLOR1 0%, #YOUR_COLOR2 100%);
}Change Position:
.chatbot-container {
bottom: 20px; /* Adjust vertical position */
right: 20px; /* Adjust horizontal position */
}Change Size:
.chatbot-window {
width: 380px; /* Adjust width */
height: 550px; /* Adjust height */
}- Average response time: < 500ms
- Pattern matching is fast (regex-based)
- Database queries are optimized
- Stateless design (context stored per session)
- Can handle multiple concurrent users
- No heavy ML processing in chatbot itself
- Graceful fallback for unknown intents
- Error messages for failed API calls
- Timeout handling for slow responses
-
Natural Language Processing
- Integrate with OpenAI/Anthropic for better understanding
- Support for multiple languages
- Sentiment analysis
-
Advanced Booking
- Complete appointment booking through chat
- Payment integration
- Appointment rescheduling
-
Voice Support
- Speech-to-text input
- Text-to-speech responses
- Voice commands
-
Analytics
- Track common queries
- Measure user satisfaction
- Identify improvement areas
-
Personalization
- Remember user preferences
- Proactive notifications
- Personalized recommendations
- Check if JavaScript is enabled
- Verify
base.htmlincludes chatbot widget - Check browser console for errors
- Verify Flask server is running
- Check
/chatbot/messageendpoint is accessible - Review browser network tab for failed requests
- Review intent patterns in
chatbot_service.py - Check database has required data
- Verify ML models are loaded correctly
- Clear browser cache
- Check
style.cssis loaded - Verify Bootstrap CSS is included
Request:
{
"message": "User message text",
"context": {
"patient_id": 123,
"phone": "1234567890"
}
}Response:
{
"response": "Bot response text",
"suggestions": ["Suggestion 1", "Suggestion 2"],
"type": "intent_type",
"data": {
"additional": "context data"
}
}Status Codes:
200- Success400- Bad request (missing message)500- Server error
Response:
{
"status": "success",
"message": "Context reset"
}- β
app/services/chatbot_service.py- Chatbot backend logic - β
app/routes/chatbot.py- API endpoints - β
CHATBOT_IMPLEMENTATION_GUIDE.md- This documentation
- β
app/__init__.py- Registered chatbot blueprint - β
app/templates/base.html- Added chatbot widget HTML - β
app/static/css/style.css- Added chatbot styles - β
app/static/js/app.js- Added chatbot JavaScript
The chatbot feature is now fully integrated into the SmartCare Hospital system. It provides:
β 10 intelligent intents for common hospital queries β Real-time integration with existing ML models and database β Modern UI with floating widget and smooth animations β Context-aware responses with smart suggestions β Mobile responsive design β Easy to extend with new intents and features
The chatbot enhances user experience by providing instant assistance for appointments, doctor searches, wait times, and general hospital information.