Spaces:
Running

File size: 4,429 Bytes
87cc7f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// BotCompany β€” pure rule-based chat engine.
// Intent classification (regex) + persona-aware bot replies + deterministic scoring.
// Thresholds: score >= 65 -> READY, 35..64 -> WAITING, < 35 -> CANCELLED.

import { OFFER, PORTFOLIO, portfolioMessage } from "./business";
import type { Intent, Lead, LeadStatus } from "./types";

// ---------- intent classification ----------
const RX: Array<[Intent, RegExp]> = [
  ["STOP", /\b(stop|do not (msg|message|text)|remove my number|unsubscribe|block)\b/i],
  ["NO", /\b(no|not interested|nope|nahi|later|not now|don'?t need|do not need|pass)\b/i],
  ["PRICE", /\b(price|cost|charge|kitna|rate|budget|how much|fees|quotation)\b/i],
  ["TIME", /\b(time|timeline|how long|delivery|days|week|duration|kab tak)\b/i],
  ["YES", /\b(yes|ok(ay)?|done|deal|i am in|let us|let's|haan|sure|proceed|go ahead|start)\b/i],
  ["INTEREST", /\b(sample|portfolio|example|show|work|site|website|interested|share|send|demo)\b/i],
];

export function classifyIntent(text: string): Intent {
  for (const [intent, rx] of RX) {
    if (rx.test(text)) return intent;
  }
  return "NEUTRAL";
}

// ---------- bot replies ----------
export const PRICE_ANSWER = [
  `Flat ${OFFER.priceLabel} for a complete website β€” no monthly fee from me for the build.`,
  `Hosting + domain is a small add-on: ${OFFER.addOn}, billed monthly. Cancel anytime.`,
  `Delivery in ${OFFER.delivery}. Include: ${OFFER.includes[0]}, WhatsApp button, mobile-first pages.`,
  `Shall I send the proposal?`,
].join("\n");

export const TIME_ANSWER = [
  `Typical build: ${OFFER.delivery}.`,
  `Day 1-2 content + design lock, then build, then your review.`,
].join("\n");

export function botReply(intent: Intent, lead: Lead): string[] {
  const name = lead.owner;
  switch (intent) {
    case "PRICE":
      return [PRICE_ANSWER];
    case "TIME":
      return [TIME_ANSWER];
    case "YES":
      return [
        `Great ${name} ji πŸ™Œ I will WhatsApp you the proposal today. One question β€” which of my 4 demo styles do you like?`,
        portfolioMessage(),
      ];
    case "INTEREST":
      return [
        portfolioMessage(),
        `All 4 are live client sites. For ${lead.business} I would do a similar style with your photos + WhatsApp enquiry button.`,
      ];
    case "NO":
      return [
        `No problem ${name} ji πŸ™ If you ever need a site, message me anytime. Have a good day!`,
      ];
    case "STOP":
      return [`Understood β€” you will not hear from me again. Sorry to disturb. πŸ™`];
    default:
      return [
        `Quick context: I build websites for ${lead.niche.toLowerCase()}s β€” like ${PORTFOLIO[2].name} and ${PORTFOLIO[1].name}. Want samples?`,
      ];
  }
}

// ---------- scoring ----------
export const SCORE_DELTA: Record<Intent, number> = {
  YES: 25,
  INTEREST: 15,
  PRICE: 10,
  TIME: 10,
  NEUTRAL: 2,
  NO: -15,
  STOP: -100,
};

export function applyScore(lead: Lead, intent: Intent): number {
  lead.score = Math.max(0, Math.min(100, lead.score + SCORE_DELTA[intent]));
  return lead.score;
}

export function decideStatus(lead: Lead): LeadStatus {
  if (lead.score <= 20) return "CANCELLED";
  if (lead.score >= 65 && lead.turns >= 2) return "READY";
  if (lead.turns >= 5) return lead.score >= 35 ? "WAITING" : "CANCELLED";
  return "IN_CHAT";
}

/** Farewell / hand-off message appended when a terminal status is set. */
export function farewellFor(status: LeadStatus, lead: Lead): string | null {
  switch (status) {
    case "READY":
      return [
        `Perfect ${lead.owner} ji β€” I am sending the full proposal on WhatsApp now πŸ“²`,
        lead.callSlot
          ? `My developer Varun will call you at your allocated slot: ${lead.callSlot}. Thank you!`
          : `My developer Varun will call you shortly to lock the design. Thank you!`,
      ].join("\n");
    case "WAITING":
      return [
        `No rush ${lead.owner} ji β€” I will follow up next week with more samples. πŸ™`,
      ].join("\n");
    case "CANCELLED":
      return `Thanks for your time ${lead.owner} ji πŸ™ All the best for ${lead.business}!`;
    default:
      return null;
  }
}

/** Human label used in timeline events. */
export const INTENT_LABEL: Record<Intent, string> = {
  YES: "agreed to proceed",
  NO: "declined",
  STOP: "asked to stop",
  PRICE: "asked for price",
  TIME: "asked for timeline",
  INTEREST: "asked for samples",
  NEUTRAL: "neutral reply",
};