n8n-nodes-zalo-tools
Version:
Các node hỗ trợ Zalo cho n8n
138 lines • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZaloMessageTrigger = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const zca_js_1 = require("zca-js");
let api;
let reconnectTimer;
class ZaloMessageTrigger {
constructor() {
this.description = {
displayName: 'Zalo Message Trigger',
name: 'zaloMessageTrigger',
icon: 'file:../shared/zalo.svg',
group: ['trigger'],
version: 1,
description: 'Sự kiện lắng nghe tin nhắn trên Zalo',
defaults: {
name: 'Zalo Message Trigger',
},
inputs: [],
outputs: ['main'],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
credentials: [
{
name: 'zaloApi',
required: true,
displayName: 'Zalo Credential to connect with',
},
],
properties: [
{
displayName: 'Event Types',
name: 'eventTypes',
type: 'multiOptions',
options: [
{
name: 'User Messages',
value: zca_js_1.ThreadType.User,
description: 'Lắng nghe tin nhắn từ người dùng',
},
{
name: 'Group Messages',
value: zca_js_1.ThreadType.Group,
description: 'Lắng nghe tin nhắn từ nhóm',
},
],
default: [zca_js_1.ThreadType.User, zca_js_1.ThreadType.Group],
required: true,
description: 'Types of messages to listen for',
},
],
};
this.webhookMethods = {
default: {
async checkExists() {
const webhookData = this.getWorkflowStaticData('node');
return !!webhookData.isConnected;
},
async create() {
const credentials = await this.getCredentials('zaloApi');
if (!credentials) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No credentials found');
}
try {
const cookieFromCred = JSON.parse(credentials.cookie);
const imeiFromCred = credentials.imei;
const userAgentFromCred = credentials.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.');
}
const webhookUrl = this.getNodeWebhookUrl('default');
console.log(webhookUrl);
api.listener.on('message', async (message) => {
const webhookData = this.getWorkflowStaticData('node');
this.helpers.httpRequest({
method: 'POST',
url: webhookUrl,
body: {
message: message,
},
headers: {
'Content-Type': 'application/json',
},
});
webhookData.lastMessage = message;
});
api.listener.start();
const webhookData = this.getWorkflowStaticData('node');
webhookData.isConnected = true;
webhookData.eventTypes = this.getNodeParameter('eventTypes', 0);
return true;
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Zalo connection failed');
}
},
async delete() {
const webhookData = this.getWorkflowStaticData('node');
if (api) {
api.listener.stop();
api = undefined;
}
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = undefined;
}
delete webhookData.isConnected;
delete webhookData.eventTypes;
delete webhookData.lastMessage;
return true;
},
},
};
}
async webhook() {
const req = this.getRequestObject();
const body = req.body;
console.log(body);
const webhookData = this.getWorkflowStaticData('node');
const message = webhookData.lastMessage;
console.log(message);
delete webhookData.lastMessage;
return {
workflowData: [this.helpers.returnJsonArray(req.body)],
};
}
}
exports.ZaloMessageTrigger = ZaloMessageTrigger;
//# sourceMappingURL=ZaloMessageTrigger.node.js.map