const bot = { userInvestment: 0, planDuration: 3, // Default to 3 months currentKIBOR: 10, // Example KIBOR rate (10% for now) userName: '', userAge: 0, userCity: '', additionalTopUp: 0, topUpPerMonth: 0,
responses: {
greeting: "Hello! Welcome to PakQatar Takaful. How can I assist you today?",
quickReplies: [
{ text: "I want to calculate my investment" },
{ text: "What is the minimum investment?" },
{ text: "Show me some options for investment amounts" }
],
request_details: "Please provide your name, age, and city in the format: Name, Age, City (e.g., John, 30, Karachi).",
request_investment: "Thank you for the details! Now, please tell me how much you would like to invest (minimum 50,000 PKR).",
request_duration: "Please choose a duration for your investment. Here are some options: 5 months, 1 year, 2 years, 3 years, 5 years.",
invalid_input: "Sorry, I didn't quite understand that. Please try again.",
investment_options: [
{ amount: 50000, label: "50,000 PKR" },
{ amount: 100000, label: "100,000 PKR" },
{ amount: 150000, label: "150,000 PKR" },
{ amount: 200000, label: "200,000 PKR" }
],
top_up_prompt: "Would you like to top up your investment? You can add one of the following amounts per month:\n- 1,000 PKR\n- 5,000 PKR\n- 10,000 PKR\n- 20,000 PKR",
top_up_options: [1000, 5000, 10000, 20000],
top_up_applied: function(topUpAmount, duration) {
return You have successfully applied a top-up of ${topUpAmount} PKR per month for a duration of ${duration} months. Your total investment will be updated accordingly.;
},
coverage_calculation: function() {
if (this.userInvestment <= 0 || this.planDuration <= 0) {
return "There seems to be an issue with your investment or duration. Please try again.";
}
let totalInvestment = this.userInvestment + (this.topUpPerMonth * this.planDuration);
let profit = (totalInvestment * this.currentKIBOR) / 100;
totalInvestment += profit;
let naturalDeathCoverage = Math.min(totalInvestment * 3, 15000000); // 3x investment, capped at 15 million
let accidentalDeathCoverage = Math.min(totalInvestment * 6, 30000000); // 6x investment, capped at 30 million
return `
**Customer Details:**
- **Name**: ${this.userName}
- **Age**: ${this.userAge}
- **City**: ${this.userCity}
**Investment Breakdown:**
- **Total Investment (with profit)**: ${totalInvestment.toFixed(2)} PKR
- **Natural Death Coverage**: ${naturalDeathCoverage.toFixed(2)} PKR
- **Accidental Death Coverage**: ${accidentalDeathCoverage.toFixed(2)} PKR
- **Duration**: ${this.planDuration} months
Your investment has been successfully calculated.
`;
}
},
getResponse: async function(query) { query = query.trim().toLowerCase();
// Start: Check if user's details are missing
if (!this.userName) {
return {
action: 'send_message',
message: {
type: 'text',
text: this.responses.greeting,
quick_replies: this.responses.quickReplies
}
};
}
// Collecting user's details
if (this.userName && this.userAge === 0 && this.userCity === '') {
const details = query.split(",");
if (details.length === 3) {
this.userName = details[0].trim();
this.userAge = parseInt(details[1].trim());
this.userCity = details[2].trim();
return this.responses.request_investment;
} else {
return "Please provide your details in the format: Name, Age, City (e.g., John, 30, Karachi).";
}
}
// Collecting investment amount
if (this.userInvestment === 0) {
let investment = parseInt(query);
if (isNaN(investment) || investment < 50000) {
return "The minimum investment amount is 50,000 PKR. Please provide a valid amount.";
}
this.userInvestment = investment;
return this.responses.request_duration;
}
// Collecting duration
if (this.planDuration === 3 && query) {
let durationOption = this.responses.duration_options?.find(option => query.includes(option.title.toLowerCase()));
if (durationOption) {
this.planDuration = durationOption.value;
// Investment breakdown
const coverageDetails = this.responses.coverage_calculation();
return {
action: 'send_message',
message: {
type: 'success',
text: coverageDetails + `\n\n${this.responses.top_up_prompt}`
}
};
} else {
return "Sorry, I didn't quite understand that duration. Please choose from the following: 5 months, 1 year, 2 years, 3 years, 5 years.";
}
}
// Handling Top-up Option
if (this.additionalTopUp === 0) {
let topUpAmount = parseInt(query);
if (this.responses.top_up_options.includes(topUpAmount)) {
this.topUpPerMonth = topUpAmount;
// Applying top-up
const topUpMessage = this.responses.top_up_applied(topUpAmount, this.planDuration);
const updatedCoverageDetails = this.responses.coverage_calculation();
return {
action: 'send_message',
message: {
type: 'success',
text: `${topUpMessage}\n\n${updatedCoverageDetails}`
}
};
} else {
return this.responses.top_up_prompt;
}
}
// Default Fallback (in case no valid flow is matched)
return {
action: 'send_message',
message: {
type: 'text',
text: "Sorry, I didn't understand that. Can you please try again?"
}
};
} };
// Example Flow: User queries console.log(bot.getResponse("Hello")); // Bot greets the user console.log(bot.getResponse("Mansoor, 19, Lahore")); // User provides details console.log(bot.getResponse("110000")); // User provides investment amount console.log(bot.getResponse("1 year")); // User selects the duration (1 year) console.log(bot.getResponse("5000")); // User opts for a top-up of 5000 PKR