UNPKG

n8n-nodes-zalo-tools

Version:
210 lines 9.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZaloUser = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ZaloUserDescription_1 = require("./ZaloUserDescription"); const zca_js_1 = require("zca-js"); let api; class ZaloUser { constructor() { this.description = { displayName: 'Zalo User', name: 'zaloUser', icon: 'file:../shared/zalo.svg', group: ['Zalo'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Quản lý người dùng Zalo', defaults: { name: 'Zalo User', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'zaloApi', required: true, displayName: 'Zalo Credential to connect with', }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Zalo User', value: 'zaloUser', }, ], default: 'zaloUser', }, ...ZaloUserDescription_1.zaloUserOperations, ...ZaloUserDescription_1.zaloUserFields, ], }; } 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'); const cookieFromCred = JSON.parse(zaloCred.cookie); 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, 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 { if (resource === 'zaloUser') { if (operation === 'acceptFriendRequest') { const userId = this.getNodeParameter('userId', i); const response = await api.acceptFriendRequest(userId); returnData.push({ json: { status: "Thành công", response: response, }, pairedItem: { item: i, }, }); } else if (operation === 'sendFriendRequest') { const userId = this.getNodeParameter('userId', i); const message = this.getNodeParameter('message', i); const response = await api.sendFriendRequest(message, userId); returnData.push({ json: { status: "Thành công", response: response, }, pairedItem: { item: i, }, }); } else if (operation === 'blockUser') { const userId = this.getNodeParameter('userId', i); const response = await api.blockUser(userId); returnData.push({ json: { status: "Thành công", response: response, }, pairedItem: { item: i, }, }); } else if (operation === 'unblockUser') { const userId = this.getNodeParameter('userId', i); const response = await api.unblockUser(userId); returnData.push({ json: { status: "Thành công", response: response, }, pairedItem: { item: i, }, }); } else if (operation === 'changeAccountAvatar') { const userId = this.getNodeParameter('userId', i); const filePath = this.getNodeParameter('filePath', i); const response = await api.changeAccountAvatar(userId, filePath); returnData.push({ json: { status: "Thành công", response: response, }, pairedItem: { item: i, }, }); } else if (operation === 'changeAccountSetting') { const name = this.getNodeParameter('name', i); const dob = this.getNodeParameter('dob', i); const gender = this.getNodeParameter('gender', i); const language = this.getNodeParameter('language', i); const response = await api.changeAccountSetting(name, dob, gender, language); returnData.push({ json: { status: "Thành công", response: response, }, pairedItem: { item: i, }, }); } else if (operation === 'getUserInfo') { const userId = this.getNodeParameter('userId', i); const response = await api.getUserInfo(userId); returnData.push({ json: response, pairedItem: { item: i, }, }); } else if (operation === 'getAllFriends') { const limit = this.getNodeParameter('limit', i); const response = await api.getAllFriends(); const friends = response.slice(0, limit) || []; returnData.push({ json: { friends: friends, }, pairedItem: { item: i, }, }); } else if (operation === 'findUser') { const phoneNumber = this.getNodeParameter('phoneNumber', i); const response = await api.findUser(phoneNumber); returnData.push({ json: response, 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.ZaloUser = ZaloUser; //# sourceMappingURL=ZaloUser.node.js.map