Skip to content

Commit

Permalink
feat(Slack Node): Add blocks to slack message update (#2182)
Browse files Browse the repository at this point in the history
* Adding blocks to slack message update

* Fixing lint

* Adding blocks to slack message update

* Fixing lint

* ⚡ added toggle to display json inputs in update operation

* ⚡ Improvements

* feat(Markdown Node): Add new node to covert between Markdown <> HTML (#1728)

* ✨ Markdown Node

* Tweaked wording

* ⬆️ Bump showdown to latest version

* ⚡ Small improvement

* 👕 Fix linting issue

* ⚡ Small improvements

* 🔨 added options, added continue on fail, some clean up

* ⚡ removed test code

* ⚡ added missing semicolumn

* 🔨 wip

* 🔨 replaced library for converting html to markdown, added options

* ⚡ lock file fix

* 🔨 clean up

Co-authored-by: sirdavidoff <[email protected]>
Co-authored-by: Michael Kret <[email protected]>

Co-authored-by: Michael Kret <[email protected]>
Co-authored-by: ricardo <[email protected]>
Co-authored-by: Ricardo Espinoza <[email protected]>
Co-authored-by: sirdavidoff <[email protected]>
Co-authored-by: Jan Oberhauser <[email protected]>
  • Loading branch information
6 people authored Apr 19, 2022
1 parent f566569 commit b5b6000
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 150 deletions.
25 changes: 23 additions & 2 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */

import { Credentials, NodeExecuteFunctions } from 'n8n-core';

// eslint-disable-next-line import/no-extraneous-dependencies
import { get } from 'lodash';
import { NodeVersionedType } from 'n8n-nodes-base';

import {
Expand Down Expand Up @@ -633,8 +634,10 @@ export class CredentialsHelper extends ICredentialsHelper {
mode,
);

let response: INodeExecutionData[][] | null | undefined;

try {
await routingNode.runNode(
response = await routingNode.runNode(
inputData,
runIndex,
nodeTypeCopy,
Expand Down Expand Up @@ -683,6 +686,24 @@ export class CredentialsHelper extends ICredentialsHelper {
};
}

if (
credentialTestFunction.testRequest.rules &&
Array.isArray(credentialTestFunction.testRequest.rules)
) {
// Special testing rules are defined so check all in order
for (const rule of credentialTestFunction.testRequest.rules) {
if (rule.type === 'responseSuccessBody') {
const responseData = response![0][0].json;
if (get(responseData, rule.properties.key) === rule.properties.value) {
return {
status: 'Error',
message: rule.properties.message,
};
}
}
}
}

return {
status: 'OK',
message: 'Connection successful!',
Expand Down
26 changes: 25 additions & 1 deletion packages/nodes-base/credentials/SlackApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
IAuthenticateBearer,
IAuthenticateQueryAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';


export class SlackApi implements ICredentialType {
name = 'slackApi';
displayName = 'Slack API';
Expand All @@ -17,4 +19,26 @@ export class SlackApi implements ICredentialType {
required: true,
},
];
authenticate: IAuthenticateBearer = {
type: 'bearer',
properties: {
tokenPropertyName: 'accessToken',
},
};
test: ICredentialTestRequest = {
request: {
baseURL: 'https://slack.com',
url: '/api/users.profile.get',
},
rules: [
{
type: 'responseSuccessBody',
properties: {
key: 'ok',
value: false,
message: 'Invalid access token',
},
},
],
};
}
26 changes: 10 additions & 16 deletions packages/nodes-base/nodes/Slack/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import {
IDataObject,
IOAuth2Options,
JsonObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
Expand All @@ -36,23 +37,16 @@ export async function slackApiRequest(this: IExecuteFunctions | IExecuteSingleFu
if (Object.keys(query).length === 0) {
delete options.qs;
}
try {
let response: any; // tslint:disable-line:no-any

if (authenticationMethod === 'accessToken') {
const credentials = await this.getCredentials('slackApi');
options.headers!.Authorization = `Bearer ${credentials.accessToken}`;
//@ts-ignore
response = await this.helpers.request(options);
} else {
const oAuth2Options: IOAuth2Options = {
tokenType: 'Bearer',
property: 'authed_user.access_token',
};

const oAuth2Options: IOAuth2Options = {
tokenType: 'Bearer',
property: 'authed_user.access_token',
};
//@ts-ignore
response = await this.helpers.requestOAuth2.call(this, 'slackOAuth2Api', options, oAuth2Options);
}
try {
let response: any; // tslint:disable-line:no-any
const credentialType = authenticationMethod === 'accessToken' ? 'slackApi' : 'slackOAuth2Api';
response = await this.helpers.requestWithAuthentication.call(this, credentialType, options, { oauth2: oAuth2Options });

if (response.ok === false) {
if (response.error === 'paid_teams_only') {
Expand All @@ -66,7 +60,7 @@ export async function slackApiRequest(this: IExecuteFunctions | IExecuteSingleFu

return response;
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

Expand Down
Loading

0 comments on commit b5b6000

Please sign in to comment.