UNPKG

n8n-nodes-1clickai-wadmin

Version:
160 lines 5.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OneClickAiWadminTrigger = void 0; class OneClickAiWadminTrigger { constructor() { this.description = { displayName: 'WAdmin Trigger', name: 'oneClickAiWadminTrigger', icon: 'file:oneclick.svg', group: ['trigger'], version: 1, description: 'Trigger that fires flows when messages are received from 1ClickAi WAdmin', defaults: { name: 'WAdmin Trigger', }, inputs: [], outputs: [ "main", "main", "main" ], outputNames: [ 'audio', 'text', 'image' ], credentials: [ { name: '1ClickAiWadminApi', required: true } ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: '={{$parameter["path"]}}', isPrivate: true } ], properties: [ { displayName: 'Path', name: 'path', type: 'string', default: 'oneclickai-wadmin', description: 'Webhook path (final part of the URL) - must be unique per workflow' } ] }; } async webhook() { const res = this.getResponseObject(); const credentials = await this.getCredentials('1ClickAiWadminApi'); if (!credentials || !credentials.token) { res.statusCode = 500; res.end(JSON.stringify({ error: true, message: '1ClickAiWadminApi credential not configured' })); return { noWebhookResponse: true }; } const expectedToken = credentials.token; const headers = this.getHeaderData(); const authHeader = headers['authorization'] || headers['Authorization']; if (!authHeader || typeof authHeader !== 'string' || !authHeader.startsWith('Bearer ')) { res.statusCode = 401; res.end(JSON.stringify({ error: true, message: 'Bearer authentication not provided' })); return { noWebhookResponse: true }; } const tokenReceived = authHeader.slice(7).trim(); if (tokenReceived !== expectedToken) { res.statusCode = 403; res.end(JSON.stringify({ error: true, message: 'Invalid authentication token' })); return { noWebhookResponse: true }; } const body = this.getBodyData(); if (!body || typeof body !== 'object') { res.statusCode = 400; res.end(JSON.stringify({ error: true, message: 'Invalid request (JSON not found)' })); return { noWebhookResponse: true }; } if (Object.keys(body).length === 0) { res.statusCode = 200; res.end(JSON.stringify({ error: false, message: 'Test webhook working' })); return { noWebhookResponse: true }; } const { name, user: user, message: message, type: type, group: group } = body; if (user === undefined || message === undefined || type === undefined || group === undefined) { console.log(name, user, message, type, group); res.statusCode = 400; res.end(JSON.stringify({ error: true, message: 'Invalid request: "user", "message", "type" and "group" fields are required.' })); return { noWebhookResponse: true }; } let finalType = ''; if (type === 'audio') { finalType = 'audio'; } else if (type === 'text') { finalType = 'text'; } else if (type === 'image') { finalType = 'image'; } else { res.statusCode = 400; res.end(JSON.stringify({ error: true, message: `Unsupported message type: ${type}` })); return { noWebhookResponse: true }; } const outputMap = { 'audio': 0, 'text': 1, 'image': 2 }; const outputIndex = outputMap[finalType]; if (outputIndex === undefined) { res.statusCode = 400; res.end(JSON.stringify({ error: true, message: `Unsupported message type: ${finalType}` })); return { noWebhookResponse: true }; } const outputData = { ...body, }; const outputs = [[], [], []]; outputs[outputIndex].push({ json: outputData }); res.statusCode = 200; res.end(JSON.stringify({ error: false, message: 'Message processed successfully', finalType })); return { workflowData: outputs }; } } exports.OneClickAiWadminTrigger = OneClickAiWadminTrigger; //# sourceMappingURL=OneClickAiWadminTrigger.node.js.map