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.
361 lines • 19.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaloGroup = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ZaloGroup_properties_1 = require("./ZaloGroup.properties");
const zca_js_1 = require("zca-js");
const utils_1 = require("./utils");
let api;
class ZaloGroup {
constructor() {
this.description = {
displayName: 'Zalo Nhóm by diginno.net',
name: 'zaloGroup',
icon: 'file:../shared/image.svg',
group: ['Zalo'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Quản lý nhóm Zalo by diginno.net',
defaults: {
name: 'Zalo Nhóm by diginno.net',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'zaloApi',
required: true,
displayName: 'Zalo Credential to connect with',
},
],
properties: ZaloGroup_properties_1.zaloGroupProperties,
};
}
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 === 'group') {
switch (operation) {
case 'create':
result = await (0, utils_1.processCreateGroup)(api, {
groupName: this.getNodeParameter('groupName', i),
userIds: this.getNodeParameter('userIds', i),
});
break;
case 'get':
const includeMembers = this.getNodeParameter('includeMembers', i, false);
result = await (0, utils_1.processGetGroupInfo)(api, {
groupId: this.getNodeParameter('groupId', i),
includeMembers,
memberLimit: includeMembers
? this.getNodeParameter('memberLimit', i, 100)
: undefined,
});
break;
case 'getMany':
result = await (0, utils_1.processGetAllGroups)(api, {
limit: this.getNodeParameter('limit', i),
});
break;
case 'join':
result = await (0, utils_1.processJoinGroupLink)(api, {
groupLink: this.getNodeParameter('groupLink', i),
});
break;
case 'leave':
result = await (0, utils_1.processLeaveGroup)(api, {
groupId: this.getNodeParameter('groupId', i),
silent: this.getNodeParameter('silent', i),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'member') {
switch (operation) {
case 'add':
result = await (0, utils_1.processAddUserToGroup)(api, {
groupId: this.getNodeParameter('groupId', i),
userIds: this.getNodeParameter('userIds', i),
});
break;
case 'remove':
result = await (0, utils_1.processRemoveUserFromGroup)(api, {
groupId: this.getNodeParameter('groupId', i),
userIds: this.getNodeParameter('userIds', i),
});
break;
case 'replace':
result = await (0, utils_1.processReplaceGroupMembers)(api, {
groupId: this.getNodeParameter('groupId', i),
oldMembers: this.getNodeParameter('oldMembers', i),
newMembers: this.getNodeParameter('newMembers', i),
});
break;
case 'inviteToMany':
result = await (0, utils_1.processInviteUsers)(api, {
userId: this.getNodeParameter('userId', i),
groupIds: this.getNodeParameter('groupIds', i),
});
break;
case 'promoteDeputy':
result = await (0, utils_1.processAddGroupDeputy)(api, {
groupId: this.getNodeParameter('groupId', i),
userId: this.getNodeParameter('userId', i),
});
break;
case 'removeDeputy':
result = await (0, utils_1.processRemoveGroupDeputy)(api, {
groupId: this.getNodeParameter('groupId', i),
memberIds: this.getNodeParameter('memberIds', i),
});
break;
case 'changeOwner':
result = await (0, utils_1.processChangeGroupOwner)(api, {
groupId: this.getNodeParameter('groupId', i),
memberId: this.getNodeParameter('memberId', i),
});
break;
case 'getMembersInfo':
result = await (0, utils_1.processGetGroupMembersInfo)(api, {
memberIds: this.getNodeParameter('memberIds', i),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'settings') {
switch (operation) {
case 'updateName':
result = await (0, utils_1.processChangeGroupName)(api, {
groupId: this.getNodeParameter('groupId', i),
newName: this.getNodeParameter('newName', i),
});
break;
case 'updateAvatar':
result = await (0, utils_1.processChangeGroupAvatar)(api, {
groupId: this.getNodeParameter('groupId', i),
imageUrl: this.getNodeParameter('imageUrl', i),
});
break;
case 'upgradeToCommunity':
result = await (0, utils_1.processUpgradeGroupToCommunity)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
case 'updateGroupSettings': {
const selected = this.getNodeParameter('groupSettingsOptions', i, []);
const options = Object.fromEntries(selected.map((key) => [key, true]));
result = await (0, utils_1.processUpdateGroupSettings)(api, {
groupId: this.getNodeParameter('groupId', i),
...options,
});
break;
}
case 'disperseGroup':
result = await (0, utils_1.processDisperseGroup)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'moderation') {
switch (operation) {
case 'getBlocked':
result = await (0, utils_1.processGetBlockedMembers)(api, {
groupId: this.getNodeParameter('groupId', i),
page: this.getNodeParameter('page', i),
count: this.getNodeParameter('count', i),
});
break;
case 'block':
result = await (0, utils_1.processBlockMember)(api, {
groupId: this.getNodeParameter('groupId', i),
memberIds: this.getNodeParameter('memberIds', i),
});
break;
case 'unblock':
result = await (0, utils_1.processUnblockMember)(api, {
groupId: this.getNodeParameter('groupId', i),
memberIds: this.getNodeParameter('memberIds', i),
});
break;
case 'getPending':
result = await (0, utils_1.processGetPendingRequests)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
case 'review':
result = await (0, utils_1.processReviewPendingRequest)(api, {
groupId: this.getNodeParameter('groupId', i),
memberIds: this.getNodeParameter('memberIds', i),
action: 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 === 'content') {
switch (operation) {
case 'createNote':
result = await (0, utils_1.processCreateNote)(api, {
groupId: this.getNodeParameter('groupId', i),
content: this.getNodeParameter('content', i),
pinAct: this.getNodeParameter('pinAct', i),
});
break;
case 'getChatHistory': {
const rawTimestamp = this.getNodeParameter('timestamp', i, '');
result = await (0, utils_1.processGetGroupChatHistory)(api, {
groupId: this.getNodeParameter('groupId', i),
limit: this.getNodeParameter('limit', i),
timestamp: rawTimestamp === '' || rawTimestamp === null
? undefined
: Number(rawTimestamp),
});
break;
}
case 'editNote':
result = await (0, utils_1.processEditNote)(api, {
groupId: this.getNodeParameter('groupId', i),
topicId: this.getNodeParameter('topicId', i),
title: this.getNodeParameter('content', i),
pinAct: this.getNodeParameter('pinAct', i, false),
});
break;
case 'getListBoard':
result = await (0, utils_1.processGetListBoard)(api, {
groupId: this.getNodeParameter('groupId', i),
page: this.getNodeParameter('page', i, 1),
count: this.getNodeParameter('count', i, 20),
});
break;
case 'getFriendBoardList':
result = await (0, utils_1.processGetFriendBoardList)(api, {
conversationId: this.getNodeParameter('conversationId', i),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'link') {
switch (operation) {
case 'getInfo':
result = await (0, utils_1.processGetGroupLinkInfo)(api, {
link: this.getNodeParameter('groupLink', i),
memberPage: this.getNodeParameter('memberPage', i),
});
break;
case 'getDetail':
result = await (0, utils_1.processGetGroupLinkDetail)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
case 'enable':
result = await (0, utils_1.processEnableGroupLink)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
case 'disable':
result = await (0, utils_1.processDisableGroupLink)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation} for resource: ${resource}`, { itemIndex: i });
}
}
else if (resource === 'inviteBox') {
switch (operation) {
case 'getInviteInfo':
result = await (0, utils_1.processGetGroupInviteBoxInfo)(api, {
groupId: this.getNodeParameter('groupId', i),
memberPage: this.getNodeParameter('memberPage', i, 1),
memberCount: this.getNodeParameter('memberCount', i, 20),
});
break;
case 'getInviteList':
result = await (0, utils_1.processGetGroupInviteBoxList)(api, {
page: this.getNodeParameter('page', i, 1),
invitesPerPage: this.getNodeParameter('invitesPerPage', i, 20),
});
break;
case 'joinFromInvite':
result = await (0, utils_1.processJoinGroupInviteBox)(api, {
groupId: this.getNodeParameter('groupId', i),
});
break;
case 'deleteInvite':
result = await (0, utils_1.processDeleteGroupInviteBox)(api, {
groupIds: this.getNodeParameter('groupIds', i),
blockFutureInvite: this.getNodeParameter('blockFutureInvite', i, false),
});
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.ZaloGroup = ZaloGroup;
//# sourceMappingURL=ZaloGroup.node.js.map