n8n-nodes-wuzapi
Version:
n8n community nodes for Wuzapi - WhatsApp Multi-Device REST API
247 lines • 11.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WuzapiUser = void 0;
const GenericFunctions_1 = require("../GenericFunctions");
class WuzapiUser {
constructor() {
this.description = {
displayName: 'Wuzapi User',
name: 'wuzapiUser',
icon: 'file:wuzapi.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Get user information and manage presence with Wuzapi WhatsApp API',
defaults: {
name: 'Wuzapi User',
},
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
credentials: [
{
name: 'wuzapiApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Check Users',
value: 'check',
description: 'Check if users have WhatsApp',
action: 'Check users',
},
{
name: 'Get Avatar',
value: 'avatar',
description: 'Get user profile picture',
action: 'Get avatar',
},
{
name: 'Get Contacts',
value: 'contacts',
description: 'Get all contacts',
action: 'Get contacts',
},
{
name: 'Get User Info',
value: 'info',
description: 'Get detailed user information',
action: 'Get user info',
},
{
name: 'Set Presence',
value: 'presence',
description: 'Set global user presence',
action: 'Set presence',
},
],
default: 'check',
},
// Check Users & Get User Info
{
displayName: 'Phone Numbers',
name: 'phoneNumbers',
type: 'string',
default: '',
placeholder: 'e.g. 5491155553934,5491155553935',
description: 'Comma-separated list of phone numbers to check',
required: true,
displayOptions: {
show: {
operation: ['check', 'info'],
},
},
},
// Get Avatar
{
displayName: 'Phone Number',
name: 'avatarPhone',
type: 'string',
default: '',
placeholder: 'e.g. 5491155553934',
description: 'Phone number to get avatar for',
required: true,
displayOptions: {
show: {
operation: ['avatar'],
},
},
},
{
displayName: 'Preview',
name: 'preview',
type: 'boolean',
default: true,
description: 'Whether to get thumbnail (true) or full picture (false)',
displayOptions: {
show: {
operation: ['avatar'],
},
},
},
{
displayName: 'Save as Binary',
name: 'saveAsBinary',
type: 'boolean',
default: false,
description: 'Whether to save the avatar as binary data',
displayOptions: {
show: {
operation: ['avatar'],
},
},
},
{
displayName: 'Binary Property Name',
name: 'binaryPropertyName',
type: 'string',
default: 'avatar',
description: 'Name of the binary property to store the avatar',
displayOptions: {
show: {
operation: ['avatar'],
saveAsBinary: [true],
},
},
},
// Set Presence
{
displayName: 'Presence Type',
name: 'presenceType',
type: 'options',
default: 'available',
options: [
{
name: 'Available',
value: 'available',
description: 'Show as online',
},
{
name: 'Unavailable',
value: 'unavailable',
description: 'Show as offline',
},
],
description: 'Global presence status to set',
displayOptions: {
show: {
operation: ['presence'],
},
},
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const operation = this.getNodeParameter('operation', i);
let responseData;
if (operation === 'check') {
const phoneNumbers = this.getNodeParameter('phoneNumbers', i);
const phones = phoneNumbers.split(',').map(phone => (0, GenericFunctions_1.validatePhoneNumber)(phone.trim()));
const body = {
Phone: phones,
};
responseData = await GenericFunctions_1.wuzapiApiRequest.call(this, 'POST', '/user/check', body);
}
else if (operation === 'info') {
const phoneNumbers = this.getNodeParameter('phoneNumbers', i);
const phones = phoneNumbers.split(',').map(phone => (0, GenericFunctions_1.validatePhoneNumber)(phone.trim()));
const body = {
Phone: phones,
};
responseData = await GenericFunctions_1.wuzapiApiRequest.call(this, 'POST', '/user/info', body);
}
else if (operation === 'avatar') {
const phone = this.getNodeParameter('avatarPhone', i);
const preview = this.getNodeParameter('preview', i);
const saveAsBinary = this.getNodeParameter('saveAsBinary', i);
const body = {
Phone: (0, GenericFunctions_1.validatePhoneNumber)(phone),
Preview: preview,
};
responseData = await GenericFunctions_1.wuzapiApiRequest.call(this, 'POST', '/user/avatar', body);
// Handle binary data if requested
if (saveAsBinary && responseData.data) {
const avatarUrl = responseData.data.URL || responseData.data;
if (avatarUrl && typeof avatarUrl === 'string') {
// If it's a URL, we need to fetch it
if (avatarUrl.startsWith('http')) {
// Store the URL for user to fetch separately
responseData.data.Note = 'Use HTTP Request node to download the image from the URL';
}
else if (avatarUrl.startsWith('data:')) {
// It's a data URL, extract the base64
const matches = avatarUrl.match(/^data:([^;]+);base64,(.+)$/);
if (matches && matches[2]) {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const mimeType = matches[1] || 'image/jpeg';
const base64Data = matches[2];
items[i].binary = items[i].binary || {};
items[i].binary[binaryPropertyName] = {
data: base64Data,
mimeType,
fileExtension: mimeType.split('/')[1] || 'jpg',
fileName: `avatar-${phone}.${mimeType.split('/')[1] || 'jpg'}`,
};
}
}
}
}
}
else if (operation === 'contacts') {
responseData = await GenericFunctions_1.wuzapiApiRequest.call(this, 'GET', '/user/contacts');
}
else if (operation === 'presence') {
const presenceType = this.getNodeParameter('presenceType', i);
const body = {
type: presenceType,
};
responseData = await GenericFunctions_1.wuzapiApiRequest.call(this, 'POST', '/user/presence', body);
}
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } });
returnData.push(...executionErrorData);
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.WuzapiUser = WuzapiUser;
//# sourceMappingURL=WuzapiUser.node.js.map