2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2021-01-02 11:16:18 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { phantombusterApiRequest, validateJSON } from './GenericFunctions';
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { agentFields, agentOperations } from './AgentDescription';
|
2021-01-02 11:16:18 -08:00
|
|
|
|
|
|
|
// import {
|
|
|
|
// sentenceCase,
|
|
|
|
// } from 'change-case';
|
|
|
|
|
|
|
|
export class Phantombuster implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Phantombuster',
|
|
|
|
name: 'phantombuster',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2021-01-02 11:16:18 -08:00
|
|
|
icon: 'file:phantombuster.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Phantombuster API',
|
2021-01-02 11:16:18 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Phantombuster',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'phantombusterApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-01-02 11:16:18 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Agent',
|
|
|
|
value: 'agent',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'agent',
|
|
|
|
},
|
|
|
|
...agentOperations,
|
|
|
|
...agentFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getAgents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const responseData = await phantombusterApiRequest.call(this, 'GET', '/agents/fetch-all');
|
2021-01-02 11:16:18 -08:00
|
|
|
|
|
|
|
for (const item of responseData) {
|
|
|
|
returnData.push({
|
|
|
|
name: item.name,
|
|
|
|
value: item.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get all the arguments to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
// async getArguments(
|
|
|
|
// this: ILoadOptionsFunctions,
|
|
|
|
// ): Promise<INodePropertyOptions[]> {
|
|
|
|
// const returnData: INodePropertyOptions[] = [];
|
|
|
|
// const agentId = this.getCurrentNodeParameter('agentId') as string;
|
|
|
|
|
|
|
|
// const { argument } = await phantombusterApiRequest.call(
|
|
|
|
// this,
|
|
|
|
// 'GET',
|
|
|
|
// '/agents/fetch',
|
|
|
|
// {},
|
|
|
|
// { id: agentId },
|
|
|
|
// );
|
|
|
|
|
|
|
|
// for (const key of Object.keys(JSON.parse(argument))) {
|
|
|
|
// returnData.push({
|
|
|
|
// name: sentenceCase(key),
|
|
|
|
// value: key,
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// return returnData;
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2023-01-31 11:39:20 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-01-02 11:16:18 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-01-02 11:16:18 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'agent') {
|
|
|
|
//https://hub.phantombuster.com/reference#post_agents-delete-1
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const agentId = this.getNodeParameter('agentId', i) as string;
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await phantombusterApiRequest.call(this, 'POST', '/agents/delete', {
|
|
|
|
id: agentId,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://hub.phantombuster.com/reference#get_agents-fetch-1
|
|
|
|
if (operation === 'get') {
|
|
|
|
const agentId = this.getNodeParameter('agentId', i) as string;
|
|
|
|
|
|
|
|
responseData = await phantombusterApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/agents/fetch',
|
|
|
|
{},
|
|
|
|
{ id: agentId },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//https://hub.phantombuster.com/reference#get_agents-fetch-output-1
|
|
|
|
if (operation === 'getOutput') {
|
|
|
|
const agentId = this.getNodeParameter('agentId', i) as string;
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const resolveData = this.getNodeParameter('resolveData', i);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.id = agentId;
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await phantombusterApiRequest.call(
|
2021-01-02 11:16:18 -08:00
|
|
|
this,
|
|
|
|
'GET',
|
2021-07-19 23:58:54 -07:00
|
|
|
'/agents/fetch-output',
|
2021-01-02 11:16:18 -08:00
|
|
|
{},
|
2021-07-19 23:58:54 -07:00
|
|
|
qs,
|
2021-01-02 11:16:18 -08:00
|
|
|
);
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (resolveData) {
|
2021-07-19 23:58:54 -07:00
|
|
|
const { resultObject } = await phantombusterApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/containers/fetch-result-object',
|
|
|
|
{},
|
|
|
|
{ id: responseData.containerId },
|
|
|
|
);
|
|
|
|
|
|
|
|
if (resultObject === null) {
|
|
|
|
responseData = {};
|
|
|
|
} else {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = JSON.parse(resultObject as string);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-01-02 11:16:18 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://api.phantombuster.com/api/v2/agents/fetch-all
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await phantombusterApiRequest.call(this, 'GET', '/agents/fetch-all');
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2021-01-02 11:16:18 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://hub.phantombuster.com/reference#post_agents-launch-1
|
|
|
|
if (operation === 'launch') {
|
|
|
|
const agentId = this.getNodeParameter('agentId', i) as string;
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const resolveData = this.getNodeParameter('resolveData', i);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
id: agentId,
|
|
|
|
};
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
|
|
|
if (additionalFields.argumentsJson) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.arguments = validateJSON(
|
|
|
|
this,
|
|
|
|
additionalFields.argumentsJson as string,
|
|
|
|
'Arguments',
|
|
|
|
);
|
2021-04-16 09:33:36 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
delete additionalFields.argumentsJson;
|
|
|
|
}
|
|
|
|
if (additionalFields.bonusArgumentJson) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.bonusArgument = validateJSON(
|
|
|
|
this,
|
|
|
|
additionalFields.bonusArgumentJson as string,
|
|
|
|
'Bonus Argument',
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
delete additionalFields.bonusArgumentJson;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const argumentParameters =
|
2022-12-02 12:54:28 -08:00
|
|
|
((additionalFields.argumentsUi as IDataObject)?.argumentValues as IDataObject[]) ||
|
|
|
|
[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.arguments = argumentParameters.reduce((object, currentValue) => {
|
|
|
|
object[currentValue.key as string] = currentValue.value;
|
|
|
|
return object;
|
|
|
|
}, {});
|
|
|
|
delete additionalFields.argumentsUi;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const bonusParameters =
|
2022-12-02 12:54:28 -08:00
|
|
|
((additionalFields.bonusArgumentUi as IDataObject)
|
|
|
|
?.bonusArgumentValue as IDataObject[]) || [];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.bonusArgument = bonusParameters.reduce((object, currentValue) => {
|
|
|
|
object[currentValue.key as string] = currentValue.value;
|
|
|
|
return object;
|
|
|
|
}, {});
|
|
|
|
delete additionalFields.bonusArgumentUi;
|
2021-01-02 11:16:18 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2021-01-02 11:16:18 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await phantombusterApiRequest.call(this, 'POST', '/agents/launch', body);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (resolveData) {
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await phantombusterApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/containers/fetch',
|
|
|
|
{},
|
|
|
|
{ id: responseData.containerId },
|
|
|
|
);
|
|
|
|
}
|
2021-01-02 11:16:18 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2023-01-31 11:39:20 -08:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-01-02 11:16:18 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-01-02 11:16:18 -08:00
|
|
|
}
|
|
|
|
}
|