emit webhook
parent
8300b5f957
commit
c5f413f543
@ -0,0 +1,11 @@
|
||||
const EventEmitter = require('events');
|
||||
module.exports = new EventEmitter();
|
||||
|
||||
/**
|
||||
* 监听事件
|
||||
* whatsapp EventEmitter:
|
||||
* - connection:removed
|
||||
* - connection:added
|
||||
* - message:received
|
||||
* - message:updated
|
||||
*/
|
@ -1,14 +1,11 @@
|
||||
// core/index.js
|
||||
const websocketServicesI = require('./websocket/services/session');
|
||||
const websocketConnectionI = require('./websocket/connection');
|
||||
const sessionServicesI = require('./services/session');
|
||||
|
||||
// Create the instances here
|
||||
const websocketService = websocketServicesI();
|
||||
const websocketManager = websocketConnectionI(websocketService);
|
||||
const sessionService = sessionServicesI();
|
||||
const createWhatsApp = require('./baileys/index');
|
||||
|
||||
module.exports = {
|
||||
websocketService,
|
||||
websocketManager,
|
||||
sessionService,
|
||||
createWhatsApp,
|
||||
};
|
||||
|
@ -0,0 +1,16 @@
|
||||
const whatsappEvents = require('../emitter');
|
||||
const { callWebhook } = require('../webhook');
|
||||
|
||||
const logger = console;
|
||||
|
||||
function setupMessageHandler() {
|
||||
whatsappEvents.on('message:received', async messageData => {
|
||||
try {
|
||||
await callWebhook(messageData);
|
||||
} catch (error) {
|
||||
logger.error({ messageData, error }, 'error call webhook');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { setupMessageHandler };
|
@ -1,4 +1,8 @@
|
||||
const generateId = require('../../../utils/generateId.util');
|
||||
const whatsappEvents = require('../emitter');
|
||||
|
||||
whatsappEvents.on('connection:added', ({ sock, sessionId }) => {});
|
||||
whatsappEvents.on('connection:removed', ({ sessionId }) => {});
|
||||
|
||||
module.exports = () => {
|
||||
const sessions = new Map();
|
@ -0,0 +1,37 @@
|
||||
const axios = require('axios');
|
||||
const axiosRetry = require('axios-retry');
|
||||
const logger = console;
|
||||
const webhookUrl = require('../../config').webhook;
|
||||
|
||||
// 5s, 10s, 20s, 30s, 1m, 15m, 30m, 1h
|
||||
const retryDelays = [5000, 10000, 20000, 30000, 300000, 900000, 1800000, 3600000];
|
||||
axiosRetry(axios, {
|
||||
retries: 8, // Number of retries
|
||||
retryDelay: retryCount => {
|
||||
const delayIndex = Math.min(retryCount - 1, retryDelays.length - 1);
|
||||
const delay = retryDelays[delayIndex];
|
||||
logger.warn(`retry attempt: ${retryCount}, delay: ${delay / 1000}s`);
|
||||
return delay;
|
||||
},
|
||||
retryCondition: error => {
|
||||
return error.response?.status !== 200; // Retry only on non-200 status codes
|
||||
},
|
||||
onRetry: (retryCount, error, requestConfig) => {
|
||||
logger.warn({ retryCount, error, requestConfig }, `Retrying webhook call, attempt ${retryCount}`);
|
||||
},
|
||||
});
|
||||
|
||||
async function callWebhook(messageData) {
|
||||
try {
|
||||
if (!webhookUrl) {
|
||||
logger.info('no webhook url provided');
|
||||
return;
|
||||
}
|
||||
await axios.post(webhookUrl, messageData);
|
||||
logger.info({ webhookUrl: webhookUrl, messageData }, 'Webhook called successfully');
|
||||
} catch (error) {
|
||||
logger.error({ webhookUrl: webhookUrl, messageData, error }, 'Error calling webhook');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { callWebhook };
|
@ -1,60 +0,0 @@
|
||||
const WebSocket = require('ws');
|
||||
const websocketServerNodes = require('../../config').websockets;
|
||||
|
||||
module.exports = callService => {
|
||||
let connections = [];
|
||||
|
||||
const connect = () => {
|
||||
websocketServerNodes.forEach((node, index) => {
|
||||
createConnection(node, index);
|
||||
});
|
||||
};
|
||||
|
||||
const createConnection = (node, index) => {
|
||||
try {
|
||||
const ws = new WebSocket(node.host, [node.protocol]);
|
||||
ws.on('open', () => {
|
||||
console.log(`Connected to WebSocket ${index}: ${node.protocol} ${node.host}`);
|
||||
// connections.push(ws);
|
||||
connections = [...connections, ws];
|
||||
console.log('Current connections:', connections.length);
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log(`WebSocket ${index} disconnected. Reconnecting...`);
|
||||
connections.splice(connections.indexOf(ws), 1);
|
||||
setTimeout(() => createConnection(node, index), 3000);
|
||||
});
|
||||
|
||||
ws.on('error', error => {
|
||||
console.error(`WebSocket ${index} error:`, error);
|
||||
});
|
||||
|
||||
ws.on('message', message => {
|
||||
console.log(`Received from WebSocket ${index}: ${message}`);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error connecting to WebSocket ${index}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
const getAvailableConnection = () => {
|
||||
// console.log('Available connections length:', connections.length);
|
||||
for (const ws of connections) {
|
||||
// console.log('Session has ws:', callService.sessions.has(ws));
|
||||
if (!callService.sessions.has(ws)) {
|
||||
// console.log('Found available ws:', ws); // Add this line
|
||||
return ws;
|
||||
}
|
||||
}
|
||||
console.log('No available connections found.');
|
||||
return null;
|
||||
};
|
||||
const getConnections = () => connections; // Add this getter
|
||||
|
||||
return {
|
||||
connect,
|
||||
getAvailableConnection,
|
||||
getConnections,
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue