Deploy: 2026-06-24 15:01:02

This commit is contained in:
Hamza-Ayed
2026-06-24 15:01:02 +03:00
parent 0264e3e90e
commit 23d9cfc506
4 changed files with 112 additions and 100 deletions

View File

@@ -20,7 +20,7 @@ for (const p of envPaths) {
const express = require('express');
const cors = require('cors');
const { startSession, disconnectSession, sendMessage, getActiveSessions, checkContact } = require('./puppeteer-client');
const { startSession, disconnectSession, sendMessage, getActiveSessions, checkContact, isSessionReady } = require('./puppeteer-client');
const app = express();
app.use(cors());
@@ -84,12 +84,25 @@ app.get('/api/sessions/active', (req, res) => {
// Check if contact is on WhatsApp
app.post('/api/contacts/check', async (req, res) => {
const { session_key, phone } = req.body;
let { session_key, phone } = req.body;
if (!session_key || !phone) {
return res.status(400).json({ error: 'Missing session_key or phone' });
}
if (session_key === 'auto') {
const activeSlots = [];
for (let i = 1; i <= 6; i++) {
if (isSessionReady(`slot-${i}`)) {
activeSlots.push(`slot-${i}`);
}
}
if (activeSlots.length === 0) {
return res.status(503).json({ error: 'No WhatsApp slots are ready' });
}
session_key = activeSlots[0]; // Just use the first available slot for checking
}
try {
const result = await checkContact(session_key, phone);
res.json({ status: 'success', data: result });
@@ -100,8 +113,10 @@ app.post('/api/contacts/check', async (req, res) => {
});
// Send outbound message
let currentSlotIndex = 1;
app.post('/api/messages/send', async (req, res) => {
const { session_key, phone, message, media_url, audio, mimetype, image } = req.body;
let { session_key, phone, message, media_url, audio, mimetype, image } = req.body;
if (!session_key || !phone) {
return res.status(400).json({ error: 'Missing session_key or phone' });
@@ -112,6 +127,24 @@ app.post('/api/messages/send', async (req, res) => {
}
try {
if (session_key === 'auto') {
const activeSlots = [];
for (let i = 1; i <= 6; i++) {
if (isSessionReady(`slot-${i}`)) {
activeSlots.push(`slot-${i}`);
}
}
if (activeSlots.length === 0) {
return res.status(503).json({ error: 'No WhatsApp slots are currently ready to send messages' });
}
// Round-robin selection
session_key = activeSlots[currentSlotIndex % activeSlots.length];
currentSlotIndex++;
console.log(`[RoundRobin] Selected ${session_key} out of ${activeSlots.length} active slots.`);
}
const result = await sendMessage(session_key, phone, message, media_url, audio, mimetype, image);
res.json({ status: 'success', data: result });
} catch (err) {
@@ -120,10 +153,12 @@ app.post('/api/messages/send', async (req, res) => {
}
});
// Auto-start default session
// Auto-start default sessions (6 slots)
setTimeout(() => {
console.log('🔄 Auto-starting default session: flash_call_otp');
startSession('flash_call_otp', 'https://otp.intaleqapp.com/api/whatsapp-webhook.php').catch(console.error);
console.log('🔄 Auto-starting 6 WhatsApp slots...');
for (let i = 1; i <= 6; i++) {
startSession(`slot-${i}`, 'https://otp.intaleqapp.com/api/whatsapp-webhook.php').catch(console.error);
}
}, 2000);
app.listen(PORT, () => {