UNPKG

n8n-nodes-zalo-nnt

Version:

Unofficial Zalo integration for n8n - Send messages, manage groups, user operations with QR login. No API key required, works via browser simulation.

252 lines 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventListenerRegistry = void 0; const constants_1 = require("./constants"); class EventListenerRegistry { static registerAll(hookFunctions, apiInstance, webhookData, eventTypes, filter) { if (eventTypes.includes(constants_1.EVENT_TYPES.MESSAGE)) { this.registerMessageListener(hookFunctions, apiInstance, webhookData, filter); } if (eventTypes.includes(constants_1.EVENT_TYPES.REACTION)) { this.registerReactionListener(hookFunctions, apiInstance, webhookData, filter); } if (eventTypes.includes(constants_1.EVENT_TYPES.TYPING)) { this.registerTypingListener(hookFunctions, apiInstance, webhookData, filter); } if (eventTypes.includes(constants_1.EVENT_TYPES.SEEN_MESSAGES)) { this.registerSeenMessagesListener(hookFunctions, apiInstance, webhookData); } if (eventTypes.includes(constants_1.EVENT_TYPES.DELIVERED_MESSAGES)) { this.registerDeliveredMessagesListener(hookFunctions, apiInstance, webhookData); } if (eventTypes.includes(constants_1.EVENT_TYPES.UNDO)) { this.registerUndoListener(hookFunctions, apiInstance, webhookData); } if (eventTypes.includes(constants_1.EVENT_TYPES.GROUP_EVENT)) { this.registerGroupEventListener(hookFunctions, apiInstance, webhookData); } if (eventTypes.includes(constants_1.EVENT_TYPES.FRIEND_EVENT)) { this.registerFriendEventListener(hookFunctions, apiInstance, webhookData); } this.registerConnectionListeners(hookFunctions, apiInstance, webhookData, eventTypes); } static registerMessageListener(hookFunctions, apiInstance, webhookData, filter) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('message', async (message) => { if (!filter.shouldProcessMessage(message)) { return; } const event = { type: constants_1.EVENT_TYPES.MESSAGE, data: message, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerReactionListener(hookFunctions, apiInstance, webhookData, filter) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('reaction', async (reaction) => { if (!filter.shouldProcessReaction(reaction)) { return; } const event = { type: constants_1.EVENT_TYPES.REACTION, data: reaction, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerTypingListener(hookFunctions, apiInstance, webhookData, filter) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('typing', async (typing) => { if (!filter.shouldProcessTyping(typing)) { return; } const event = { type: constants_1.EVENT_TYPES.TYPING, data: typing, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerSeenMessagesListener(hookFunctions, apiInstance, webhookData) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('seen_messages', async (messages) => { const event = { type: constants_1.EVENT_TYPES.SEEN_MESSAGES, data: messages, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerDeliveredMessagesListener(hookFunctions, apiInstance, webhookData) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('delivered_messages', async (messages) => { const event = { type: constants_1.EVENT_TYPES.DELIVERED_MESSAGES, data: messages, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerUndoListener(hookFunctions, apiInstance, webhookData) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('undo', async (undo) => { const event = { type: constants_1.EVENT_TYPES.UNDO, data: undo, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerGroupEventListener(hookFunctions, apiInstance, webhookData) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('group_event', async (event) => { const eventData = { type: constants_1.EVENT_TYPES.GROUP_EVENT, data: event, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: eventData, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerFriendEventListener(hookFunctions, apiInstance, webhookData) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('friend_event', async (event) => { const eventData = { type: constants_1.EVENT_TYPES.FRIEND_EVENT, data: event, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: eventData, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); }); } static registerConnectionListeners(hookFunctions, apiInstance, webhookData, eventTypes) { const webhookUrl = hookFunctions.getNodeWebhookUrl('default'); apiInstance.listener.on('connected', async () => { webhookData.connectionStatus = 'connected'; webhookData.lastConnectTime = Date.now(); console.log('[ZaloMessageTrigger] Connected successfully'); if (eventTypes.includes(constants_1.EVENT_TYPES.CONNECTION)) { const event = { type: 'connected', data: { status: 'connected' }, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); } }); apiInstance.listener.on('disconnected', async (reason) => { webhookData.connectionStatus = 'disconnected'; webhookData.lastDisconnectTime = Date.now(); webhookData.disconnectReason = reason; console.warn(`[ZaloMessageTrigger] Disconnected: ${reason}`); if (eventTypes.includes(constants_1.EVENT_TYPES.CONNECTION)) { const event = { type: 'disconnected', data: { reason }, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); } }); apiInstance.listener.on('error', async (error) => { webhookData.lastError = String(error); webhookData.lastErrorTime = Date.now(); console.error('[ZaloMessageTrigger] Error:', error); if (eventTypes.includes(constants_1.EVENT_TYPES.CONNECTION)) { const event = { type: 'error', data: { error: String(error) }, timestamp: Date.now(), }; await hookFunctions.helpers.httpRequest({ method: 'POST', url: webhookUrl, body: event, headers: { 'Content-Type': 'application/json' }, }).catch(error => { console.error('[ZaloMessageTrigger] Failed to trigger webhook:', error); }); } }); } } exports.EventListenerRegistry = EventListenerRegistry; //# sourceMappingURL=EventListenerRegistry.js.map