n8n-nodes-megaapi
Version:
N8N Community Node for MegaAPI WhatsApp automation - Complete WhatsApp integration with messaging, groups, media, and more
497 lines • 23.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MegaApi = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const properties_1 = require("./properties");
const chat_1 = require("./properties/chat");
const group_1 = require("./properties/group");
const execute_1 = require("./execute");
const chat_2 = require("./execute/chat");
const group_2 = require("./execute/group");
class MegaApi {
constructor() {
this.description = {
displayName: 'MegaAPI',
name: 'megaApi',
icon: 'file:megaapi.svg',
group: ['communication'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Interact with MegaAPI WhatsApp service',
defaults: {
name: 'MegaAPI',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'megaApiCredentialsApi',
required: true,
},
],
requestDefaults: {
baseURL: '={{$credentials.host}}',
headers: {
Authorization: '=Bearer {{$credentials.token}}',
'Content-Type': 'application/json',
},
},
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Chat',
value: 'chat',
description: 'Chat management operations',
},
{
name: 'Group',
value: 'group',
description: 'Group management operations',
},
{
name: 'Instance',
value: 'instance',
description: 'WhatsApp instance operations',
},
{
name: 'Message',
value: 'message',
description: 'Message sending operations',
},
{
name: 'Webhook',
value: 'webhook',
description: 'Webhook configuration operations',
},
],
default: 'group',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['chat'],
},
},
options: [
{
name: 'Delete Message From Me',
value: 'deleteMessageFromMe',
description: 'Delete a specific message sent by you using message key and timestamp',
action: 'Delete message from me',
},
{
name: 'Delete Message',
value: 'deleteMessage',
description: 'Delete a specific message from the chat',
action: 'Delete message',
},
{
name: 'Presence Update Chat',
value: 'presenceUpdateChat',
description: 'Update presence status in a chat (typing, recording, online, etc.)',
action: 'Update presence status',
},
],
default: 'deleteMessageFromMe',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['group'],
},
},
options: [
{
name: 'Add Participants',
value: 'addParticipants',
description: 'Add participants to an existing WhatsApp group',
action: 'Add participants to group',
},
{
name: 'Create Group',
value: 'createGroup',
description: 'Create a new WhatsApp group with specified name and participants',
action: 'Create new group',
},
{
name: 'Details Group',
value: 'detailsGroup',
description: 'Get detailed information about a specific WhatsApp group',
action: 'Get group details',
},
{
name: 'Get Groups',
value: 'getGroups',
description: 'Get a list of all groups that your WhatsApp instance is a member of',
action: 'Get groups list',
},
{
name: 'Leave Group',
value: 'leaveGroup',
description: 'Leave an existing WhatsApp group',
action: 'Leave group',
},
{
name: 'Remove Participants',
value: 'removeParticipants',
description: 'Remove participants from an existing WhatsApp group',
action: 'Remove participants from group',
},
{
name: 'Send Media Base64 Group',
value: 'sendMediaBase64Group',
description: 'Send a media file from base64 data to a specific WhatsApp group',
action: 'Send media base64 to group',
},
{
name: 'Send Media URL Group',
value: 'sendMediaUrlGroup',
description: 'Send a media file from URL to a specific WhatsApp group',
action: 'Send media URL to group',
},
{
name: 'Send Message Group',
value: 'sendMessageGroup',
description: 'Send a text message to a specific WhatsApp group',
action: 'Send message to group',
},
],
default: 'leaveGroup',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['instance'],
},
},
options: [
{
name: 'Download Media Message',
value: 'downloadMediaMessage',
description: 'Download media content from a WhatsApp message',
action: 'Download media message',
},
{
name: 'Generate Pairing Code',
value: 'generatePairingCode',
description: 'Generate a pairing code for WhatsApp connection',
action: 'Generate a pairing code',
},
{
name: 'Generate QR Code',
value: 'generateQrCode',
description: 'Generate a QR code for WhatsApp connection',
action: 'Generate a QR code',
},
{
name: 'Is On WhatsApp',
value: 'isOnWhatsapp',
description: 'Check if a contact is registered on WhatsApp',
action: 'Check if contact is on whatsapp',
},
{
name: 'Logout',
value: 'logout',
description: 'Logout from WhatsApp instance',
action: 'Logout from instance',
},
{
name: 'Restart',
value: 'restart',
description: 'Restart WhatsApp instance',
action: 'Restart instance',
},
{
name: 'Status',
value: 'status',
description: 'Get WhatsApp instance status',
action: 'Get instance status',
},
],
default: 'generatePairingCode',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['message'],
},
},
options: [
{
name: 'Forward Message',
value: 'forwardMessage',
description: 'Forward an existing WhatsApp message to another contact',
action: 'Forward message',
},
{
name: 'Quote Message',
value: 'quoteMessage',
description: 'Send a text message as a reply/quote to an existing WhatsApp message',
action: 'Quote message',
},
{
name: 'Send Link Preview',
value: 'sendLinkPreview',
description: 'Send a message with a link that WhatsApp will preview automatically',
action: 'Send link with preview',
},
{
name: 'Send List Message',
value: 'sendListMessage',
description: 'Send an interactive list message with selectable options to a WhatsApp contact',
action: 'Send interactive list',
},
{
name: 'Send Location',
value: 'sendLocation',
description: 'Send a location message to a WhatsApp contact',
action: 'Send location',
},
{
name: 'Send Media Base64',
value: 'sendMediaBase64',
description: 'Send a media file from base64 data to a WhatsApp contact',
action: 'Send media from base64',
},
{
name: 'Send Media URL',
value: 'sendMediaUrl',
description: 'Send a media file from URL to a WhatsApp contact',
action: 'Send media from URL',
},
{
name: 'Send Text Message',
value: 'sendTextMessage',
description: 'Send a text message to a WhatsApp contact',
action: 'Send text message',
},
],
default: 'quoteMessage',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['webhook'],
},
},
options: [
{
name: 'Config Webhook',
value: 'configWebhook',
description: 'Configure webhook URL and enable/disable notifications',
action: 'Configure webhook settings',
},
{
name: 'Get Webhook',
value: 'getWebhook',
description: 'Get current webhook configuration',
action: 'Get webhook configuration',
},
],
default: 'configWebhook',
},
...chat_1.presenceUpdateChatProperties,
...chat_1.deleteMessageProperties,
...chat_1.deleteMessageFromMeProperties,
...group_1.getGroupsProperties,
...group_1.detailsGroupProperties,
...group_1.createGroupProperties,
...group_1.sendMessageGroupProperties,
...group_1.sendMediaUrlGroupProperties,
...group_1.sendMediaBase64GroupProperties,
...group_1.addParticipantsProperties,
...group_1.removeParticipantsProperties,
...group_1.leaveGroupProperties,
...properties_1.pairingCodeProperties,
...properties_1.qrCodeProperties,
...properties_1.logoutProperties,
...properties_1.restartProperties,
...properties_1.statusProperties,
...properties_1.downloadMediaMessageProperties,
...properties_1.isOnWhatsappProperties,
...properties_1.getWebhookProperties,
...properties_1.configWebhookProperties,
...properties_1.sendTextMessageProperties,
...properties_1.sendMediaUrlProperties,
...properties_1.sendMediaBase64Properties,
...properties_1.sendLocationProperties,
...properties_1.sendLinkPreviewProperties,
...properties_1.sendListMessageProperties,
...properties_1.forwardMessageProperties,
...properties_1.quoteMessageProperties,
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
try {
let responseData;
if (resource === 'chat') {
switch (operation) {
case 'deleteMessageFromMe':
responseData = await chat_2.deleteMessageFromMe.call(this, i);
break;
case 'deleteMessage':
responseData = await chat_2.deleteMessage.call(this, i);
break;
case 'presenceUpdateChat':
responseData = await chat_2.presenceUpdateChat.call(this, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown chat operation: ${operation}`, { itemIndex: i });
}
}
else if (resource === 'group') {
switch (operation) {
case 'addParticipants':
responseData = await group_2.addParticipants.call(this, i);
break;
case 'createGroup':
responseData = await group_2.createGroup.call(this, i);
break;
case 'detailsGroup':
responseData = await group_2.detailsGroup.call(this, i);
break;
case 'getGroups':
responseData = await group_2.getGroups.call(this, i);
break;
case 'leaveGroup':
responseData = await group_2.leaveGroup.call(this, i);
break;
case 'removeParticipants':
responseData = await group_2.removeParticipants.call(this, i);
break;
case 'sendMediaBase64Group':
responseData = await group_2.sendMediaBase64Group.call(this, i);
break;
case 'sendMediaUrlGroup':
responseData = await group_2.sendMediaUrlGroup.call(this, i);
break;
case 'sendMessageGroup':
responseData = await group_2.sendMessageGroup.call(this, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown group operation: ${operation}`, { itemIndex: i });
}
}
else if (resource === 'instance') {
switch (operation) {
case 'generatePairingCode':
responseData = await execute_1.generatePairingCode.call(this, i);
break;
case 'generateQrCode':
responseData = await execute_1.generateQrCode.call(this, i);
break;
case 'logout':
responseData = await execute_1.logoutInstance.call(this, i);
break;
case 'restart':
responseData = await execute_1.restartInstance.call(this, i);
break;
case 'status':
responseData = await execute_1.getInstanceStatus.call(this, i);
break;
case 'downloadMediaMessage':
responseData = await execute_1.downloadMediaMessage.call(this, i);
break;
case 'isOnWhatsapp':
responseData = await execute_1.checkIsOnWhatsapp.call(this, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation}`, { itemIndex: i });
}
}
else if (resource === 'message') {
switch (operation) {
case 'quoteMessage':
responseData = await execute_1.quoteMessage.call(this, i);
break;
case 'forwardMessage':
responseData = await execute_1.forwardMessage.call(this, i);
break;
case 'sendListMessage':
responseData = await execute_1.sendListMessage.call(this, i);
break;
case 'sendLinkPreview':
responseData = await execute_1.sendLinkPreview.call(this, i);
break;
case 'sendLocation':
responseData = await execute_1.sendLocation.call(this, i);
break;
case 'sendMediaBase64':
responseData = await execute_1.sendMediaBase64.call(this, i);
break;
case 'sendMediaUrl':
responseData = await execute_1.sendMediaUrl.call(this, i);
break;
case 'sendTextMessage':
responseData = await execute_1.sendTextMessage.call(this, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown message operation: ${operation}`, { itemIndex: i });
}
}
else if (resource === 'webhook') {
switch (operation) {
case 'configWebhook':
responseData = await execute_1.configureWebhook.call(this, i);
break;
case 'getWebhook':
responseData = await execute_1.getWebhookConfig.call(this, i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown webhook operation: ${operation}`, { itemIndex: i });
}
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown resource: ${resource}`, { itemIndex: i });
}
returnData.push(responseData);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
pairedItem: { item: i },
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.MegaApi = MegaApi;
//# sourceMappingURL=MegaApi.node.js.map