Pepguy commited on
Commit
29def84
·
verified ·
1 Parent(s): 099148d

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +27 -4
app.js CHANGED
@@ -22,6 +22,9 @@ const log = (tag, ...args) => console.log(`[${new Date().toISOString()}] [${tag}
22
  let pendingCalls = new Map();
23
  let activeLeadIds = new Map();
24
 
 
 
 
25
  // ── MU-LAW VAD ──
26
  const MU_LAW_TABLE = new Int16Array(256);
27
  for (let i = 0; i < 256; i++) {
@@ -58,7 +61,7 @@ app.post("/api/call-action", async (req, res) => {
58
  const { call_control_id, action, lead_id } = req.body;
59
  if (!pendingCalls.has(call_control_id)) return res.status(404).json({ error: "Call not pending" });
60
 
61
- activeLeadIds.set(call_control_id, lead_id);
62
  pendingCalls.set(call_control_id, action);
63
 
64
  if (action === "reject") {
@@ -75,6 +78,16 @@ app.post("/api/hangup", async (req, res) => {
75
  res.json({ ok: true });
76
  });
77
 
 
 
 
 
 
 
 
 
 
 
78
  app.post("/dial", async (req, res) => {
79
  const { to, lead_id, action = "manual" } = req.body;
80
  log("DIAL", `Outbound to ${to} (Mode: ${action})`);
@@ -109,13 +122,23 @@ app.post("/webhook/call", async (req, res) => {
109
  switch (type) {
110
  case "call.initiated":
111
  if (payload?.direction === "incoming") {
112
- if (pendingCalls.has(callId)) return; // DEDUPLICATE SPAM FROM TELNYX FAILOVER
 
113
 
114
- log("INCOMING", `From: ${payload.from}`);
 
 
 
 
 
 
 
 
115
  pendingCalls.set(callId, "waiting");
 
116
  await fetch(`${AI_SERVER_URL}/api/incoming-call`, {
117
  method: "POST", headers: { "Content-Type": "application/json" },
118
- body: JSON.stringify({ call_control_id: callId, from: payload.from })
119
  }).catch(()=>{});
120
  }
121
  break;
 
22
  let pendingCalls = new Map();
23
  let activeLeadIds = new Map();
24
 
25
+ // 10-Second Debounce Buffer for Telnyx Incoming Webhook Spam
26
+ const incomingCallDebounce = new Map();
27
+
28
  // ── MU-LAW VAD ──
29
  const MU_LAW_TABLE = new Int16Array(256);
30
  for (let i = 0; i < 256; i++) {
 
61
  const { call_control_id, action, lead_id } = req.body;
62
  if (!pendingCalls.has(call_control_id)) return res.status(404).json({ error: "Call not pending" });
63
 
64
+ if (lead_id) activeLeadIds.set(call_control_id, lead_id);
65
  pendingCalls.set(call_control_id, action);
66
 
67
  if (action === "reject") {
 
78
  res.json({ ok: true });
79
  });
80
 
81
+ app.post("/api/send-sms", async (req, res) => {
82
+ try {
83
+ const response = await fetch("https://api.telnyx.com/v2/messages", {
84
+ method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${TELNYX_API_KEY}` },
85
+ body: JSON.stringify({ from: TELNYX_FROM_NUMBER, to: req.body.to, text: req.body.text })
86
+ });
87
+ res.json(await response.json());
88
+ } catch(e) { res.status(500).json({ error: e.message }); }
89
+ });
90
+
91
  app.post("/dial", async (req, res) => {
92
  const { to, lead_id, action = "manual" } = req.body;
93
  log("DIAL", `Outbound to ${to} (Mode: ${action})`);
 
122
  switch (type) {
123
  case "call.initiated":
124
  if (payload?.direction === "incoming") {
125
+ const fromNumber = payload.from;
126
+ const now = Date.now();
127
 
128
+ if (incomingCallDebounce.has(fromNumber)) {
129
+ if (now - incomingCallDebounce.get(fromNumber) < 10000) {
130
+ log("INCOMING DEBOUNCED", `Ignoring duplicate payload from ${fromNumber}`);
131
+ return;
132
+ }
133
+ }
134
+ incomingCallDebounce.set(fromNumber, now);
135
+
136
+ log("INCOMING", `From: ${fromNumber}`);
137
  pendingCalls.set(callId, "waiting");
138
+
139
  await fetch(`${AI_SERVER_URL}/api/incoming-call`, {
140
  method: "POST", headers: { "Content-Type": "application/json" },
141
+ body: JSON.stringify({ call_control_id: callId, from: fromNumber })
142
  }).catch(()=>{});
143
  }
144
  break;