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.
Global-sales/wai-server/api/channels/channel.controller.js

69 lines
2.4 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const { sessionService } = require('../../core'); // Import from core/index.js
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
const generateId = require('../../utils/generateId.util');
const waInstance = {
wa: null,
};
exports.newConnect = async ctx => {
try {
const { phone } = ctx.query;
const whatsApp1 = await createWhatsApp(phone);
// console.info(whatsApp1);
whatsApp1.on('connection.open', async sock => {
console.info('收到connection.open');
// await sock.sendMessage('8617607730395' + '@s.whatsapp.net', { text: '收到事件,发送消息' });
await whatsApp1.sendTextMessage('8617607730395', '收到事件,发送消息');
});
const qr = await whatsApp1.start();
waInstance.wa = whatsApp1;
ctx.assert(whatsApp1, 503, 'No available connections');
const { sessionId } = sessionService.createSession(phone, whatsApp1);
return { qr };
} 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.testSend = async ctx => {
const { from, to, content } = ctx.request.body;
waInstance.wa.sendTextMessage(to, content);
return { waInstance, ret: 'Message sent successfully' };
};
exports.getAll = async ctx => {
const sessions = sessionService.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
};