n8n-nodes-evolution-tools
Version:
n8n node para integrar com Evolution API WhatsApp como ferramenta para agentes com IA
146 lines • 6.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvolutionApiAiTool = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const axios_1 = __importDefault(require("axios"));
const EvolutionApiToolDescription_1 = require("./EvolutionApiToolDescription");
class EvolutionApiAiTool {
constructor() {
this.description = {
displayName: 'Evolution API AI Tool',
name: 'evolutionApiAiTool',
icon: 'file:evolutionApi.svg',
group: ['transform', 'ai', 'aiTool'],
version: 1,
description: 'Use Evolution API to send WhatsApp messages in AI agents',
subtitle: 'WhatsApp messaging via Evolution API',
defaults: {
name: 'Evolution API AI Tool',
},
credentials: [
{
name: 'evolutionApiApi',
required: true,
},
],
inputs: ['main'],
outputs: ['main'],
properties: [
...EvolutionApiToolDescription_1.evolutionApiToolDescription,
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('evolutionApiApi');
const baseUrl = credentials.url;
const apiKey = credentials.apiKey;
// Get operation selected by user
const operation = this.getNodeParameter('operation', 0);
let responseData;
for (let i = 0; i < items.length; i++) {
try {
// Define parameters based on the operation
if (operation === 'sendMessage') {
const apiUrl = `${baseUrl}/message/sendText/${this.getNodeParameter('instance', i)}`;
const number = this.getNodeParameter('phoneNumber', i);
const message = this.getNodeParameter('message', i);
// Make API call
const response = await (0, axios_1.default)({
method: 'POST',
url: apiUrl,
headers: {
'Content-Type': 'application/json',
'apikey': apiKey,
},
data: {
number,
options: {
delay: 1200,
},
textMessage: {
text: message,
},
},
});
responseData = {
success: true,
result: response.data,
message: `Message sent successfully to ${number}`,
};
}
else if (operation === 'sendMedia') {
const apiUrl = `${baseUrl}/message/sendMedia/${this.getNodeParameter('instance', i)}`;
const number = this.getNodeParameter('phoneNumber', i);
const mediaUrl = this.getNodeParameter('mediaUrl', i);
const caption = this.getNodeParameter('caption', i);
const mediaType = this.getNodeParameter('mediaType', i);
// Make API call
const response = await (0, axios_1.default)({
method: 'POST',
url: apiUrl,
headers: {
'Content-Type': 'application/json',
'apikey': apiKey,
},
data: {
number,
options: {
delay: 1200,
},
mediaMessage: {
mediatype: mediaType,
media: mediaUrl,
caption,
},
},
});
responseData = {
success: true,
result: response.data,
message: `Media message sent successfully to ${number}`,
};
}
else if (operation === 'getQrCode') {
const apiUrl = `${baseUrl}/instance/qrcode/${this.getNodeParameter('instance', i)}`;
// Make API call
const response = await (0, axios_1.default)({
method: 'GET',
url: apiUrl,
headers: {
'Content-Type': 'application/json',
'apikey': apiKey,
},
});
responseData = {
success: true,
result: response.data,
message: 'QR code retrieved successfully',
};
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The operation "${operation}" is not supported!`);
}
// Return the data
const executionData = this.helpers.returnJsonArray(responseData);
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
// Return error
const executionData = this.helpers.returnJsonArray({ success: false, error: error.message });
returnData.push(...executionData);
continue;
}
throw error;
}
}
return this.prepareOutputData(returnData);
}
}
exports.EvolutionApiAiTool = EvolutionApiAiTool;
//# sourceMappingURL=EvolutionApiAiTool.node.js.map