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.

139 lines 5.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZaloQuickMessage = void 0; const n8n_workflow_1 = require("n8n-workflow"); const zca_js_1 = require("zca-js"); const ZaloQuickMessage_properties_1 = require("./ZaloQuickMessage.properties"); const utils_1 = require("./utils"); let api; class ZaloQuickMessage { constructor() { this.description = { displayName: 'Tin Nhắn Nhanh Zalo', name: 'zaloQuickMessage', icon: 'file:../shared/image.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Quản lý tin nhắn nhanh (quick messages) trong Zalo', defaults: { name: 'Tin Nhắn Nhanh Zalo', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'zaloApi', required: true, }, ], properties: ZaloQuickMessage_properties_1.zaloQuickMessageProperties, }; } async execute() { const returnData = []; const items = this.getInputData(); 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; try { const zalo = new zca_js_1.Zalo(); api = await zalo.login({ cookie: cookieFromCred, imei: imeiFromCred, userAgent: userAgentFromCred, }); if (!api) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Failed to initialize Zalo API. Check your credentials.'); } } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Zalo login error: ${error.message}`); } for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); let result; switch (operation) { case 'get': { const skip = this.getNodeParameter('skip', i, 0); const count = this.getNodeParameter('count', i, 20); (0, utils_1.validateGetQuickMessage)(skip, count); const input = { skip, count }; result = await (0, utils_1.getQuickMessages)(api, input); break; } case 'add': { const keyword = this.getNodeParameter('keyword', i); const title = this.getNodeParameter('title', i); const media = this.getNodeParameter('media', i, ''); const input = { keyword, title, ...(media.trim() && { media: media.trim() }), }; (0, utils_1.validateAddQuickMessage)(input); result = await (0, utils_1.addQuickMessage)(api, input); break; } case 'update': { const itemId = this.getNodeParameter('itemId', i); const keyword = this.getNodeParameter('keyword', i); const title = this.getNodeParameter('title', i); const media = this.getNodeParameter('media', i, ''); const input = { itemId, keyword, title, ...(media.trim() && { media: media.trim() }), }; (0, utils_1.validateUpdateQuickMessage)(input); result = await (0, utils_1.updateQuickMessage)(api, input); break; } case 'remove': { const itemIdsRaw = this.getNodeParameter('itemIds', i); const itemIds = itemIdsRaw .split(',') .map((id) => id.trim()) .filter((id) => id) .map((id) => Number(id)); const input = { itemIds }; (0, utils_1.validateRemoveQuickMessage)(input); result = await (0, utils_1.removeQuickMessage)(api, input); break; } default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation}`); } returnData.push({ json: result, pairedItem: { item: i }, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { success: false, error: error.message, }, pairedItem: { item: i }, }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i }); } } } return [returnData]; } } exports.ZaloQuickMessage = ZaloQuickMessage; //# sourceMappingURL=ZaloQuickMessage.node.js.map