n8n-nodes-quepasa
Version:
This is an n8n community node. It lets you use Quepasa in your n8n workflows. (use with Quepasa >= v4)
131 lines • 6.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resourceMessage = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("../GenericFunctions");
async function resourceMessage(operation, items, i) {
var _a, _b, _c, _d, _e, _f, _g, _h;
let fullResponse;
if (operation === 'download') {
const qs = {};
const messageid = this.getNodeParameter('messageid', i);
qs.messageid = messageid;
fullResponse = await GenericFunctions_1.apiRequest.call(this, 'GET', '/download', {}, qs);
const itemsData = {};
if (items[i].binary !== undefined) {
Object.assign(itemsData, items[i].binary);
}
let fileName = this.getNodeParameter('fileName', i);
if (!fileName) {
fileName = (0, GenericFunctions_1.getFileNameFromHeaderContent)(fullResponse.headers);
}
const buffer = fullResponse.body;
const binaryData = await this.helpers.prepareBinaryData(buffer, fileName || "unknownFileName");
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const responseItem = {
json: items[i].json,
binary: itemsData,
};
responseItem.binary[binaryPropertyName] = binaryData;
items[i] = responseItem;
return;
}
else if (operation === 'send') {
const headers = {};
const chatid = this.getNodeParameter('chatid', i);
if (!chatid || chatid.trim().length === 0)
throw new Error('missing chat id');
headers['X-QUEPASA-CHATID'] = chatid.trim();
const trackid = this.getNodeParameter('trackid', i, '');
if (trackid && trackid.trim().length > 0) {
headers['X-QUEPASA-TRACKID'] = trackid;
}
const body = {
id: this.getNodeParameter('messageid', i, ''),
text: this.getNodeParameter('text', i, ''),
inreply: this.getNodeParameter('inreply', i, ''),
filename: this.getNodeParameter('filename', i, ''),
filelength: this.getNodeParameter('filelength', i, 0),
mime: this.getNodeParameter('mime', i, ''),
url: this.getNodeParameter('url', i, ''),
content: this.getNodeParameter('content', i, ''),
};
if (((_a = body.id) === null || _a === void 0 ? void 0 : _a.trim().length) === 0)
delete body.id;
if (((_b = body.text) === null || _b === void 0 ? void 0 : _b.trim().length) === 0)
delete body.text;
if (((_c = body.inreply) === null || _c === void 0 ? void 0 : _c.trim().length) === 0)
delete body.inreply;
if (((_d = body.filename) === null || _d === void 0 ? void 0 : _d.trim().length) === 0)
delete body.filename;
if (((_e = body.filelength) !== null && _e !== void 0 ? _e : 0) <= 0)
delete body.filelength;
if (((_f = body.mime) === null || _f === void 0 ? void 0 : _f.trim().length) === 0)
delete body.mime;
if (((_g = body.url) === null || _g === void 0 ? void 0 : _g.trim().length) === 0)
delete body.url;
if (((_h = body.content) === null || _h === void 0 ? void 0 : _h.trim().length) === 0)
delete body.content;
const method = this.getNodeParameter('method', i);
if (method === 'sendany' || method === 'sendtext' || method === 'sendurl' || method === 'sendencoded') {
fullResponse = await GenericFunctions_1.apiRequest.call(this, 'POST', '/send', body, {}, undefined, headers);
}
else if (method === 'sendbinary') {
if (items[i].binary === undefined)
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'missing binary data');
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i, 'data');
const binary = items[i].binary[binaryPropertyName];
if (!body.mime && binary.mimeType)
body.mime = binary.mimeType;
if (!body.filename && binary.fileName) {
body.filename = binary.fileName;
{
let fileExtension = binary.fileExtension;
if (!fileExtension && body.mime) {
const mime = require('mime-types');
fileExtension = mime.extension(body.mime);
}
if (fileExtension)
body.filename += `.${fileExtension}`;
}
}
if (body.filename) {
headers['Content-Disposition'] = `attachment;filename=${body.filename}`;
delete body.filename;
}
if (body.mime) {
headers['Content-Type'] = body.mime;
delete body.mime;
}
const buffer = Buffer.from(binary.data, 'base64');
fullResponse = await GenericFunctions_1.apiRequest.call(this, 'POST', '/sendbinary', buffer, body, undefined, headers);
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Send Method not implemented: ' + method);
}
}
else if (operation === 'revoke') {
const qs = {};
const messageid = this.getNodeParameter('messageid', i);
qs.messageid = messageid;
fullResponse = await GenericFunctions_1.apiRequest.call(this, 'DELETE', '/message', {}, qs)
.then((response) => {
return response;
}).catch((error) => {
var _a;
return (_a = error.response) !== null && _a !== void 0 ? _a : error;
});
}
else if (operation === 'where') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Operation not implemented: ' + operation);
}
else if (operation === 'find') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Operation not implemented: ' + operation);
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Operation not implemented: ' + operation);
}
return fullResponse === null || fullResponse === void 0 ? void 0 : fullResponse.body;
}
exports.resourceMessage = resourceMessage;
//# sourceMappingURL=MessageMethods.js.map