From b8b5ba4e79cd346205b8b0b89a8a288b0eca9d39 Mon Sep 17 00:00:00 2001 From: neomaking Date: Wed, 16 Oct 2019 22:53:30 +0200 Subject: [PATCH] Add Discord Webhook node --- .../credentials/DiscordApi.credentials.ts | 18 ++ .../DiscordWebhook/DiscordWebhook.node.ts | 185 ++++++++++++++++++ .../nodes/DiscordWebhook/discord.png | Bin 0 -> 2365 bytes packages/nodes-base/package.json | 4 +- 4 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 packages/nodes-base/credentials/DiscordApi.credentials.ts create mode 100644 packages/nodes-base/nodes/DiscordWebhook/DiscordWebhook.node.ts create mode 100644 packages/nodes-base/nodes/DiscordWebhook/discord.png diff --git a/packages/nodes-base/credentials/DiscordApi.credentials.ts b/packages/nodes-base/credentials/DiscordApi.credentials.ts new file mode 100644 index 0000000000..96bd2fb2ea --- /dev/null +++ b/packages/nodes-base/credentials/DiscordApi.credentials.ts @@ -0,0 +1,18 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class DiscordApi implements ICredentialType { + name = 'discordApi'; + displayName = 'Discord API'; + properties = [ + { + displayName: 'Webhook URI', + name: 'webhookUri', + type: 'string' as NodePropertyTypes, + default: '', + }, + ]; +} diff --git a/packages/nodes-base/nodes/DiscordWebhook/DiscordWebhook.node.ts b/packages/nodes-base/nodes/DiscordWebhook/DiscordWebhook.node.ts new file mode 100644 index 0000000000..08266f9ca4 --- /dev/null +++ b/packages/nodes-base/nodes/DiscordWebhook/DiscordWebhook.node.ts @@ -0,0 +1,185 @@ +import { IExecuteFunctions } from 'n8n-core'; +import { + IDataObject, + INodeTypeDescription, + INodeExecutionData, + INodeType, +} from 'n8n-workflow'; + +interface Attachment { + fields: { + item?: object[]; + }; +} + +export class DiscordWebhook implements INodeType { + description: INodeTypeDescription = { + displayName: 'Discord Webhook', + name: 'discordwebhook', + icon: 'file:discord.png', + group: ['output'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Sends data to Discord', + defaults: { + name: 'Discord Webhook', + color: '#BB2244', + }, + inputs: ['main'], + outputs: ['main'], + credentials: [ + { + name: 'discordApi', + required: true, + } + ], + properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Message', + value: 'message', + }, + ], + default: 'message', + description: 'The resource to operate on.', + }, + + + + // ---------------------------------- + // operations + // ---------------------------------- + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'message', + ], + }, + }, + options: [ + { + name: 'Post', + value: 'post', + description: 'Post a message into a channel', + }, + ], + default: 'post', + description: 'The operation to perform.', + }, + + // ---------------------------------- + // message + // ---------------------------------- + + // ---------------------------------- + // message:post + // ---------------------------------- + { + displayName: 'Text', + name: 'text', + type: 'string', + typeOptions: { + alwaysOpenEditWindow: true, + }, + default: '', + displayOptions: { + show: { + operation: [ + 'post' + ], + resource: [ + 'message', + ], + }, + }, + description: 'The text to send.', + } + ], + }; + + + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + + const credentials = this.getCredentials('discordApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + let operation: string; + let resource: string; + let requestMethod = 'POST'; + + // For Post + let body: IDataObject; + // For Query string + let qs: IDataObject; + + for (let i = 0; i < items.length; i++) { + body = {}; + qs = {}; + + resource = this.getNodeParameter('resource', i) as string; + operation = this.getNodeParameter('operation', i) as string; + + if (resource === 'message') { + if (operation === 'post') { + // ---------------------------------- + // message:post + // ---------------------------------- + + requestMethod = 'POST'; + body.content = this.getNodeParameter('text', i) as string; + + const attachments = this.getNodeParameter('attachments', i, []) as unknown as Attachment[]; + + // The node does save the fields data differently than the API + // expects so fix the data befre we send the request + for (const attachment of attachments) { + if (attachment.fields !== undefined) { + if (attachment.fields.item !== undefined) { + // Move the field-content up + // @ts-ignore + attachment.fields = attachment.fields.item; + } else { + // If it does not have any items set remove it + delete attachment.fields; + } + } + } + body['attachments'] = attachments; + } + } else { + throw new Error(`The resource "${resource}" is not known!`); + } + + const options = { + method: requestMethod, + body, + qs, + uri: `${credentials.webhookUri}`, + headers: { + 'content-type': 'application/json; charset=utf-8' + }, + json: true + }; + + const responseData = await this.helpers.request(options); + + returnData.push(responseData as IDataObject); + } + + return [this.helpers.returnJsonArray(returnData)]; + } +} diff --git a/packages/nodes-base/nodes/DiscordWebhook/discord.png b/packages/nodes-base/nodes/DiscordWebhook/discord.png new file mode 100644 index 0000000000000000000000000000000000000000..f42e5500f21ff870076eb989b3787c7b6fe329f1 GIT binary patch literal 2365 zcmV-D3BvY?P)tKn%{z!-onf7_xSyttmKrX z;^F1?+~M`;>-c()+}Ymry2R{$liiV`;p*-AjGo}i((t&#>(AElskY~uspD96)e`rQ zh5!Hvk4Z#9RCt{2oolyqJP?M7R1Qk1mTKwY{r^Am)v7*t zEeiw!fj}S-2m}IwKp+rEpyRFB%&Fpb)XKNr<(xA#uNdd~ZmWs#_@Ic`OAtZyp`|um zZyA7xsxGHaaNtIZM@KhyYx~O(qi+Bfhh%sFvVhPEVHP?_V6S0J} zRWU|*ZWf`Cs|GRVbCXEK)wI@pxz$*<*1ZbEyo5g3;vU<5`*jD%pR!=(K8G>6KpjD#S{S@v*!2?WmEC}19C zeNL$#~D?3oxpgZdakWhbm?2n9sC*#z6dL?YJkKW5m(x>O*p!8cLD|_n#hZj!hw<10HZE*{GSFIs{uv@Vfh2v$lt&Kqux>VsjYm6PCp|_6i^IEzWC73h^Iec z;w$K9q?%U8O~um2s-2M#1q_3lml2<(g^$;B;JdOzUx$01<@bSP27B4;p`B4Wb8OH2 z=}g6p6oM<_?CHNFm?zREzmu6d+8Jes57Unuk?{VL(Wg)P2;#}!RJw@N&nPY!H$9~& z>bYX$HSWY^EkYbLp`Ve6yJIUkL=VE{yLM!|3FjFZk=Vb{Do0S~iv0VVx1Hzok>r& zN|S7}YG#yJ09|hkZ>^;}8*Vc5USb7kW|Z9i(PeD4>rVa%Suz~STj*v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~oL7nJC8nk&*COZL)fhA#QT8)iYYw8LgkOu>rDPM$6&rXS8>uDT6byI@0I9!pZg6A$Y6Ff5C{YU jfj}S-2m}IwKug#EvX7ZV_!qZ$00000NkvXXu0mjfs?MLh literal 0 HcmV?d00001 diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index c7d3809070..e48c7ca9fe 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -51,7 +51,8 @@ "dist/credentials/TelegramApi.credentials.js", "dist/credentials/TrelloApi.credentials.js", "dist/credentials/TwilioApi.credentials.js", - "dist/credentials/TypeformApi.credentials.js" + "dist/credentials/TypeformApi.credentials.js", + "dist/credentials/DiscordApi.credentials.js" ], "nodes": [ "dist/nodes/ActiveCampaign/ActiveCampaign.node.js", @@ -64,6 +65,7 @@ "dist/nodes/Chargebee/ChargebeeTrigger.node.js", "dist/nodes/Cron.node.js", "dist/nodes/Dropbox/Dropbox.node.js", + "dist/nodes/DiscordWebhook/DiscordWebhook.node.js", "dist/nodes/EditImage.node.js", "dist/nodes/EmailReadImap.node.js", "dist/nodes/EmailSend.node.js",