n8n-nodes-wuzapi
Version:
n8n community nodes for Wuzapi - WhatsApp Multi-Device REST API
130 lines • 6.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WuzapiSendAndWait = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("../GenericFunctions");
const configureWaitTillDate_util_1 = require("../utils/sendAndWait/configureWaitTillDate.util");
const descriptions_1 = require("../utils/sendAndWait/descriptions");
const utils_1 = require("../utils/sendAndWait/utils");
const WUZAPI_CREDENTIALS_TYPE = 'wuzapiApi';
class WuzapiSendAndWait {
constructor() {
this.description = {
displayName: 'Wuzapi Send and Wait',
name: 'wuzapiSendAndWait',
icon: 'file:wuzapi.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Send a WhatsApp message and wait for approval',
defaults: {
name: 'Wuzapi Send and Wait',
},
usableAsTool: true,
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
webhooks: descriptions_1.sendAndWaitWebhooksDescription,
credentials: [
{
name: WUZAPI_CREDENTIALS_TYPE,
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Send and Wait',
value: 'sendAndWait',
},
],
default: 'sendAndWait',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['sendAndWait'],
},
},
options: [
{
name: 'Send and Wait for Approval',
value: 'sendAndWaitForApproval',
description: 'Send a message and wait for response',
action: 'Send message and wait for approval',
},
],
default: 'sendAndWaitForApproval',
},
...(0, utils_1.getSendAndWaitProperties)([], 'sendAndWait', [], {
noButtonStyle: false,
defaultApproveLabel: '✓ Approve',
defaultDisapproveLabel: '✗ Decline',
}),
],
};
this.webhook = utils_1.sendAndWaitWebhook;
this.customOperations = {
sendAndWait: {
async [n8n_workflow_1.SEND_AND_WAIT_OPERATION]() {
try {
const phone = this.getNodeParameter('phone', 0);
const cleanPhone = (0, GenericFunctions_1.validatePhoneNumber)(phone);
const instanceId = this.getInstanceId();
const responseType = this.getNodeParameter('responseType', 0, 'approval');
// Create the message based on response type
if (responseType === 'approval') {
// For approval messages, we use template messages with buttons
const config = (0, utils_1.getSendAndWaitConfig)(this);
const body = {
Phone: cleanPhone,
Content: config.message,
Footer: config.appendAttribution !== false
? 'This message was sent automatically with n8n'
: undefined,
Buttons: config.options.map(option => ({
DisplayText: option.label,
Type: 'quickreply',
})),
};
// Send template message
await GenericFunctions_1.wuzapiApiRequest.call(this, 'POST', '/chat/send/template', body);
}
else {
// For freeText/customForm, send regular message with link
const config = (0, utils_1.getSendAndWaitConfig)(this);
const resumeUrl = config.url;
const messageText = config.appendAttribution !== false
? `${config.message}\n\nClick here to respond: ${resumeUrl}\n\nThis message was sent automatically with n8n`
: `${config.message}\n\nClick here to respond: ${resumeUrl}`;
const body = {
Phone: cleanPhone,
Body: messageText,
};
// Send text message
await GenericFunctions_1.wuzapiApiRequest.call(this, 'POST', '/chat/send/text', body);
}
// Configure wait time
const waitTill = (0, configureWaitTillDate_util_1.configureWaitTillDate)(this);
// Put execution to wait
await this.putExecutionToWait(waitTill);
return [this.getInputData()];
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error);
}
},
},
};
}
}
exports.WuzapiSendAndWait = WuzapiSendAndWait;
//# sourceMappingURL=WuzapiSendAndWait.node.js.map