n8n-nodes-zalo-tools
Version:
Các node hỗ trợ Zalo cho n8n
198 lines • 10.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaloPoll = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ZaloPollDescription_1 = require("./ZaloPollDescription");
const zca_js_1 = require("zca-js");
const helper_1 = require("../utils/helper");
class ZaloPoll {
constructor() {
this.description = {
displayName: 'Zalo Poll',
name: 'zaloPoll',
icon: 'file:../shared/zalo.svg',
group: ['Zalo'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Quản bình chọn Zalo',
defaults: {
name: 'Zalo Poll',
},
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: 'Poll',
value: 'zaloPoll',
},
],
default: 'zaloPoll',
},
...ZaloPollDescription_1.zaloPollOperations,
...ZaloPollDescription_1.zaloPollFields,
],
};
}
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({ 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 === 'zaloPoll') {
if (operation === 'createPoll') {
const groupId = this.getNodeParameter('groupId', i);
const question = this.getNodeParameter('question', i);
const optionInputType = this.getNodeParameter('optionInputType', i, 'list');
let options = [];
if (optionInputType === 'list') {
try {
const pollOptionsCollection = this.getNodeParameter('pollOptionsCollection', i, {
options: [],
});
if ((pollOptionsCollection === null || pollOptionsCollection === void 0 ? void 0 : pollOptionsCollection.options) &&
Array.isArray(pollOptionsCollection.options)) {
options = pollOptionsCollection.options
.map((item) => ((item === null || item === void 0 ? void 0 : item.option) || '').trim())
.filter((option) => option !== '');
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Lỗi xử lý các lựa chọn: ' + (error.message || 'Lỗi không xác định'));
}
}
else if (optionInputType === 'text') {
const optionsString = this.getNodeParameter('optionsString', i, '');
if (optionsString && optionsString.trim() !== '') {
options = optionsString
.split(',')
.map((option) => option.trim())
.filter((option) => option !== '');
}
}
if (!options || options.length === 0) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Vui lòng nhập ít nhất một lựa chọn cho bình chọn', { itemIndex: i });
}
const expiredTime = this.getNodeParameter('expiredTime', i, null) !== null
? new Date(this.getNodeParameter('expiredTime', i)).getTime() || 0
: 0;
const pinAct = this.getNodeParameter('pinAct', i, false);
const allowMultiChoices = this.getNodeParameter('allowMultiChoices', i, true);
const allowAddNewOption = this.getNodeParameter('allowAddNewOption', i, true);
const hideVotePreview = this.getNodeParameter('hideVotePreview', i, false);
const isAnonymous = this.getNodeParameter('isAnonymous', i, false);
const createPollData = {
question: question,
options: options,
expiredTime: expiredTime,
pinAct: pinAct,
allowMultiChoices: allowMultiChoices,
allowAddNewOption: allowAddNewOption,
hideVotePreview: hideVotePreview,
isAnonymous: isAnonymous,
};
this.logger.info(`Create poll with parameters: ${JSON.stringify(createPollData)}`);
if (!api) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Zalo API not initialized', {
itemIndex: i,
});
}
const response = await api.createPoll(createPollData, groupId);
this.logger.info('Create poll successfully', { groupId, question });
returnData.push({
json: {
success: true,
response,
groupId,
createPollData,
},
});
}
else if (operation === 'getPoll') {
const poll_id = Number(this.getNodeParameter('poll_id', i));
this.logger.info(`Get poll with parameters: ${JSON.stringify(poll_id)}`);
if (!api) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Zalo API not initialized', {
itemIndex: i,
});
}
const response = await api.getPollDetail(poll_id);
this.logger.info('Get poll successfully', { response });
returnData.push({
json: {
success: true,
response,
poll_id,
},
});
}
else if (operation === 'lockPoll') {
const poll_id = Number(this.getNodeParameter('poll_id', i));
this.logger.info(`Lock poll with parameters: ${JSON.stringify(poll_id)}`);
if (!api) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Zalo API not initialized', {
itemIndex: i,
});
}
const response = await api.lockPoll(poll_id);
this.logger.info('Lock poll successfully', { response });
returnData.push({
json: {
success: true,
response,
poll_id,
},
});
}
}
}
catch (error) {
this.logger.info(`Error create poll: ${JSON.stringify(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.ZaloPoll = ZaloPoll;
//# sourceMappingURL=ZaloPoll.node.js.map