You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.0 KiB
JavaScript
59 lines
2.0 KiB
JavaScript
const { websocketService, websocketManager } = require('../../core'); // Import from core/index.js
|
|
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
|
|
const generateId = require('../../utils/generateId.util');
|
|
// const { createConnection, getConnection } = require('../../core/whatsapp/connection');
|
|
// const { getAvailableConnection } = require('../../core/whatsapp/sessionStore');
|
|
|
|
exports.newConnect = async ctx => {
|
|
try {
|
|
const { phone } = ctx.query;
|
|
// return phone;
|
|
const connectId = generateId();
|
|
const whatsApp1 = await createWhatsApp(connectId, phone);
|
|
ctx.assert(whatsApp1, 503, 'No available connections');
|
|
const { sessionId } = websocketService.createSession(phone, whatsApp1);
|
|
return { sessionId, whatsApp1 };
|
|
} catch (error) {
|
|
console.error('create connection error', error);
|
|
ctx.assert(null, 500, 'Failed to create connection or generate QR code.');
|
|
|
|
// const wa = findConnection('from');
|
|
// wa.senedTextMessage(to, text);
|
|
}
|
|
};
|
|
|
|
exports.getAll = async ctx => {
|
|
const sessions = websocketService.sessions;
|
|
return Array.from(sessions);
|
|
};
|
|
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
exports.getIn = async ctx => {
|
|
// Wait for at least one connection to be established (or handle the case where no connections are available)
|
|
// await new Promise(resolve => {
|
|
// const checkConnections = () => {
|
|
// if (websocketManager.getConnections().length > 0) {
|
|
// resolve();
|
|
// } else {
|
|
// setTimeout(checkConnections, 100); // Check again after 100ms
|
|
// }
|
|
// };
|
|
// checkConnections();
|
|
// });
|
|
|
|
const availableWs = websocketManager.getAvailableConnection();
|
|
|
|
if (!availableWs) {
|
|
// ctx.status = 503;
|
|
// ctx.body = { message: 'No available connections' };
|
|
ctx.assert(availableWs, 503, 'No available connections');
|
|
return;
|
|
}
|
|
// return availableWs;
|
|
|
|
const { sessionId, url } = websocketService.createSession(availableWs);
|
|
return { sessionId, url, message: 'Connection established' }; // availableWs
|
|
};
|