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.
81 lines
2.7 KiB
JavaScript
81 lines
2.7 KiB
JavaScript
'use strict';
|
|
|
|
const { sessionStore } = require('../../core'); // Import from core/index.js
|
|
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
|
|
const { getConnection } = require('../../services/connections.service');
|
|
const { isEmpty } = require('../../utils/commons.util');
|
|
const { getUserLogger } = require('../../utils/logger.util');
|
|
const { domain } = require('../../config').server;
|
|
const waEmitter = require('../../core/emitter');
|
|
|
|
const newConnect = async ctx => {
|
|
const { phone } = ctx.query;
|
|
const existsSession = sessionStore.getSession(phone);
|
|
if (!isEmpty(existsSession)) {
|
|
ctx.assert(null, 400, `WhatsApp ${phone} 已连接`);
|
|
// return { phone, session_id: phone, ...existsSession, stauts: 'open', wai_server: domain };
|
|
}
|
|
try {
|
|
getUserLogger(phone).info(`WhatsApp ${phone} 正在连接`);
|
|
const whatsApp1 = await createWhatsApp(phone);
|
|
ctx.assert(whatsApp1, 503, 'No available connections');
|
|
whatsApp1.start();
|
|
sessionStore.createSession(phone, whatsApp1);
|
|
return { phone, session_id: phone, wai_server: domain, ...whatsApp1 };
|
|
} catch (error) {
|
|
console.error('create connection error', error);
|
|
ctx.assert(null, 500, 'Failed to create connection or generate QR code.');
|
|
}
|
|
};
|
|
|
|
const getAll = async () => {
|
|
const findConnection = await getConnection({});
|
|
return findConnection;
|
|
};
|
|
|
|
const getSessions = async () => {
|
|
return Array.from(sessionStore.sessions);
|
|
};
|
|
|
|
const offline = async ctx => {
|
|
const { phone } = ctx.query;
|
|
const existsSession = sessionStore.getSession(phone);
|
|
ctx.assert(existsSession, 400, `WhatsApp ${phone} 已离线`);
|
|
try {
|
|
getUserLogger(phone).info(`WhatsApp ${phone} 准备离线`);
|
|
// existsSession.stop(); // todo:
|
|
// sessionStore.removeSession(existsSession.channelId);
|
|
waEmitter.emit('connection:close', { phone, whatsAppNo: phone, status: 'offline', channelId: existsSession.channelId });
|
|
return ''; // { wsToSend, ret: 'Message sent successfully' };
|
|
} catch (error) {
|
|
console.error('set offline error', error);
|
|
ctx.assert(null, 500, 'Failed to set offline');
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
const setStatus = async (ctx) => {
|
|
const { phone, status } = ctx.request.body;
|
|
const existsSession = sessionStore.getSession(phone);
|
|
ctx.assert(existsSession, 400, `WhatsApp ${phone} 已离线`);
|
|
try {
|
|
// existsSession.stop(); // todo:
|
|
waEmitter.emit('connection:close', { phone, status: 'offline', channelId: existsSession.channelId });
|
|
} catch (error) {
|
|
console.error('close connection error', error);
|
|
ctx.assert(null, 500, 'Failed to close connection');
|
|
}
|
|
};
|
|
|
|
const test = async () => {};
|
|
|
|
module.exports = {
|
|
newConnect,
|
|
getAll,
|
|
getSessions,
|
|
offline,
|
|
test,
|
|
};
|