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
24 changed files with 1298 additions and 196 deletions

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;
}