feat: Human in the loop (#10675)

Co-authored-by: Giulio Andreini <g.andreini@gmail.com>
This commit is contained in:
Michael Kret 2024-10-07 16:45:22 +03:00 committed by GitHub
parent d2713ae50a
commit 41228b472d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 1298 additions and 196 deletions

View file

@ -63,7 +63,7 @@ describe('WaitingWebhooks', () => {
* Arrange
*/
executionRepository.findSingleExecution.mockResolvedValue(
mock<IExecutionResponse>({ finished: true }),
mock<IExecutionResponse>({ finished: true, workflowData: { nodes: [] } }),
);
/**

View file

@ -1,5 +1,11 @@
import type express from 'express';
import { NodeHelpers, Workflow } from 'n8n-workflow';
import {
type INodes,
type IWorkflowBase,
NodeHelpers,
SEND_AND_WAIT_OPERATION,
Workflow,
} from 'n8n-workflow';
import { Service } from 'typedi';
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
@ -42,6 +48,29 @@ export class WaitingWebhooks implements IWebhookManager {
execution.data.executionData!.nodeExecutionStack[0].node.disabled = true;
}
private isSendAndWaitRequest(nodes: INodes, suffix: string | undefined) {
return (
suffix &&
Object.keys(nodes).some(
(node) =>
nodes[node].id === suffix && nodes[node].parameters.operation === SEND_AND_WAIT_OPERATION,
)
);
}
private getWorkflow(workflowData: IWorkflowBase) {
return new Workflow({
id: workflowData.id,
name: workflowData.name,
nodes: workflowData.nodes,
connections: workflowData.connections,
active: workflowData.active,
nodeTypes: this.nodeTypes,
staticData: workflowData.staticData,
settings: workflowData.settings,
});
}
async executeWebhook(
req: WaitingWebhookRequest,
res: express.Response,
@ -66,10 +95,21 @@ export class WaitingWebhooks implements IWebhookManager {
throw new ConflictError(`The execution "${executionId} is running already.`);
}
if (execution.finished || execution.data.resultData.error) {
if (execution.data?.resultData?.error) {
throw new ConflictError(`The execution "${executionId} has finished already.`);
}
if (execution.finished) {
const { workflowData } = execution;
const { nodes } = this.getWorkflow(workflowData);
if (this.isSendAndWaitRequest(nodes, suffix)) {
res.render('send-and-wait-no-action-required', { isTestWebhook: false });
return { noWebhookResponse: true };
} else {
throw new ConflictError(`The execution "${executionId} has finished already.`);
}
}
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted as string;
// Set the node as disabled so that the data does not get executed again as it would result
@ -83,17 +123,7 @@ export class WaitingWebhooks implements IWebhookManager {
execution.data.resultData.runData[lastNodeExecuted].pop();
const { workflowData } = execution;
const workflow = new Workflow({
id: workflowData.id,
name: workflowData.name,
nodes: workflowData.nodes,
connections: workflowData.connections,
active: workflowData.active,
nodeTypes: this.nodeTypes,
staticData: workflowData.staticData,
settings: workflowData.settings,
});
const workflow = this.getWorkflow(workflowData);
const workflowStartNode = workflow.getNode(lastNodeExecuted);
if (workflowStartNode === null) {
@ -116,9 +146,14 @@ export class WaitingWebhooks implements IWebhookManager {
if (webhookData === undefined) {
// If no data got found it means that the execution can not be started via a webhook.
// Return 404 because we do not want to give any data if the execution exists or not.
if (this.isSendAndWaitRequest(workflow.nodes, suffix)) {
res.render('send-and-wait-no-action-required', { isTestWebhook: false });
return { noWebhookResponse: true };
} else {
const errorMessage = `The workflow for execution "${executionId}" does not contain a waiting webhook with a matching path/method.`;
throw new NotFoundError(errorMessage);
}
}
const runExecutionData = execution.data;

View file

@ -0,0 +1,73 @@
<html lang='en'>
<head>
<meta charset='UTF-8' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<link rel='icon' type='image/png' href='https://n8n.io/favicon.ico' />
<link
href='http://fonts.googleapis.com/css?family=Open+Sans'
rel='stylesheet'
type='text/css'
/>
<title>No action required</title>
<style>
*, ::after, ::before { box-sizing: border-box; margin: 0; padding: 0; } body { font-family:
Open Sans, sans-serif; font-weight: 400; font-size: 12px; display: flex; flex-direction:
column; justify-content: start; background-color: #FBFCFE; } .container { margin: auto;
text-align: center; padding-top: 24px; width: 448px; } .card { padding: 24px;
background-color: white; border: 1px solid #DBDFE7; border-radius: 8px; box-shadow: 0px 4px
16px 0px #634DFF0F; margin-bottom: 16px; } .n8n-link a { color: #7E8186; font-weight: 600;
font-size: 12px; text-decoration: none; } .n8n-link svg { display: inline-block;
vertical-align: middle; } .header h1 { color: #525356; font-size: 20px; font-weight: 400;
padding-bottom: 8px; } .header p { color: #7E8186; font-size: 14px; font-weight: 400; }
</style>
</head>
<body>
<div class='container'>
<section>
<div class='card'>
<div class='header'>
<h1>No action required</h1>
</div>
</div>
<div class='n8n-link'>
<a
href='https://n8n.io/?utm_source=n8n-internal&amp;utm_medium=form-trigger&amp'
target='_blank'
>
Form automated with
<svg
width='73'
height='20'
viewBox='0 0 73 20'
fill='none'
xmlns='http://www.w3.org/2000/svg'
>
<path
fill-rule='evenodd'
clip-rule='evenodd'
d='M40.2373 4C40.2373 6.20915 38.4464 8 36.2373 8C34.3735 8 32.8074 6.72525 32.3633 5H26.7787C25.801 5 24.9666 5.70685 24.8059 6.6712L24.6415 7.6576C24.4854 8.59415 24.0116 9.40925 23.3417 10C24.0116 10.5907 24.4854 11.4058 24.6415 12.3424L24.8059 13.3288C24.9666 14.2931 25.801 15 26.7787 15H28.3633C28.8074 13.2747 30.3735 12 32.2373 12C34.4464 12 36.2373 13.7908 36.2373 16C36.2373 18.2092 34.4464 20 32.2373 20C30.3735 20 28.8074 18.7253 28.3633 17H26.7787C24.8233 17 23.1546 15.5864 22.8331 13.6576L22.6687 12.6712C22.508 11.7069 21.6736 11 20.6959 11H19.0645C18.5652 12.64 17.0406 13.8334 15.2373 13.8334C13.434 13.8334 11.9094 12.64 11.4101 11H9.06449C8.56519 12.64 7.04059 13.8334 5.2373 13.8334C3.02817 13.8334 1.2373 12.0424 1.2373 9.83335C1.2373 7.6242 3.02817 5.83335 5.2373 5.83335C7.16069 5.83335 8.76699 7.19085 9.15039 9H11.3242C11.7076 7.19085 13.3139 5.83335 15.2373 5.83335C17.1607 5.83335 18.767 7.19085 19.1504 9H20.6959C21.6736 9 22.508 8.29315 22.6687 7.3288L22.8331 6.3424C23.1546 4.41365 24.8233 3 26.7787 3H32.3633C32.8074 1.27478 34.3735 0 36.2373 0C38.4464 0 40.2373 1.79086 40.2373 4ZM38.2373 4C38.2373 5.10455 37.3419 6 36.2373 6C35.1327 6 34.2373 5.10455 34.2373 4C34.2373 2.89543 35.1327 2 36.2373 2C37.3419 2 38.2373 2.89543 38.2373 4ZM5.2373 11.8334C6.34189 11.8334 7.23729 10.9379 7.23729 9.83335C7.23729 8.72875 6.34189 7.83335 5.2373 7.83335C4.13273 7.83335 3.2373 8.72875 3.2373 9.83335C3.2373 10.9379 4.13273 11.8334 5.2373 11.8334ZM15.2373 11.8334C16.3419 11.8334 17.2373 10.9379 17.2373 9.83335C17.2373 8.72875 16.3419 7.83335 15.2373 7.83335C14.1327 7.83335 13.2373 8.72875 13.2373 9.83335C13.2373 10.9379 14.1327 11.8334 15.2373 11.8334ZM32.2373 18C33.3419 18 34.2373 17.1045 34.2373 16C34.2373 14.8954 33.3419 14 32.2373 14C31.1327 14 30.2373 14.8954 30.2373 16C30.2373 17.1045 31.1327 18 32.2373 18Z'
fill='#EA4B71'
></path>
<path
d='M44.2393 15.0007H46.3277V10.5791C46.3277 9.12704 47.2088 8.49074 48.204 8.49074C49.183 8.49074 49.9498 9.14334 49.9498 10.4812V15.0007H52.038V10.057C52.038 7.91969 50.798 6.67969 48.8567 6.67969C47.633 6.67969 46.9477 7.16914 46.4582 7.80544H46.3277L46.1482 6.84284H44.2393V15.0007Z'
fill='#101330'
></path>
<path
d='M60.0318 9.50205V9.40415C60.7498 9.0452 61.4678 8.4252 61.4678 7.20155C61.4678 5.43945 60.0153 4.37891 58.0088 4.37891C55.9528 4.37891 54.4843 5.5047 54.4843 7.23415C54.4843 8.4089 55.1698 9.0452 55.9203 9.40415V9.50205C55.0883 9.79575 54.0928 10.6768 54.0928 12.1452C54.0928 13.9237 55.5613 15.1637 57.9923 15.1637C60.4233 15.1637 61.8428 13.9237 61.8428 12.1452C61.8428 10.6768 60.8638 9.81205 60.0318 9.50205ZM57.9923 5.87995C58.8083 5.87995 59.4118 6.40205 59.4118 7.2831C59.4118 8.16415 58.7918 8.6863 57.9923 8.6863C57.1928 8.6863 56.5238 8.16415 56.5238 7.2831C56.5238 6.38575 57.1603 5.87995 57.9923 5.87995ZM57.9923 13.5974C57.0458 13.5974 56.2793 12.9937 56.2793 11.9658C56.2793 11.0358 56.9153 10.3342 57.9758 10.3342C59.0203 10.3342 59.6568 11.0195 59.6568 11.9984C59.6568 12.9937 58.9223 13.5974 57.9923 13.5974Z'
fill='#101330'
></path>
<path
d='M63.9639 15.0007H66.0524V10.5791C66.0524 9.12704 66.9334 8.49074 67.9289 8.49074C68.9079 8.49074 69.6744 9.14334 69.6744 10.4812V15.0007H71.7629V10.057C71.7629 7.91969 70.5229 6.67969 68.5814 6.67969C67.3579 6.67969 66.6724 7.16914 66.1829 7.80544H66.0524L65.8729 6.84284H63.9639V15.0007Z'
fill='#101330'
></path>
</svg>
</a>
</div>
</section>
</div>
</body>
</html>

View file

@ -17,7 +17,7 @@ import type {
NodeOperationError,
Workflow,
} from 'n8n-workflow';
import { NodeConnectionType, NodeHelpers } from 'n8n-workflow';
import { NodeConnectionType, NodeHelpers, SEND_AND_WAIT_OPERATION } from 'n8n-workflow';
import type { StyleValue } from 'vue';
import { computed, onMounted, ref, watch } from 'vue';
import xss from 'xss';
@ -336,6 +336,9 @@ const waiting = computed(() => {
: i18n.baseText('node.theNodeIsWaitingFormCall');
return event;
}
if (node?.parameters.operation === SEND_AND_WAIT_OPERATION) {
return i18n.baseText('node.theNodeIsWaitingUserInput');
}
const waitDate = new Date(workflowExecution.waitTill);
if (waitDate.toISOString() === WAIT_TIME_UNLIMITED) {
return i18n.baseText('node.theNodeIsWaitingIndefinitelyForAnIncomingWebhookCall');

View file

@ -36,7 +36,7 @@ import type {
ITaskData,
Workflow,
} from 'n8n-workflow';
import { NodeConnectionType, NodeHelpers } from 'n8n-workflow';
import { NodeConnectionType, NodeHelpers, SEND_AND_WAIT_OPERATION } from 'n8n-workflow';
import type { INodeUi } from '@/Interface';
import {
CUSTOM_API_CALL_KEY,
@ -348,6 +348,11 @@ export function useCanvasMapping({
return acc;
}
if (node?.parameters.operation === SEND_AND_WAIT_OPERATION) {
acc[node.id] = i18n.baseText('node.theNodeIsWaitingUserInput');
return acc;
}
const waitDate = new Date(workflowExecution.waitTill);
if (waitDate.toISOString() === WAIT_TIME_UNLIMITED) {

View file

@ -378,6 +378,7 @@ export function useRunWorkflow(useRunWorkflowOpts: { router: ReturnType<typeof u
['error', 'canceled', 'crashed', 'success'].includes(execution.status)
) {
workflowsStore.setWorkflowExecutionData(execution);
workflowsStore.activeExecutionId = null;
if (timeoutId) clearTimeout(timeoutId);
resolve();
return;

View file

@ -966,6 +966,7 @@
"ndv.output.runNodeHint": "Execute this node to view data",
"ndv.output.runNodeHintSubNode": "Output will appear here once the parent node is run",
"ndv.output.waitNodeWaitingForWebhook": "Execution will continue when webhook is received on ",
"ndv.output.sendAndWaitWaitingApproval": "Execution will continue after the user's response",
"ndv.output.waitNodeWaitingForFormSubmission": "Execution will continue when form is submitted on ",
"ndv.output.waitNodeWaiting": "Execution will continue when wait time is over",
"ndv.output.insertTestData": "set mock data",
@ -1016,6 +1017,7 @@
"node.theNodeIsWaitingIndefinitelyForAnIncomingWebhookCall": "The node is waiting for an incoming webhook call (indefinitely)",
"node.theNodeIsWaitingWebhookCall": "The node is waiting for an incoming webhook call",
"node.theNodeIsWaitingFormCall": "The node is waiting for a form submission",
"node.theNodeIsWaitingUserInput": "The node is waiting for user input",
"node.waitingForYouToCreateAnEventIn": "Waiting for you to create an event in {nodeType}",
"node.discovery.pinData.canvas": "You can pin this output instead of waiting for a test event. Open node to do so.",
"node.discovery.pinData.ndv": "You can pin this output instead of waiting for a test event.",

View file

@ -53,7 +53,13 @@ import type {
IWorkflowSettings,
INodeType,
} from 'n8n-workflow';
import { deepCopy, NodeConnectionType, NodeHelpers, Workflow } from 'n8n-workflow';
import {
deepCopy,
NodeConnectionType,
NodeHelpers,
SEND_AND_WAIT_OPERATION,
Workflow,
} from 'n8n-workflow';
import { findLast } from 'lodash-es';
import { useRootStore } from '@/stores/root.store';
@ -168,7 +174,11 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, () => {
const allNodes = computed<INodeUi[]>(() => workflow.value.nodes);
const isWaitingExecution = computed(() => {
return allNodes.value.some((node) => node.type === WAIT_NODE_TYPE && node.disabled !== true);
return allNodes.value.some(
(node) =>
(node.type === WAIT_NODE_TYPE || node.parameters.operation === SEND_AND_WAIT_OPERATION) &&
node.disabled !== true,
);
});
// Names of all nodes currently on canvas.

View file

@ -1,4 +1,11 @@
import type { ExecutionStatus, IDataObject, INode, IPinData, IRunData } from 'n8n-workflow';
import {
SEND_AND_WAIT_OPERATION,
type ExecutionStatus,
type IDataObject,
type INode,
type IPinData,
type IRunData,
} from 'n8n-workflow';
import type { ExecutionFilterType, ExecutionsQueryFilter } from '@/Interface';
import { isEmpty } from '@/utils/typesUtils';
import { FORM_TRIGGER_NODE_TYPE } from '../constants';
@ -160,6 +167,10 @@ export const waitingNodeTooltip = () => {
return `${message}<a href="${resumeUrl}" target="_blank">${resumeUrl}</a>`;
}
}
if (lastNode?.parameters.operation === SEND_AND_WAIT_OPERATION) {
return i18n.baseText('ndv.output.sendAndWaitWaitingApproval');
}
} catch (error) {
// do not throw error if could not compose tooltip
}

View file

@ -18,20 +18,6 @@ import { DateTime } from 'luxon';
import isEmpty from 'lodash/isEmpty';
export interface IEmail {
from?: string;
to?: string;
cc?: string;
bcc?: string;
replyTo?: string;
inReplyTo?: string;
reference?: string;
subject: string;
body: string;
htmlBody?: string;
attachments?: IDataObject[];
}
export interface IAttachments {
type: string;
name: string;
@ -40,6 +26,8 @@ export interface IAttachments {
import MailComposer from 'nodemailer/lib/mail-composer';
import { getGoogleAccessToken } from '../GenericFunctions';
import { escapeHtml } from '../../../utils/utilities';
import type { IEmail } from '../../../utils/sendAndWait/interfaces';
export async function googleApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
@ -516,22 +504,7 @@ export function unescapeSnippets(items: INodeExecutionData[]) {
const result = items.map((item) => {
const snippet = item.json.snippet as string;
if (snippet) {
item.json.snippet = snippet.replace(/&amp;|&lt;|&gt;|&#39;|&quot;/g, (match) => {
switch (match) {
case '&amp;':
return '&';
case '&lt;':
return '<';
case '&gt;':
return '>';
case '&#39;':
return "'";
case '&quot;':
return '"';
default:
return match;
}
});
item.json.snippet = escapeHtml(snippet);
}
return item;
});

View file

@ -13,7 +13,7 @@ import {
} from 'n8n-workflow';
import isEmpty from 'lodash/isEmpty';
import type { IEmail } from '../GenericFunctions';
import type { IEmail } from '../../../../utils/sendAndWait/interfaces';
import {
encodeEmail,
extractEmail,

View file

@ -8,9 +8,14 @@ import type {
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import {
NodeConnectionType,
NodeOperationError,
SEND_AND_WAIT_OPERATION,
WAIT_TIME_UNLIMITED,
} from 'n8n-workflow';
import type { IEmail } from '../GenericFunctions';
import type { IEmail } from '../../../../utils/sendAndWait/interfaces';
import {
encodeEmail,
googleApiRequest,
@ -33,6 +38,12 @@ import { draftFields, draftOperations } from './DraftDescription';
import { threadFields, threadOperations } from './ThreadDescription';
import {
getSendAndWaitProperties,
createEmail,
sendAndWaitWebhook,
} from '../../../../utils/sendAndWait/utils';
const versionDescription: INodeTypeDescription = {
displayName: 'Gmail',
name: 'gmail',
@ -67,6 +78,17 @@ const versionDescription: INodeTypeDescription = {
},
},
],
webhooks: [
{
name: 'default',
httpMethod: 'GET',
responseMode: 'onReceived',
responseData: '',
path: '={{ $nodeId }}',
restartWebhook: true,
isFullPath: true,
},
],
properties: [
{
displayName: 'Authentication',
@ -125,6 +147,16 @@ const versionDescription: INodeTypeDescription = {
//-------------------------------
...messageOperations,
...messageFields,
...getSendAndWaitProperties([
{
displayName: 'To',
name: 'sendTo',
type: 'string',
default: '',
required: true,
placeholder: 'e.g. info@example.com',
},
]),
//-------------------------------
// Thread Operations
//-------------------------------
@ -221,6 +253,8 @@ export class GmailV2 implements INodeType {
},
};
webhook = sendAndWaitWebhook;
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
@ -229,6 +263,17 @@ export class GmailV2 implements INodeType {
const nodeVersion = this.getNode().typeVersion;
const instanceId = this.getInstanceId();
if (resource === 'message' && operation === SEND_AND_WAIT_OPERATION) {
const email: IEmail = createEmail(this);
await googleApiRequest.call(this, 'POST', '/gmail/v1/users/me/messages/send', {
raw: await encodeEmail(email),
});
await this.putExecutionToWait(new Date(WAIT_TIME_UNLIMITED));
return [this.getInputData()];
}
let responseData;
for (let i = 0; i < items.length; i++) {

View file

@ -1,4 +1,4 @@
import type { INodeProperties } from 'n8n-workflow';
import { SEND_AND_WAIT_OPERATION, type INodeProperties } from 'n8n-workflow';
import { appendAttributionOption } from '../../../../utils/descriptions';
export const messageOperations: INodeProperties[] = [
@ -58,6 +58,11 @@ export const messageOperations: INodeProperties[] = [
value: 'send',
action: 'Send a message',
},
{
name: 'Send and Wait for Approval',
value: SEND_AND_WAIT_OPERATION,
action: 'Send a message and wait for approval',
},
],
default: 'send',
},

View file

@ -11,6 +11,7 @@ import type {
import { NodeOperationError } from 'n8n-workflow';
import get from 'lodash/get';
import { getSendAndWaitConfig } from '../../../utils/sendAndWait/utils';
export async function slackApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
@ -219,3 +220,81 @@ export function validateJSON(json: string | undefined): any {
}
return result;
}
export function getTarget(
context: IExecuteFunctions,
itemIndex: number,
idType: 'user' | 'channel',
): string {
let target = '';
if (idType === 'channel') {
target = context.getNodeParameter('channelId', itemIndex, undefined, {
extractValue: true,
}) as string;
} else {
target = context.getNodeParameter('user', itemIndex, undefined, {
extractValue: true,
}) as string;
}
if (
idType === 'user' &&
(context.getNodeParameter('user', itemIndex) as IDataObject).mode === 'username'
) {
target = target.slice(0, 1) === '@' ? target : `@${target}`;
}
return target;
}
export function createSendAndWaitMessageBody(context: IExecuteFunctions) {
const select = context.getNodeParameter('select', 0) as 'user' | 'channel';
const target = getTarget(context, 0, select);
const config = getSendAndWaitConfig(context);
const body: IDataObject = {
channel: target,
blocks: [
{
type: 'divider',
},
{
type: 'section',
text: {
type: 'plain_text',
text: config.message,
emoji: true,
},
},
{
type: 'section',
text: {
type: 'plain_text',
text: ' ',
},
},
{
type: 'divider',
},
{
type: 'actions',
elements: config.options.map((option) => {
return {
type: 'button',
style: option.style === 'primary' ? 'primary' : undefined,
text: {
type: 'plain_text',
text: option.label,
emoji: true,
},
url: `${config.url}?approved=${option.value}`,
};
}),
},
],
};
return body;
}

View file

@ -1,4 +1,4 @@
import type { INodeProperties } from 'n8n-workflow';
import { SEND_AND_WAIT_OPERATION, type INodeProperties } from 'n8n-workflow';
export const messageOperations: INodeProperties[] = [
{
@ -32,6 +32,11 @@ export const messageOperations: INodeProperties[] = [
value: 'post',
action: 'Send a message',
},
{
name: 'Send and Wait for Approval',
value: SEND_AND_WAIT_OPERATION,
action: 'Send a message and wait for approval',
},
{
name: 'Update',
value: 'update',
@ -42,6 +47,134 @@ export const messageOperations: INodeProperties[] = [
},
];
export const sendToSelector: INodeProperties = {
displayName: 'Send Message To',
name: 'select',
type: 'options',
required: true,
displayOptions: {
show: {
resource: ['message'],
operation: ['post'],
},
},
options: [
{
name: 'Channel',
value: 'channel',
},
{
name: 'User',
value: 'user',
},
],
default: '',
placeholder: 'Select...',
};
export const channelRLC: INodeProperties = {
displayName: 'Channel',
name: 'channelId',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
placeholder: 'Select a channel...',
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select a channel...',
typeOptions: {
searchListMethod: 'getChannels',
searchable: true,
},
},
{
displayName: 'By ID',
name: 'id',
type: 'string',
validation: [
{
type: 'regex',
properties: {
regex: '[a-zA-Z0-9]{2,}',
errorMessage: 'Not a valid Slack Channel ID',
},
},
],
placeholder: 'C0122KQ70S7E',
},
{
displayName: 'By Name',
name: 'name',
type: 'string',
placeholder: '#general',
},
{
displayName: 'By URL',
name: 'url',
type: 'string',
placeholder: 'https://app.slack.com/client/TS9594PZK/B0556F47Z3A',
validation: [
{
type: 'regex',
properties: {
regex: 'http(s)?://app.slack.com/client/.*/([a-zA-Z0-9]{2,})',
errorMessage: 'Not a valid Slack Channel URL',
},
},
],
extractValue: {
type: 'regex',
regex: 'https://app.slack.com/client/.*/([a-zA-Z0-9]{2,})',
},
},
],
required: true,
description: 'The Slack channel to send to',
};
export const userRLC: INodeProperties = {
displayName: 'User',
name: 'user',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
placeholder: 'Select a user...',
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select a user...',
typeOptions: {
searchListMethod: 'getUsers',
searchable: true,
},
},
{
displayName: 'By ID',
name: 'id',
type: 'string',
validation: [
{
type: 'regex',
properties: {
regex: '[a-zA-Z0-9]{2,}',
errorMessage: 'Not a valid Slack User ID',
},
},
],
placeholder: 'U123AB45JGM',
},
{
displayName: 'By username',
name: 'username',
type: 'string',
placeholder: '@username',
},
],
};
export const messageFields: INodeProperties[] = [
/* ----------------------------------------------------------------------- */
/* message:getPermalink
@ -125,88 +258,9 @@ export const messageFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */
/* message:post */
/* -------------------------------------------------------------------------- */
sendToSelector,
{
displayName: 'Send Message To',
name: 'select',
type: 'options',
required: true,
displayOptions: {
show: {
resource: ['message'],
operation: ['post'],
},
},
options: [
{
name: 'Channel',
value: 'channel',
},
{
name: 'User',
value: 'user',
},
],
default: '',
placeholder: 'Select...',
},
{
displayName: 'Channel',
name: 'channelId',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
placeholder: 'Select a channel...',
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select a channel...',
typeOptions: {
searchListMethod: 'getChannels',
searchable: true,
},
},
{
displayName: 'By ID',
name: 'id',
type: 'string',
validation: [
{
type: 'regex',
properties: {
regex: '[a-zA-Z0-9]{2,}',
errorMessage: 'Not a valid Slack Channel ID',
},
},
],
placeholder: 'C0122KQ70S7E',
},
{
displayName: 'By Name',
name: 'name',
type: 'string',
placeholder: '#general',
},
{
displayName: 'By URL',
name: 'url',
type: 'string',
placeholder: 'https://app.slack.com/client/TS9594PZK/B0556F47Z3A',
validation: [
{
type: 'regex',
properties: {
regex: 'http(s)?://app.slack.com/client/.*/([a-zA-Z0-9]{2,})',
errorMessage: 'Not a valid Slack Channel URL',
},
},
],
extractValue: {
type: 'regex',
regex: 'https://app.slack.com/client/.*/([a-zA-Z0-9]{2,})',
},
},
],
...channelRLC,
displayOptions: {
show: {
operation: ['post'],
@ -214,15 +268,9 @@ export const messageFields: INodeProperties[] = [
select: ['channel'],
},
},
required: true,
description: 'The Slack channel to send to',
},
{
displayName: 'User',
name: 'user',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
placeholder: 'Select a user...',
...userRLC,
displayOptions: {
show: {
operation: ['post'],
@ -230,39 +278,6 @@ export const messageFields: INodeProperties[] = [
select: ['user'],
},
},
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select a user...',
typeOptions: {
searchListMethod: 'getUsers',
searchable: true,
},
},
{
displayName: 'By ID',
name: 'id',
type: 'string',
validation: [
{
type: 'regex',
properties: {
regex: '[a-zA-Z0-9]{2,}',
errorMessage: 'Not a valid Slack User ID',
},
},
],
placeholder: 'U123AB45JGM',
},
{
displayName: 'By username',
name: 'username',
type: 'string',
placeholder: '@username',
},
],
},
{
displayName: 'Message Type',

View file

@ -15,17 +15,36 @@ import type {
JsonObject,
} from 'n8n-workflow';
import { BINARY_ENCODING, NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import {
BINARY_ENCODING,
NodeConnectionType,
NodeOperationError,
SEND_AND_WAIT_OPERATION,
WAIT_TIME_UNLIMITED,
} from 'n8n-workflow';
import moment from 'moment-timezone';
import { channelFields, channelOperations } from './ChannelDescription';
import { messageFields, messageOperations } from './MessageDescription';
import {
channelRLC,
messageFields,
messageOperations,
sendToSelector,
userRLC,
} from './MessageDescription';
import { starFields, starOperations } from './StarDescription';
import { fileFields, fileOperations } from './FileDescription';
import { reactionFields, reactionOperations } from './ReactionDescription';
import { userGroupFields, userGroupOperations } from './UserGroupDescription';
import { userFields, userOperations } from './UserDescription';
import { slackApiRequest, slackApiRequestAllItems, getMessageContent } from './GenericFunctions';
import {
slackApiRequest,
slackApiRequestAllItems,
getMessageContent,
getTarget,
createSendAndWaitMessageBody,
} from './GenericFunctions';
import { getSendAndWaitProperties, sendAndWaitWebhook } from '../../../utils/sendAndWait/utils';
export class SlackV2 implements INodeType {
description: INodeTypeDescription;
@ -60,6 +79,17 @@ export class SlackV2 implements INodeType {
},
},
],
webhooks: [
{
name: 'default',
httpMethod: 'GET',
responseMode: 'onReceived',
responseData: '',
path: '={{ $nodeId }}',
restartWebhook: true,
isFullPath: true,
},
],
properties: [
{
displayName: 'Authentication',
@ -120,6 +150,25 @@ export class SlackV2 implements INodeType {
...channelFields,
...messageOperations,
...messageFields,
...getSendAndWaitProperties([
{ ...sendToSelector, default: 'user' },
{
...channelRLC,
displayOptions: {
show: {
select: ['channel'],
},
},
},
{
...userRLC,
displayOptions: {
show: {
select: ['user'],
},
},
},
]).filter((p) => p.name !== 'subject'),
...starOperations,
...starFields,
...fileOperations,
@ -307,6 +356,8 @@ export class SlackV2 implements INodeType {
},
};
webhook = sendAndWaitWebhook;
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
@ -320,6 +371,18 @@ export class SlackV2 implements INodeType {
const nodeVersion = this.getNode().typeVersion;
const instanceId = this.getInstanceId();
if (resource === 'message' && operation === SEND_AND_WAIT_OPERATION) {
await slackApiRequest.call(
this,
'POST',
'/chat.postMessage',
createSendAndWaitMessageBody(this),
);
await this.putExecutionToWait(new Date(WAIT_TIME_UNLIMITED));
return [this.getInputData()];
}
for (let i = 0; i < length; i++) {
try {
responseData = {
@ -749,22 +812,8 @@ export class SlackV2 implements INodeType {
if (resource === 'message') {
//https://api.slack.com/methods/chat.postMessage
if (operation === 'post') {
const select = this.getNodeParameter('select', i) as string;
let target =
select === 'channel'
? (this.getNodeParameter('channelId', i, undefined, {
extractValue: true,
}) as string)
: (this.getNodeParameter('user', i, undefined, {
extractValue: true,
}) as string);
if (
select === 'user' &&
(this.getNodeParameter('user', i) as IDataObject).mode === 'username'
) {
target = target.slice(0, 1) === '@' ? target : `@${target}`;
}
const select = this.getNodeParameter('select', i) as 'user' | 'channel';
const target = getTarget(this, i, select);
const { sendAsUser } = this.getNodeParameter('otherOptions', i) as IDataObject;
const content = getMessageContent.call(this, i, nodeVersion, instanceId);

View file

@ -0,0 +1,146 @@
import { type MockProxy, mock } from 'jest-mock-extended';
import type { IExecuteFunctions } from 'n8n-workflow';
import { getTarget, createSendAndWaitMessageBody } from '../../V2/GenericFunctions';
describe('Slack Utility Functions', () => {
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
beforeEach(() => {
mockExecuteFunctions = mock<IExecuteFunctions>();
mockExecuteFunctions.getNode.mockReturnValue({ name: 'Slack', typeVersion: 1 } as any);
jest.clearAllMocks();
});
describe('getTarget', () => {
it('should return corect target id', () => {
mockExecuteFunctions.getNodeParameter.mockImplementation((parameterName: string) => {
if (parameterName === 'user') {
return 'testUser';
}
return 'testChannel';
});
expect(getTarget(mockExecuteFunctions, 0, 'channel')).toEqual('testChannel');
expect(getTarget(mockExecuteFunctions, 0, 'user')).toEqual('testUser');
});
});
describe('createSendAndWaitMessageBody', () => {
it('should create message with single button', () => {
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('channel');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('channelID');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('message');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('subject');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('localhost');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('node123');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({});
expect(createSendAndWaitMessageBody(mockExecuteFunctions)).toEqual({
blocks: [
{
type: 'divider',
},
{
text: {
emoji: true,
text: 'message',
type: 'plain_text',
},
type: 'section',
},
{
text: {
text: ' ',
type: 'plain_text',
},
type: 'section',
},
{
type: 'divider',
},
{
elements: [
{
style: 'primary',
text: {
emoji: true,
text: 'Approve',
type: 'plain_text',
},
type: 'button',
url: 'localhost/node123?approved=true',
},
],
type: 'actions',
},
],
channel: 'channelID',
});
});
it('should create message with double buttona', () => {
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('channel');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('channelID');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('message');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('subject');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('localhost');
mockExecuteFunctions.evaluateExpression.mockReturnValueOnce('node123');
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({ approvalType: 'double' });
expect(createSendAndWaitMessageBody(mockExecuteFunctions)).toEqual({
blocks: [
{
type: 'divider',
},
{
text: {
emoji: true,
text: 'message',
type: 'plain_text',
},
type: 'section',
},
{
text: {
text: ' ',
type: 'plain_text',
},
type: 'section',
},
{
type: 'divider',
},
{
elements: [
{
style: undefined,
text: {
emoji: true,
text: 'Disapprove',
type: 'plain_text',
},
type: 'button',
url: 'localhost/node123?approved=false',
},
{
style: 'primary',
text: {
emoji: true,
text: 'Approve',
type: 'plain_text',
},
type: 'button',
url: 'localhost/node123?approved=true',
},
],
type: 'actions',
},
],
channel: 'channelID',
});
});
});
});

View file

@ -0,0 +1,141 @@
export const BUTTON_STYLE_SECONDARY =
'display:inline-block; text-decoration:none; background-color:#fff; color:#4a4a4a; padding:12px 24px; font-family: Arial,sans-serif; font-size:14px;font-weight:600; border:1px solid #d1d1d1; border-radius:6px; min-width:120px; margin: 12px 6px 0 6px;';
export const BUTTON_STYLE_PRIMARY =
'display:inline-block; text-decoration:none; background-color:#ff6d5a; color: #fff; padding:12px 24px; font-family: Arial,sans-serif; font-size:14px;font-weight:600; border-radius:6px; min-width:120px; margin: 12px 2px 0 2px;';
export const ACTION_RECORDED_PAGE = `
<html lang='en'>
<head>
<meta charset='UTF-8' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<link rel='icon' type='image/png' href='https://n8n.io/favicon.ico' />
<link
href='http://fonts.googleapis.com/css?family=Open+Sans'
rel='stylesheet'
type='text/css'
/>
<title>Action recorded</title>
<style>
*, ::after, ::before { box-sizing: border-box; margin: 0; padding: 0; } body { font-family:
Open Sans, sans-serif; font-weight: 400; font-size: 12px; display: flex; flex-direction:
column; justify-content: start; background-color: #FBFCFE; } .container { margin: auto;
text-align: center; padding-top: 24px; width: 448px; } .card { padding: 24px;
background-color: white; border: 1px solid #DBDFE7; border-radius: 8px; box-shadow: 0px 4px
16px 0px #634DFF0F; margin-bottom: 16px; } .n8n-link a { color: #7E8186; font-weight: 600;
font-size: 12px; text-decoration: none; } .n8n-link svg { display: inline-block;
vertical-align: middle; } .header h1 { color: #525356; font-size: 20px; font-weight: 400;
padding-bottom: 8px; } .header p { color: #7E8186; font-size: 14px; font-weight: 400; }
</style>
</head>
<body>
<div class='container'>
<section>
<div class='card'>
<div class='header'>
<h1>Got it, thanks</h1>
<p>This page can be closed now</p>
</div>
</div>
<div class='n8n-link'>
<a
href='https://n8n.io/?utm_source=n8n-internal&amp;utm_medium=send-and-wait'
target='_blank'
>
Automated with
<svg
width='73'
height='20'
viewBox='0 0 73 20'
fill='none'
xmlns='http://www.w3.org/2000/svg'
>
<path
fill-rule='evenodd'
clip-rule='evenodd'
d='M40.2373 4C40.2373 6.20915 38.4464 8 36.2373 8C34.3735 8 32.8074 6.72525 32.3633 5H26.7787C25.801 5 24.9666 5.70685 24.8059 6.6712L24.6415 7.6576C24.4854 8.59415 24.0116 9.40925 23.3417 10C24.0116 10.5907 24.4854 11.4058 24.6415 12.3424L24.8059 13.3288C24.9666 14.2931 25.801 15 26.7787 15H28.3633C28.8074 13.2747 30.3735 12 32.2373 12C34.4464 12 36.2373 13.7908 36.2373 16C36.2373 18.2092 34.4464 20 32.2373 20C30.3735 20 28.8074 18.7253 28.3633 17H26.7787C24.8233 17 23.1546 15.5864 22.8331 13.6576L22.6687 12.6712C22.508 11.7069 21.6736 11 20.6959 11H19.0645C18.5652 12.64 17.0406 13.8334 15.2373 13.8334C13.434 13.8334 11.9094 12.64 11.4101 11H9.06449C8.56519 12.64 7.04059 13.8334 5.2373 13.8334C3.02817 13.8334 1.2373 12.0424 1.2373 9.83335C1.2373 7.6242 3.02817 5.83335 5.2373 5.83335C7.16069 5.83335 8.76699 7.19085 9.15039 9H11.3242C11.7076 7.19085 13.3139 5.83335 15.2373 5.83335C17.1607 5.83335 18.767 7.19085 19.1504 9H20.6959C21.6736 9 22.508 8.29315 22.6687 7.3288L22.8331 6.3424C23.1546 4.41365 24.8233 3 26.7787 3H32.3633C32.8074 1.27478 34.3735 0 36.2373 0C38.4464 0 40.2373 1.79086 40.2373 4ZM38.2373 4C38.2373 5.10455 37.3419 6 36.2373 6C35.1327 6 34.2373 5.10455 34.2373 4C34.2373 2.89543 35.1327 2 36.2373 2C37.3419 2 38.2373 2.89543 38.2373 4ZM5.2373 11.8334C6.34189 11.8334 7.23729 10.9379 7.23729 9.83335C7.23729 8.72875 6.34189 7.83335 5.2373 7.83335C4.13273 7.83335 3.2373 8.72875 3.2373 9.83335C3.2373 10.9379 4.13273 11.8334 5.2373 11.8334ZM15.2373 11.8334C16.3419 11.8334 17.2373 10.9379 17.2373 9.83335C17.2373 8.72875 16.3419 7.83335 15.2373 7.83335C14.1327 7.83335 13.2373 8.72875 13.2373 9.83335C13.2373 10.9379 14.1327 11.8334 15.2373 11.8334ZM32.2373 18C33.3419 18 34.2373 17.1045 34.2373 16C34.2373 14.8954 33.3419 14 32.2373 14C31.1327 14 30.2373 14.8954 30.2373 16C30.2373 17.1045 31.1327 18 32.2373 18Z'
fill='#EA4B71'
></path>
<path
d='M44.2393 15.0007H46.3277V10.5791C46.3277 9.12704 47.2088 8.49074 48.204 8.49074C49.183 8.49074 49.9498 9.14334 49.9498 10.4812V15.0007H52.038V10.057C52.038 7.91969 50.798 6.67969 48.8567 6.67969C47.633 6.67969 46.9477 7.16914 46.4582 7.80544H46.3277L46.1482 6.84284H44.2393V15.0007Z'
fill='#101330'
></path>
<path
d='M60.0318 9.50205V9.40415C60.7498 9.0452 61.4678 8.4252 61.4678 7.20155C61.4678 5.43945 60.0153 4.37891 58.0088 4.37891C55.9528 4.37891 54.4843 5.5047 54.4843 7.23415C54.4843 8.4089 55.1698 9.0452 55.9203 9.40415V9.50205C55.0883 9.79575 54.0928 10.6768 54.0928 12.1452C54.0928 13.9237 55.5613 15.1637 57.9923 15.1637C60.4233 15.1637 61.8428 13.9237 61.8428 12.1452C61.8428 10.6768 60.8638 9.81205 60.0318 9.50205ZM57.9923 5.87995C58.8083 5.87995 59.4118 6.40205 59.4118 7.2831C59.4118 8.16415 58.7918 8.6863 57.9923 8.6863C57.1928 8.6863 56.5238 8.16415 56.5238 7.2831C56.5238 6.38575 57.1603 5.87995 57.9923 5.87995ZM57.9923 13.5974C57.0458 13.5974 56.2793 12.9937 56.2793 11.9658C56.2793 11.0358 56.9153 10.3342 57.9758 10.3342C59.0203 10.3342 59.6568 11.0195 59.6568 11.9984C59.6568 12.9937 58.9223 13.5974 57.9923 13.5974Z'
fill='#101330'
></path>
<path
d='M63.9639 15.0007H66.0524V10.5791C66.0524 9.12704 66.9334 8.49074 67.9289 8.49074C68.9079 8.49074 69.6744 9.14334 69.6744 10.4812V15.0007H71.7629V10.057C71.7629 7.91969 70.5229 6.67969 68.5814 6.67969C67.3579 6.67969 66.6724 7.16914 66.1829 7.80544H66.0524L65.8729 6.84284H63.9639V15.0007Z'
fill='#101330'
></path>
</svg>
</a>
</div>
</section>
</div>
</body>
</html>`;
export function createEmailBody(message: string, buttons: string, instanceId?: string) {
const utm_campaign = instanceId ? `&utm_campaign=${instanceId}` : '';
const n8nWebsiteLink = `https://n8n.io/?utm_source=n8n-internal&utm_medium=send-and-wait${utm_campaign}`;
return `
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My form</title>
</head>
<body
style="font-family: Arial, sans-serif; font-size: 12px; background-color: #fbfcfe; margin: 0; padding: 0;">
<table width="100%" cellpadding="0" cellspacing="0"
style="background-color:#fbfcfe; border: 1px solid #dbdfe7; border-radius: 8px;">
<tr>
<td align="center" style="padding: 24px 0;">
<table width="448" cellpadding="0" cellspacing="0" border="0"
style="width: 100%; max-width: 448px; background-color: #ffffff; border: 1px solid #dbdfe7; border-radius: 8px; padding: 24px; box-shadow: 0px 4px 16px rgba(99, 77, 255, 0.06);">
<tr>
<td
style="text-align: center; padding-top: 8px; font-family: Arial, sans-serif; font-size: 14px; color: #7e8186;">
<p>${message}</p>
</td>
</tr>
<tr>
<td align="center" style="padding-top: 12px;">
${buttons}
</td>
</tr>
</table>
<!-- Divider -->
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-bottom: 24px;">
<tr>
<td style="border-top: 0px solid #dbdfe7;"></td>
</tr>
</table>
<!-- Footer -->
<table width="100%" cellpadding="0" cellspacing="0" border="0"
style="text-align: center; color: #7e8186; font-family: Arial, sans-serif; font-size: 12px;">
<tr>
<td>
<a href=${n8nWebsiteLink}
target="_blank" style="color: #7e8186; text-decoration: none;">Automated with
n8n</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
`;
}

View file

@ -0,0 +1,15 @@
import type { IDataObject } from 'n8n-workflow';
export interface IEmail {
from?: string;
to?: string;
cc?: string;
bcc?: string;
replyTo?: string;
inReplyTo?: string;
reference?: string;
subject: string;
body: string;
htmlBody?: string;
attachments?: IDataObject[];
}

View file

@ -0,0 +1,212 @@
import { type MockProxy, mock } from 'jest-mock-extended';
import type { IExecuteFunctions, INodeProperties, IWebhookFunctions } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import {
getSendAndWaitProperties,
getSendAndWaitConfig,
createEmail,
sendAndWaitWebhook,
MESSAGE_PREFIX,
} from '../utils';
describe('Send and Wait utils tests', () => {
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
let mockWebhookFunctions: MockProxy<IWebhookFunctions>;
beforeEach(() => {
mockExecuteFunctions = mock<IExecuteFunctions>();
mockWebhookFunctions = mock<IWebhookFunctions>();
});
describe('getSendAndWaitProperties', () => {
it('should return properties with correct display options', () => {
const targetProperties: INodeProperties[] = [
{
displayName: 'Test Property',
name: 'testProperty',
type: 'string',
default: '',
},
];
const result = getSendAndWaitProperties(targetProperties);
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
displayOptions: {
show: {
resource: ['message'],
operation: ['sendAndWait'],
},
},
}),
]),
);
});
});
describe('getSendAndWaitConfig', () => {
it('should return correct config for single approval', () => {
mockExecuteFunctions.getNodeParameter.mockImplementation((parameterName: string) => {
const params: { [key: string]: any } = {
message: 'Test message',
subject: 'Test subject',
'approvalOptions.values': {
approvalType: 'single',
approveLabel: 'Approve',
buttonApprovalStyle: 'primary',
},
};
return params[parameterName];
});
mockExecuteFunctions.evaluateExpression.mockImplementation((expression: string) => {
const expressions: { [key: string]: string } = {
'{{ $execution?.resumeUrl }}': 'http://localhost',
'{{ $nodeId }}': 'testNodeId',
};
return expressions[expression];
});
const config = getSendAndWaitConfig(mockExecuteFunctions);
expect(config).toEqual({
title: 'Test subject',
message: 'Test message',
url: 'http://localhost/testNodeId',
options: [
{
label: 'Approve',
value: 'true',
style: 'primary',
},
],
});
});
it('should return correct config for double approval', () => {
mockExecuteFunctions.getNodeParameter.mockImplementation((parameterName: string) => {
const params: { [key: string]: any } = {
message: 'Test message',
subject: 'Test subject',
'approvalOptions.values': {
approvalType: 'double',
approveLabel: 'Approve',
buttonApprovalStyle: 'primary',
disapproveLabel: 'Reject',
buttonDisapprovalStyle: 'secondary',
},
};
return params[parameterName];
});
mockExecuteFunctions.evaluateExpression.mockImplementation((expression: string) => {
const expressions: { [key: string]: string } = {
'{{ $execution?.resumeUrl }}': 'http://localhost',
'{{ $nodeId }}': 'testNodeId',
};
return expressions[expression];
});
const config = getSendAndWaitConfig(mockExecuteFunctions);
expect(config.options).toHaveLength(2);
expect(config.options).toEqual(
expect.arrayContaining([
{
label: 'Reject',
value: 'false',
style: 'secondary',
},
{
label: 'Approve',
value: 'true',
style: 'primary',
},
]),
);
});
});
describe('createEmail', () => {
beforeEach(() => {
mockExecuteFunctions.getNodeParameter.mockImplementation((parameterName: string) => {
const params: { [key: string]: any } = {
sendTo: 'test@example.com',
message: 'Test message',
subject: 'Test subject',
'approvalOptions.values': {
approvalType: 'single',
approveLabel: 'Approve',
buttonApprovalStyle: 'primary',
},
};
return params[parameterName];
});
mockExecuteFunctions.evaluateExpression.mockImplementation((expression: string) => {
const expressions: { [key: string]: string } = {
'{{ $execution?.resumeUrl }}': 'http://localhost',
'{{ $nodeId }}': 'testNodeId',
};
return expressions[expression];
});
});
it('should create a valid email object', () => {
const email = createEmail(mockExecuteFunctions);
expect(email).toEqual({
to: 'test@example.com',
subject: `${MESSAGE_PREFIX}Test subject`,
body: '',
htmlBody: expect.stringContaining('Test message'),
});
});
it('should throw NodeOperationError for invalid email address', () => {
mockExecuteFunctions.getNodeParameter.mockImplementation((parameterName: string) => {
const params: { [key: string]: any } = {
sendTo: 'invalid@@email.com',
message: 'Test message',
subject: 'Test subject',
'approvalOptions.values': {
approvalType: 'single',
},
};
return params[parameterName];
});
expect(() => createEmail(mockExecuteFunctions)).toThrow(NodeOperationError);
});
});
describe('sendAndWaitWebhook', () => {
it('should handle approved webhook', async () => {
mockWebhookFunctions.getRequestObject.mockReturnValue({
query: { approved: 'true' },
} as any);
const result = await sendAndWaitWebhook.call(mockWebhookFunctions);
expect(result).toEqual({
webhookResponse: expect.any(String),
workflowData: [[{ json: { data: { approved: true } } }]],
});
});
it('should handle disapproved webhook', async () => {
mockWebhookFunctions.getRequestObject.mockReturnValue({
query: { approved: 'false' },
} as any);
const result = await sendAndWaitWebhook.call(mockWebhookFunctions);
expect(result).toEqual({
webhookResponse: expect.any(String),
workflowData: [[{ json: { data: { approved: false } } }]],
});
});
});
});

View file

@ -0,0 +1,254 @@
import { NodeOperationError, SEND_AND_WAIT_OPERATION, updateDisplayOptions } from 'n8n-workflow';
import type { INodeProperties, IExecuteFunctions, IWebhookFunctions } from 'n8n-workflow';
import type { IEmail } from './interfaces';
import { escapeHtml } from '../utilities';
import {
ACTION_RECORDED_PAGE,
BUTTON_STYLE_PRIMARY,
BUTTON_STYLE_SECONDARY,
createEmailBody,
} from './email-templates';
type SendAndWaitConfig = {
title: string;
message: string;
url: string;
options: Array<{ label: string; value: string; style: string }>;
};
export const MESSAGE_PREFIX = 'ACTION REQUIRED: ';
// Operation Properties ----------------------------------------------------------
export function getSendAndWaitProperties(
targetProperties: INodeProperties[],
resource: string = 'message',
additionalProperties: INodeProperties[] = [],
) {
const buttonStyle: INodeProperties = {
displayName: 'Button Style',
name: 'buttonStyle',
type: 'options',
default: 'primary',
options: [
{
name: 'Primary',
value: 'primary',
},
{
name: 'Secondary',
value: 'secondary',
},
],
};
const sendAndWait: INodeProperties[] = [
...targetProperties,
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: '',
required: true,
placeholder: 'e.g. Approval required',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
default: '',
required: true,
typeOptions: {
rows: 5,
},
},
{
displayName: 'Approval Options',
name: 'approvalOptions',
type: 'fixedCollection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'Values',
name: 'values',
values: [
{
displayName: 'Type of Approval',
name: 'approvalType',
type: 'options',
placeholder: 'Add option',
default: 'single',
options: [
{
name: 'Approve Only',
value: 'single',
},
{
name: 'Approve and Disapprove',
value: 'double',
},
],
},
{
displayName: 'Approve Button Label',
name: 'approveLabel',
type: 'string',
default: 'Approve',
displayOptions: {
show: {
approvalType: ['single', 'double'],
},
},
},
{
...buttonStyle,
displayName: 'Approve Button Style',
name: 'buttonApprovalStyle',
displayOptions: {
show: {
approvalType: ['single', 'double'],
},
},
},
{
displayName: 'Disapprove Button Label',
name: 'disapproveLabel',
type: 'string',
default: 'Decline',
displayOptions: {
show: {
approvalType: ['double'],
},
},
},
{
...buttonStyle,
displayName: 'Disapprove Button Style',
name: 'buttonDisapprovalStyle',
default: 'secondary',
displayOptions: {
show: {
approvalType: ['double'],
},
},
},
],
},
],
},
...additionalProperties,
{
displayName:
'Use the wait node for more complex approval flows. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.wait" target="_blank">More info</a>',
name: 'useWaitNotice',
type: 'notice',
default: '',
},
];
return updateDisplayOptions(
{
show: {
resource: [resource],
operation: [SEND_AND_WAIT_OPERATION],
},
},
sendAndWait,
);
}
// Webhook Function --------------------------------------------------------------
export async function sendAndWaitWebhook(this: IWebhookFunctions) {
const query = this.getRequestObject().query as { approved: 'false' | 'true' };
const approved = query.approved === 'true';
return {
webhookResponse: ACTION_RECORDED_PAGE,
workflowData: [[{ json: { data: { approved } } }]],
};
}
// Send and Wait Config -----------------------------------------------------------
export function getSendAndWaitConfig(context: IExecuteFunctions): SendAndWaitConfig {
const message = escapeHtml((context.getNodeParameter('message', 0, '') as string).trim());
const subject = escapeHtml(context.getNodeParameter('subject', 0, '') as string);
const resumeUrl = context.evaluateExpression('{{ $execution?.resumeUrl }}', 0) as string;
const nodeId = context.evaluateExpression('{{ $nodeId }}', 0) as string;
const approvalOptions = context.getNodeParameter('approvalOptions.values', 0, {}) as {
approvalType?: 'single' | 'double';
approveLabel?: string;
buttonApprovalStyle?: string;
disapproveLabel?: string;
buttonDisapprovalStyle?: string;
};
const config: SendAndWaitConfig = {
title: subject,
message,
url: `${resumeUrl}/${nodeId}`,
options: [],
};
if (approvalOptions.approvalType === 'double') {
const approveLabel = escapeHtml(approvalOptions.approveLabel || 'Approve');
const buttonApprovalStyle = approvalOptions.buttonApprovalStyle || 'primary';
const disapproveLabel = escapeHtml(approvalOptions.disapproveLabel || 'Disapprove');
const buttonDisapprovalStyle = approvalOptions.buttonDisapprovalStyle || 'secondary';
config.options.push({
label: disapproveLabel,
value: 'false',
style: buttonDisapprovalStyle,
});
config.options.push({
label: approveLabel,
value: 'true',
style: buttonApprovalStyle,
});
} else {
const label = escapeHtml(approvalOptions.approveLabel || 'Approve');
const style = approvalOptions.buttonApprovalStyle || 'primary';
config.options.push({
label,
value: 'true',
style,
});
}
return config;
}
function createButton(url: string, label: string, approved: string, style: string) {
let buttonStyle = BUTTON_STYLE_PRIMARY;
if (style === 'secondary') {
buttonStyle = BUTTON_STYLE_SECONDARY;
}
return `<a href="${url}?approved=${approved}" target="_blank" style="${buttonStyle}">${label}</a>`;
}
export function createEmail(context: IExecuteFunctions) {
const to = (context.getNodeParameter('sendTo', 0, '') as string).trim();
const config = getSendAndWaitConfig(context);
if (to.indexOf('@') === -1 || (to.match(/@/g) || []).length > 1) {
const description = `The email address '${to}' in the 'To' field isn't valid or contains multiple addresses. Please provide only a single email address.`;
throw new NodeOperationError(context.getNode(), 'Invalid email address', {
description,
itemIndex: 0,
});
}
const buttons: string[] = [];
for (const option of config.options) {
buttons.push(createButton(config.url, option.label, option.value, option.style));
}
const instanceId = context.getInstanceId();
const email: IEmail = {
to,
subject: `${MESSAGE_PREFIX}${config.title}`,
body: '',
htmlBody: createEmailBody(config.message, buttons.join('\n'), instanceId),
};
return email;
}

View file

@ -404,3 +404,28 @@ export const sanitizeDataPathKey = (item: IDataObject, key: string) => {
}
return key;
};
/**
* Escape HTML
*
* @param {string} text The text to escape
*/
export function escapeHtml(text: string): string {
if (!text) return '';
return text.replace(/&amp;|&lt;|&gt;|&#39;|&quot;/g, (match) => {
switch (match) {
case '&amp;':
return '&';
case '&lt;':
return '<';
case '&gt;':
return '>';
case '&#39;':
return "'";
case '&quot;':
return '"';
default:
return match;
}
});
}

View file

@ -112,3 +112,5 @@ export const SINGLE_EXECUTION_NODES: { [key: string]: { [key: string]: NodeParam
operation: [undefined], // default info
},
};
export const SEND_AND_WAIT_OPERATION = 'sendAndWait';

View file

@ -1391,6 +1391,7 @@ export class WorkflowDataProxy {
$thisItemIndex: this.itemIndex,
$thisRunIndex: this.runIndex,
$nodeVersion: that.workflow.getNode(that.activeNodeName)?.typeVersion,
$nodeId: that.workflow.getNode(that.activeNodeName)?.id,
};
return new Proxy(base, {