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.
212 lines • 10.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaloBusiness = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ZaloBusinessDescription_1 = require("./ZaloBusinessDescription");
const zca_js_1 = require("zca-js");
const branding_1 = require("../shared/branding");
const utils_1 = require("./utils");
let api;
class ZaloBusiness {
constructor() {
this.description = {
displayName: 'Zalo Business by diginno.net',
name: 'zaloBusiness',
icon: 'file:../shared/image.svg',
group: ['Zalo'],
version: 1,
subtitle: '={{$parameter["resource"] + ": " + $parameter["operation"]}}',
description: 'Quản lý tự động trả lời, danh mục, sản phẩm và hồ sơ doanh nghiệp Zalo Official Account by diginno.net',
defaults: {
name: 'Zalo Business by diginno.net',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'zaloApi',
required: true,
displayName: 'Zalo Credential to connect with',
},
],
properties: [branding_1.BRANDING_CONFIG, ...ZaloBusinessDescription_1.zaloBusinessOperations, ...ZaloBusinessDescription_1.zaloBusinessFields],
};
}
async execute() {
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 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(), 'No API instance found. Please make sure to provide valid credentials.');
}
for (let i = 0; i < items.length; i++) {
try {
let result;
if (resource === 'autoReply') {
switch (operation) {
case 'create':
result = await (0, utils_1.processCreateAutoReply)(api, {
content: this.getNodeParameter('autoReplyContent', i),
isEnable: this.getNodeParameter('autoReplyEnable', i, true),
startTime: new Date(this.getNodeParameter('autoReplyStartTime', i)).getTime(),
endTime: new Date(this.getNodeParameter('autoReplyEndTime', i)).getTime(),
scope: this.getNodeParameter('autoReplyScope', i, 0),
uids: this.getNodeParameter('autoReplyUids', i, ''),
});
break;
case 'update':
result = await (0, utils_1.processUpdateAutoReply)(api, {
id: this.getNodeParameter('autoReplyId', i),
content: this.getNodeParameter('autoReplyContent', i),
isEnable: this.getNodeParameter('autoReplyEnable', i, true),
startTime: new Date(this.getNodeParameter('autoReplyStartTime', i)).getTime(),
endTime: new Date(this.getNodeParameter('autoReplyEndTime', i)).getTime(),
scope: this.getNodeParameter('autoReplyScope', i, 0),
uids: this.getNodeParameter('autoReplyUids', i, ''),
});
break;
case 'delete':
result = await (0, utils_1.processDeleteAutoReply)(api, {
id: this.getNodeParameter('autoReplyId', i),
});
break;
case 'list':
result = await (0, utils_1.processGetAutoReplyList)(api);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'catalog') {
switch (operation) {
case 'create':
result = await (0, utils_1.processCreateCatalog)(api, {
catalogName: this.getNodeParameter('catalogName', i),
});
break;
case 'update':
result = await (0, utils_1.processUpdateCatalog)(api, {
catalogId: this.getNodeParameter('catalogId', i),
catalogName: this.getNodeParameter('catalogName', i),
});
break;
case 'delete':
result = await (0, utils_1.processDeleteCatalog)(api, {
catalogId: this.getNodeParameter('catalogId', i),
});
break;
case 'list':
result = await (0, utils_1.processGetCatalogList)(api, {
limit: this.getNodeParameter('catalogLimit', i, 20),
page: this.getNodeParameter('catalogPage', i, 0),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'product') {
switch (operation) {
case 'create':
result = await (0, utils_1.processCreateProduct)(api, {
catalogId: this.getNodeParameter('productCatalogId', i),
productName: this.getNodeParameter('productName', i),
price: this.getNodeParameter('productPrice', i),
description: this.getNodeParameter('productDescription', i, ''),
productPhotos: this.getNodeParameter('productPhotos', i, ''),
});
break;
case 'update':
result = await (0, utils_1.processUpdateProduct)(api, {
catalogId: this.getNodeParameter('productCatalogId', i),
productId: this.getNodeParameter('productId', i),
productName: this.getNodeParameter('productName', i),
price: this.getNodeParameter('productPrice', i),
description: this.getNodeParameter('productDescription', i, ''),
createTime: this.getNodeParameter('productCreateTime', i),
productPhotos: this.getNodeParameter('productPhotos', i, ''),
});
break;
case 'delete':
result = await (0, utils_1.processDeleteProduct)(api, {
catalogId: this.getNodeParameter('productCatalogId', i),
productIds: this.getNodeParameter('productIds', i),
});
break;
case 'list':
result = await (0, utils_1.processGetProductList)(api, {
catalogId: this.getNodeParameter('productCatalogId', i),
limit: this.getNodeParameter('productLimit', i, 50),
page: this.getNodeParameter('productPage', i, 0),
});
break;
case 'uploadPhoto':
result = await (0, utils_1.processUploadProductPhoto)(api, {
fileUrl: this.getNodeParameter('uploadPhotoUrl', i),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'profile') {
switch (operation) {
case 'get':
result = await (0, utils_1.processGetBizAccount)(api, {
friendId: this.getNodeParameter('bizFriendId', 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.ZaloBusiness = ZaloBusiness;
//# sourceMappingURL=ZaloBusiness.node.js.map