curaflow/backend/botLogic.js

109 lines
4.0 KiB
JavaScript

const queueManager = require('./queueManager');
const triageEngine = require('./triageEngine');
const i18n = require('./i18n');
/**
* BotLogic: Simulates the WhatsApp conversational flow
*/
class BotLogic {
constructor() {
this.states = {}; // Track session state per patient
}
async handleMessage(patientId, message) {
const text = message.toLowerCase();
let state = this.states[patientId] || 'IDLE';
let lang = this.states[`${patientId}_lang`] || 'en';
// 1. Language Selection (First time or reset)
if (text.includes('hi') && !this.states[`${patientId}_lang`]) {
this.states[patientId] = 'SELECT_LANG';
return {
reply: "Welcome to Curio! Please select your language / भाषा चुनें / భాషను ఎంచుకోండి:\n\n1. English\n2. Hindi (हिंदी)\n3. Telugu (తెలుగు)",
buttons: ["English", "Hindi", "Telugu"]
};
}
if (state === 'SELECT_LANG') {
if (text.includes('hindi')) lang = 'hi';
if (text.includes('telugu')) lang = 'te';
this.states[`${patientId}_lang`] = lang;
this.states[patientId] = 'IDLE';
state = 'IDLE'; // Continue to welcome
}
// 2. Initial Greeting / Booking Intent
if (state === 'IDLE') {
return {
reply: i18n.t(lang, 'welcome'),
buttons: i18n.getButtons(lang)
};
}
// 2. Booking Flow
if (text.includes('book for today')) {
return {
reply: "Great! Please select your preferred time slot:",
buttons: ["10:00 AM", "10:30 AM", "11:00 AM"]
};
}
if (text === '10:00 am' || text === '10:30 am') {
const date = new Date().toISOString().split('T')[0];
const result = queueManager.bookOnline(date, text.replace(' am', ''));
if (result.success) {
this.states[patientId] = 'TRIAGE_START';
return {
reply: i18n.t(lang, 'triage_ask', { token: result.token }) +
` \n\n💳 *Payment Required*: Please pay ₹${result.fee} to activate your token: http://razorpay.me/Curio_mock`,
};
} else {
return {
reply: "❌ Sorry, the 10:00 AM online slots are full (Max 3/slot). Would you like to try 11:30 AM?",
buttons: ["Try 11:30 AM", "Waitlist Me"]
};
}
}
// 3. Triage State Handling
if (this.states[patientId] === 'TRIAGE_START') {
const analysis = await triageEngine.analyzeSymptoms(text);
this.states[patientId] = 'TRIAGE_FOLLOWUP';
this.states[`${patientId}_summary`] = analysis.summary;
return {
reply: `Thank you. AI Analysis: _${analysis.category}_.\n\nQuick follow-up: ${analysis.followUp}`
};
}
if (this.states[patientId] === 'TRIAGE_FOLLOWUP') {
this.states[patientId] = 'IDLE';
return {
reply: i18n.t(lang, 'triage_done')
};
}
// 4. Daily Updates / Status
if (text.includes('status') || text.includes('queue') || text.includes('कतार') || text.includes('స్థితి')) {
return {
reply: i18n.t(lang, 'status_update', { current: 'WK-3', pos: 'ON-1', wait: '8' })
};
}
if (text.includes('tips') || text.includes('update')) {
const updates = queueManager.getDailyUpdates(patientId);
return {
reply: "🔔 *Daily Updates for You:*\n\n" + updates.join('\n\n')
};
}
// 4. Default Fallback
return {
reply: "I'm sorry, I didn't quite catch that. You can type 'Hi' to see the main menu."
};
}
}
module.exports = new BotLogic();