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.

214 lines 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZaloConversation = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ZaloConversation_properties_1 = require("./ZaloConversation.properties"); const zca_js_1 = require("zca-js"); const utils_1 = require("./utils"); let api; class ZaloConversation { constructor() { this.description = { displayName: 'Zalo Hội Thoại by diginno.net', name: 'zaloConversation', icon: 'file:../shared/image.svg', group: ['Zalo'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Quản lý hội thoại Zalo: Ẩn, Ghim, Tắt thông báo, Tự động xóa by diginno.net', defaults: { name: 'Zalo Hội Thoại by diginno.net', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'zaloApi', required: true, displayName: 'Zalo Credential to connect with', }, ], properties: ZaloConversation_properties_1.zaloConversationProperties, }; } async execute() { var _a, _b, _c; const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); const zaloCred = await this.getCredentials('zaloApi'); let cookieFromCred = zaloCred.cookie; try { cookieFromCred = JSON.parse(zaloCred.cookie); } catch { } const imeiFromCred = zaloCred.imei; const userAgentFromCred = zaloCred.userAgent; const cookie = cookieFromCred !== null && cookieFromCred !== void 0 ? cookieFromCred : (_a = items.find((x) => x.json.cookie)) === null || _a === void 0 ? void 0 : _a.json.cookie; const imei = imeiFromCred !== null && imeiFromCred !== void 0 ? imeiFromCred : (_b = items.find((x) => x.json.imei)) === null || _b === void 0 ? void 0 : _b.json.imei; const userAgent = userAgentFromCred !== null && userAgentFromCred !== void 0 ? userAgentFromCred : (_c = items.find((x) => x.json.userAgent)) === null || _c === void 0 ? void 0 : _c.json.userAgent; const zalo = new zca_js_1.Zalo(); const _api = await zalo.login({ cookie: cookie, imei, userAgent }); api = _api; if (!api) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No API instance found. Please make sure to provide valid credentials.'); } for (let i = 0; i < items.length; i++) { try { let result; if (resource === 'hidden') { switch (operation) { case 'setHidden': result = await (0, utils_1.processSetHiddenConversation)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), isHidden: this.getNodeParameter('isHidden', i), }); break; case 'getHidden': result = await (0, utils_1.processGetHiddenConversations)(api, { pin: this.getNodeParameter('pin', i), }); break; case 'updatePin': result = await (0, utils_1.processUpdateHiddenConversPin)(api, { threadId: this.getNodeParameter('threadId', i), pin: this.getNodeParameter('pin', i), }); break; case 'resetPin': result = await (0, utils_1.processResetHiddenConversPin)(api, { oldPin: this.getNodeParameter('oldPin', i), newPin: this.getNodeParameter('newPin', i), }); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i }); } } else if (resource === 'unread') { switch (operation) { case 'addMark': result = await (0, utils_1.processAddUnreadMark)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), }); break; case 'removeMark': result = await (0, utils_1.processRemoveUnreadMark)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), }); break; case 'getMarks': result = await (0, utils_1.processGetUnreadMark)(api); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i }); } } else if (resource === 'pinArchive') { switch (operation) { case 'setPinned': result = await (0, utils_1.processSetPinnedConversations)(api, { threadIds: this.getNodeParameter('threadIds', i), threadType: this.getNodeParameter('threadType', i), isPinned: this.getNodeParameter('isPinned', i), }); break; case 'getPinned': result = await (0, utils_1.processGetPinConversations)(api); break; case 'getArchived': result = await (0, utils_1.processGetArchivedChatList)(api, { offset: this.getNodeParameter('offset', i), count: this.getNodeParameter('count', i), }); break; case 'updateArchived': result = await api.updateArchivedChatList(this.getNodeParameter('threadId', i), this.getNodeParameter('threadType', i), this.getNodeParameter('action', i)); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i }); } } else if (resource === 'autoDelete') { switch (operation) { case 'update': result = await (0, utils_1.processUpdateAutoDeleteChat)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), ttl: this.getNodeParameter('ttl', i), }); break; case 'get': result = await (0, utils_1.processGetAutoDeleteChat)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), }); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i }); } } else if (resource === 'chatManagement') { switch (operation) { case 'deleteChat': result = await (0, utils_1.processDeleteChat)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), }); break; case 'setMute': result = await (0, utils_1.processSetMute)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), duration: this.getNodeParameter('duration', i), }); break; case 'getMute': result = await (0, utils_1.processGetMute)(api, { threadId: this.getNodeParameter('threadId', i), threadType: this.getNodeParameter('threadType', i), }); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i }); } } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown resource: ${resource}`, { itemIndex: i, }); } returnData.push({ json: result, pairedItem: { item: i, }, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, }, pairedItem: { item: i, }, }); continue; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i, }); } } return [returnData]; } } exports.ZaloConversation = ZaloConversation; //# sourceMappingURL=ZaloConversation.node.js.map