This commit is contained in:
Ricardo Espinoza 2020-01-05 13:34:09 -05:00
parent 9c196416cd
commit 7d2e857613
5 changed files with 251 additions and 7 deletions

View file

@ -0,0 +1,29 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class ZendeskApi implements ICredentialType {
name = 'zendeskApi';
displayName = 'Zendesk API';
properties = [
{
displayName: 'URL',
name: 'url',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Email',
name: 'email',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'API Token',
name: 'apiToken',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,64 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions,
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('zendeskApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64')
let options: OptionsWithUri = {
headers: { 'Authorization': `Basic ${base64Key}`},
method,
qs,
body,
uri: uri ||`${credentials.domain}/api/v2${resource}`,
json: true
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
let errorMessage = error.message;
if (error.response.body) {
errorMessage = error.response.body.message || error.response.body.Message || error.message;
}
throw new Error(errorMessage);
}
}
/**
* Make an API request to paginated flow endpoint
* and return all results
*/
export async function zendeskApiRequestAllItems(this: IHookFunctions | IExecuteFunctions| ILoadOptionsFunctions, propertyName: string, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
let uri: string | undefined;
do {
responseData = await zendeskApiRequest.call(this, method, resource, body, query, uri);
query.continuation = responseData.pagination.continuation;
returnData.push.apply(returnData, responseData[propertyName]);
} while (
responseData.pagination !== undefined &&
responseData.pagination.has_more_items !== undefined &&
responseData.pagination.has_more_items !== false
);
return returnData;
}

View file

@ -0,0 +1,149 @@
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
INodeTypeDescription,
INodeType,
IWebhookResponseData,
} from 'n8n-workflow';
import {
zendeskApiRequest,
} from './GenericFunctions';
export class ZendeskTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Zendesk Trigger',
name: 'zendesk',
icon: 'file:zendesk.png',
group: ['trigger'],
version: 1,
description: 'Handle Zendesk events via webhooks',
defaults: {
name: 'Zendesk Trigger',
color: '#559922',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'zendeskApi',
required: true,
}
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Service',
name: 'service',
type: 'options',
required: true,
options: [
{
name: 'Support',
value: 'support',
}
],
default: 'support',
description: '',
},
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
displayOptions: {
show: {
service: [
'support'
]
}
},
options: [
{
name: 'ticket.status.open',
value: 'ticket.status.open'
},
],
required: true,
default: [],
description: '',
},
],
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
let webhooks;
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/webhooks/${webhookData.webhookId}/`;
try {
webhooks = await zendeskApiRequest.call(this, 'GET', endpoint);
} catch (e) {
return false;
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
let body, responseData;
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event') as string;
const actions = this.getNodeParameter('actions') as string[];
const endpoint = `/webhooks/`;
// @ts-ignore
body = {
endpoint_url: webhookUrl,
actions: actions.join(','),
event_id: event,
};
try {
responseData = await zendeskApiRequest.call(this, 'POST', endpoint, body);
} catch(error) {
console.log(error)
return false;
}
// @ts-ignore
webhookData.webhookId = responseData.id;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
let responseData;
const webhookData = this.getWorkflowStaticData('node');
const endpoint = `/webhooks/${webhookData.webhookId}/`;
try {
responseData = await zendeskApiRequest.call(this, 'DELETE', endpoint);
} catch(error) {
return false;
}
if (!responseData.success) {
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject();
return {
workflowData: [
this.helpers.returnJsonArray(req.body)
],
};
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View file

@ -71,11 +71,12 @@
"dist/credentials/TwilioApi.credentials.js",
"dist/credentials/TypeformApi.credentials.js",
"dist/credentials/MandrillApi.credentials.js",
"dist/credentials/TodoistApi.credentials.js",
"dist/credentials/TypeformApi.credentials.js",
"dist/credentials/TogglApi.credentials.js",
"dist/credentials/TodoistApi.credentials.js",
"dist/credentials/TypeformApi.credentials.js",
"dist/credentials/TogglApi.credentials.js",
"dist/credentials/VeroApi.credentials.js",
"dist/credentials/WordpressApi.credentials.js"
"dist/credentials/WordpressApi.credentials.js",
"dist/credentials/ZendeskApi.credentials.js"
],
"nodes": [
"dist/nodes/ActiveCampaign/ActiveCampaign.node.js",
@ -163,9 +164,10 @@
"dist/nodes/Toggl/TogglTrigger.node.js",
"dist/nodes/Vero/Vero.node.js",
"dist/nodes/WriteBinaryFile.node.js",
"dist/nodes/Webhook.node.js",
"dist/nodes/Wordpress/Wordpress.node.js",
"dist/nodes/Xml.node.js"
"dist/nodes/Webhook.node.js",
"dist/nodes/Wordpress/Wordpress.node.js",
"dist/nodes/Xml.node.js",
"dist/nodes/Zendesk/ZendeskTrigger.node.js"
]
},
"devDependencies": {