mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
✨ Intercom node
This commit is contained in:
@@ -27,9 +27,6 @@ export async function intercomApiRequest(this: IHookFunctions | IExecuteFunction
|
|||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(options)
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request!(options);
|
return await this.helpers.request!(options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -43,3 +40,13 @@ export async function intercomApiRequest(this: IHookFunctions | IExecuteFunction
|
|||||||
throw error.response.body;
|
throw error.response.body;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result = JSON.parse(json!);
|
||||||
|
} catch (exception) {
|
||||||
|
result = '';
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,7 +15,12 @@ import {
|
|||||||
} from './LeadDescription';
|
} from './LeadDescription';
|
||||||
import {
|
import {
|
||||||
intercomApiRequest,
|
intercomApiRequest,
|
||||||
|
validateJSON,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
import {
|
||||||
|
ILead,
|
||||||
|
ILeadCompany
|
||||||
|
} from './LeadInterface';
|
||||||
|
|
||||||
export class Intercom implements INodeType {
|
export class Intercom implements INodeType {
|
||||||
|
|
||||||
@@ -74,7 +79,7 @@ export class Intercom implements INodeType {
|
|||||||
companies = response.companies;
|
companies = response.companies;
|
||||||
for (const company of companies) {
|
for (const company of companies) {
|
||||||
const companyName = company.name;
|
const companyName = company.name;
|
||||||
const companyId = company.id;
|
const companyId = company.company_id;
|
||||||
returnData.push({
|
returnData.push({
|
||||||
name: companyName,
|
name: companyName,
|
||||||
value: companyId,
|
value: companyId,
|
||||||
@@ -86,9 +91,67 @@ export class Intercom implements INodeType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
|
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
|
||||||
|
const resource = this.getNodeParameter('resource') as string;
|
||||||
|
const opeation = this.getNodeParameter('operation') as string;
|
||||||
|
let response;
|
||||||
|
if (resource === 'lead') {
|
||||||
|
if (opeation === 'create') {
|
||||||
|
const email = this.getNodeParameter('email') as string;
|
||||||
|
const options = this.getNodeParameter('options') as IDataObject;
|
||||||
|
const jsonActive = this.getNodeParameter('jsonParameters') as boolean;
|
||||||
|
const body: ILead = {
|
||||||
|
email,
|
||||||
|
};
|
||||||
|
if (options.phone) {
|
||||||
|
body.phone = options.phone as string;
|
||||||
|
}
|
||||||
|
if (options.name) {
|
||||||
|
body.name = options.name as string;
|
||||||
|
}
|
||||||
|
if (options.name) {
|
||||||
|
body.name = options.name as string;
|
||||||
|
}
|
||||||
|
if (options.unsubscribedFromEmails) {
|
||||||
|
body.unsubscribed_from_emails = options.unsubscribedFromEmails as boolean;
|
||||||
|
}
|
||||||
|
if (options.updateLastRequestAt) {
|
||||||
|
body.update_last_request_at = options.updateLastRequestAt as boolean;
|
||||||
|
}
|
||||||
|
if (options.companies) {
|
||||||
|
const companies: ILeadCompany[] = [];
|
||||||
|
// @ts-ignore
|
||||||
|
options.companies.forEach( o => {
|
||||||
|
const company: ILeadCompany = {};
|
||||||
|
company.company_id = o;
|
||||||
|
companies.push(company);
|
||||||
|
});
|
||||||
|
body.companies = companies;
|
||||||
|
}
|
||||||
|
if (!jsonActive) {
|
||||||
|
const customAttributesValues = (this.getNodeParameter('customAttributesUi') as IDataObject).customAttributesValues as IDataObject[];
|
||||||
|
if (customAttributesValues) {
|
||||||
|
const customAttributes = {};
|
||||||
|
for (let i = 0; i < customAttributesValues.length; i++) {
|
||||||
|
// @ts-ignore
|
||||||
|
customAttributes[customAttributesValues[i].name] = customAttributesValues[i].value;
|
||||||
|
}
|
||||||
|
body.custom_attributes = customAttributes;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const customAttributesJson = validateJSON(this.getNodeParameter('customAttributesJson') as string);
|
||||||
|
if (customAttributesJson) {
|
||||||
|
body.custom_attributes = customAttributesJson;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
response = await intercomApiRequest.call(this, '/contacts', 'POST', body);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Intercom Error: ${JSON.stringify(err)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
json: {},
|
json: response,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,7 +125,31 @@ export const leadFields = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Custom Attributes',
|
displayName: 'Custom Attributes',
|
||||||
name: 'customAttributes',
|
name: 'customAttributesJson',
|
||||||
|
type: 'json',
|
||||||
|
required: false,
|
||||||
|
typeOptions: {
|
||||||
|
alwaysOpenEditWindow: true,
|
||||||
|
},
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'lead',
|
||||||
|
],
|
||||||
|
operation: [
|
||||||
|
'create',
|
||||||
|
],
|
||||||
|
jsonParameters: [
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
default: '',
|
||||||
|
description: 'A hash of key/value pairs to represent custom data you want to attribute to a user.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Custom Attributes',
|
||||||
|
name: 'customAttributesUi',
|
||||||
type: 'fixedCollection',
|
type: 'fixedCollection',
|
||||||
default: '',
|
default: '',
|
||||||
placeholder: 'Add Attribute',
|
placeholder: 'Add Attribute',
|
||||||
|
|||||||
16
packages/nodes-base/nodes/Intercom/LeadInterface.ts
Normal file
16
packages/nodes-base/nodes/Intercom/LeadInterface.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { IDataObject } from "n8n-workflow";
|
||||||
|
|
||||||
|
export interface ILeadCompany {
|
||||||
|
company_id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILead {
|
||||||
|
email: string;
|
||||||
|
phone?: string;
|
||||||
|
name?: string;
|
||||||
|
custom_attributes?: IDataObject;
|
||||||
|
companies?: ILeadCompany[];
|
||||||
|
last_request_at?: number;
|
||||||
|
unsubscribed_from_emails?: boolean;
|
||||||
|
update_last_request_at?: boolean;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user