n8n-nodes-zalo-tools
Version:
Các node hỗ trợ Zalo cho n8n
227 lines • 10.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaloGroup = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ZaloGroupDescription_1 = require("./ZaloGroupDescription");
const zca_js_1 = require("zca-js");
const helper_1 = require("../utils/helper");
class ZaloGroup {
constructor() {
this.description = {
displayName: 'Zalo Group',
name: 'zaloGroup',
icon: 'file:../shared/zalo.svg',
group: ['Zalo'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Quản lý nhóm Zalo',
defaults: {
name: 'Zalo Group',
},
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: 'Group',
value: 'zaloGroup',
},
],
default: 'zaloGroup',
},
...ZaloGroupDescription_1.zaloGroupOperations,
...ZaloGroupDescription_1.zaloGroupFields,
],
};
}
async execute() {
var _a, _b, _c, _d;
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({ imageMetadataGetter: helper_1.getImageMetadata });
const api = await zalo.login({ cookie, imei, userAgent });
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 === 'zaloGroup') {
if (operation === 'createGroup') {
const groupName = this.getNodeParameter('groupName', i);
const userIds = this.getNodeParameter('userIds', i);
const userList = userIds.split(',');
const response = await api.createGroup({ name: groupName, members: userList });
returnData.push({
json: response,
pairedItem: {
item: i,
},
});
}
else if (operation === 'getGroupInfo') {
const groupId = this.getNodeParameter('groupId', i);
const response = await api.getGroupInfo(groupId);
const groupInfo = response.gridInfoMap[groupId];
returnData.push({
json: {
response: response,
groupInfo: groupInfo,
},
pairedItem: {
item: i,
},
});
}
else if (operation === 'addGroupDeputy') {
const groupId = this.getNodeParameter('groupId', i);
const userId = this.getNodeParameter('userId', i);
const response = await api.addGroupDeputy(groupId, userId);
returnData.push({
json: {
status: 'Thành công',
response: response,
},
pairedItem: {
item: i,
},
});
}
else if (operation === 'addUserToGroup') {
const groupId = this.getNodeParameter('groupId', i);
const userIds = this.getNodeParameter('userIds', i);
const userList = userIds.split(',');
const response = await api.addUserToGroup(userList, groupId);
returnData.push({
json: response,
pairedItem: {
item: i,
},
});
}
else if (operation === 'changeGroupAvatar') {
const groupId = this.getNodeParameter('groupId', i);
const imageUrl = this.getNodeParameter('imageUrl', i);
const response = await api.changeGroupAvatar(groupId, imageUrl);
returnData.push({
json: {
status: 'Thành công',
response: response,
},
pairedItem: {
item: i,
},
});
}
else if (operation === 'changeGroupName') {
const groupId = this.getNodeParameter('groupId', i);
const newName = this.getNodeParameter('newName', i);
const response = await api.changeGroupName(groupId, newName);
returnData.push({
json: response,
pairedItem: {
item: i,
},
});
}
else if (operation === 'getGroupMembers') {
const groupId = this.getNodeParameter('groupId', i);
const limit = this.getNodeParameter('limit', i);
const response = await api.getGroupInfo(groupId);
const groupInfo = response.gridInfoMap[groupId];
const members = ((_d = groupInfo.memberIds) === null || _d === void 0 ? void 0 : _d.slice(0, limit)) || [];
const admins = groupInfo.adminIds || [];
const currentMems = groupInfo.currentMems || [];
const updateMems = groupInfo.updateMems || [];
const totalMember = groupInfo.totalMember || 0;
returnData.push({
json: { members, admins, currentMems, updateMems, totalMember },
pairedItem: {
item: i,
},
});
}
else if (operation === 'getAllGroups') {
const response = await api.getAllGroups();
returnData.push({
json: { response },
pairedItem: {
item: i,
},
});
}
else if (operation === 'removeUserFromGroup') {
const groupId = this.getNodeParameter('groupId', i);
const userIds = this.getNodeParameter('userIds', i);
const userList = userIds.split(',');
const response = await api.removeUserFromGroup(userList, groupId);
returnData.push({
json: response,
pairedItem: {
item: i,
},
});
}
else if (operation === 'createNote') {
const groupId = this.getNodeParameter('groupId', i);
const content = this.getNodeParameter('content', i);
const pinAct = this.getNodeParameter('pinAct', i);
const options = {
title: content,
pinAct: pinAct,
};
const response = await api.createNote(options, groupId);
returnData.push({
json: {
status: 'Thành công',
response: 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.ZaloGroup = ZaloGroup;
//# sourceMappingURL=ZaloGroup.node.js.map