UNPKG

n8n-nodes-zalo-tools

Version:
309 lines 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZaloSendMessage = void 0; const n8n_workflow_1 = require("n8n-workflow"); const zca_js_1 = require("zca-js"); const helper_1 = require("../utils/helper"); let api; class ZaloSendMessage { constructor() { this.description = { displayName: 'Zalo Send Message', name: 'zaloSendMessage', icon: 'file:../shared/zalo.svg', group: ['Zalo'], version: 4, description: 'Gửi tin nhắn qua API Zalo sử dụng kết nối đăng nhập bằng cookie', defaults: { name: 'Zalo Send Message', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'zaloApi', required: true, }, ], properties: [ { displayName: 'Thread ID', name: 'threadId', type: 'string', default: '', required: true, description: 'ID của thread để gửi tin nhắn', }, { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'User', value: 0, }, { name: 'Group', value: 1, }, ], default: 0, description: 'Loại của tin nhắn (user hoặc group)', }, { displayName: 'Message', name: 'message', type: 'string', default: '', required: true, description: 'Nội dung tin nhắn cần gửi', }, { displayName: 'Urgency', name: 'urgency', type: 'options', options: [ { name: 'Default', value: 0, }, { name: 'Important', value: 1, }, { name: 'Urgent', value: 2, }, ], default: 0, description: 'Mức độ khẩn cấp của tin nhắn', }, { displayName: 'Quote Message', name: 'quote', type: 'collection', placeholder: 'Add Quote', default: {}, options: [ { displayName: 'Message ID', name: 'msgId', type: 'string', default: '', description: 'ID của tin nhắn cần trích dẫn', }, { displayName: 'Sender ID', name: 'senderId', type: 'string', default: '', description: 'ID của người gửi tin nhắn trích dẫn', }, { displayName: 'Content', name: 'content', type: 'string', default: '', description: 'Nội dung tin nhắn trích dẫn', }, ], }, { displayName: 'Mentions', name: 'mentions', type: 'collection', placeholder: 'Add Mention', default: {}, options: [ { displayName: 'User ID', name: 'uid', type: 'string', default: '', description: 'ID của người dùng được mention', }, { displayName: 'Position', name: 'pos', type: 'number', default: 0, description: 'Vị trí mention trong tin nhắn', }, { displayName: 'Length', name: 'len', type: 'number', default: 0, description: 'Độ dài của mention', }, ], }, { displayName: 'Attachments', name: 'attachments', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Attachment', default: {}, options: [ { name: 'attachment', displayName: 'Attachment', values: [ { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'Image URL', value: 'url', } ], default: 'url', description: 'Loại file đính kèm', }, { displayName: 'Image URL', name: 'imageUrl', type: 'string', default: '', displayOptions: { show: { 'type': ['url'], }, }, description: 'URL công khai của ảnh', } ], }, ], description: 'Một hoặc nhiều ảnh đính kèm để gửi', }, ], }; this.methods = { loadOptions: { async getBinaryProperties() { const returnData = []; const binaryProperties = this.getCurrentNodeParameter('binaryProperties'); if (binaryProperties) { for (const property of binaryProperties) { returnData.push({ name: property, value: property, }); } } return returnData; }, }, }; } async execute() { const returnData = []; const items = this.getInputData(); const zaloCred = await this.getCredentials('zaloApi'); const cookieFromCred = JSON.parse(zaloCred.cookie); const imeiFromCred = zaloCred.imei; const userAgentFromCred = zaloCred.userAgent; try { 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(), 'Failed to initialize Zalo API. Check your credentials.'); } } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Zalo login error: ${error.message}`); } for (let i = 0; i < items.length; i++) { try { const threadId = this.getNodeParameter('threadId', i); const typeNumber = this.getNodeParameter('type', i); const type = typeNumber === 0 ? zca_js_1.ThreadType.User : zca_js_1.ThreadType.Group; const message = this.getNodeParameter('message', i); const urgency = this.getNodeParameter('urgency', i, 0); const quote = this.getNodeParameter('quote', i, {}); const mentions = this.getNodeParameter('mentions', i, {}); const attachments = this.getNodeParameter('attachments', i, {}); const messageContent = { msg: message, }; if (urgency !== 0) { messageContent.urgency = urgency; } if (quote && Object.keys(quote).length > 0) { messageContent.quote = { msgId: quote.msgId, senderId: quote.senderId, content: quote.content, }; } if (mentions && Object.keys(mentions).length > 0) { messageContent.mentions = [{ pos: mentions.pos || 0, uid: mentions.uid, len: mentions.len || 0, }]; } if (attachments && attachments.attachment && attachments.attachment.length > 0) { messageContent.attachments = []; for (const attachment of attachments.attachment) { let fileData; if (attachment.type === 'url') { fileData = await (0, helper_1.saveImage)(attachment.imageUrl); } messageContent.attachments.push(fileData); } } this.logger.info(`Sending message with parameters: ${JSON.stringify(messageContent)}`); if (!api) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Zalo API not initialized'); } const response = await api.sendMessage(messageContent, threadId, type); if (messageContent.attachments && messageContent.attachments.length > 0) { for (const attachment of messageContent.attachments) { this.logger.info(`Remove attachment: ${attachment}`); (0, helper_1.removeImage)(attachment); } } this.logger.info('Message sent successfully', { threadId, type }); returnData.push({ json: { success: true, response, threadId, threadType: type, messageContent, }, }); } catch (error) { this.logger.error('Error sending Zalo message:', error); if (this.continueOnFail()) { returnData.push({ json: { success: false, error: error.message, }, }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i }); } } } return [returnData]; } } exports.ZaloSendMessage = ZaloSendMessage; //# sourceMappingURL=ZaloSendMessage.node.js.map