AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { createAction } from '@activepieces/pieces-framework';
import { buildFlowOriginContextBlock, slackSendMessage, textToSectionBlocks } from '../common/utils';
import { slackAuth } from '../auth';
import { assertNotNullOrUndefined } from '@activepieces/pieces-framework';
import { ExecutionType } from '@activepieces/pieces-framework';
import { profilePicture, text, userId, username, mentionOriginFlow } from '../common/props';
import { ChatPostMessageResponse, WebClient } from '@slack/web-api';
import { getBotToken, SlackAuthValue } from '../common/auth-helpers';
import { approvalActionOutputSchema } from '../output-schemas';
export const requestApprovalDirectMessageAction = createAction({
auth: slackAuth,
name: 'request_approval_direct_message',
classification: 'WRITE',
displayName: 'Request Approval from A User',
description:
'Sends approve/reject buttons to a user and waits for the answer.',
audience: 'both',
aiMetadata: {
description:
'Direct-message one Slack user an approval request with Approve/Disapprove buttons and pause the flow until they respond. Pick this for a private one-to-one approval gate; use Request Action in A Channel to ask a whole channel. Not idempotent: each run sends a new DM and creates a fresh wait.',
idempotent: false,
},
outputSchema: approvalActionOutputSchema,
propertyGroups: [
{
key: 'user_picker',
display: 'section',
label: 'User',
icon: 'user',
props: ['userId'],
},
{
key: 'message',
display: 'section',
label: 'Message',
icon: 'text',
props: ['text'],
},
],
props: {
userId: userId(true),
text,
username,
profilePicture,
mentionOriginFlow,
},
async run(context) {
if (context.executionType === ExecutionType.BEGIN) {
const token = getBotToken(context.auth as SlackAuthValue);
const { userId, username, profilePicture, mentionOriginFlow } = context.propsValue;
assertNotNullOrUndefined(token, 'token');
assertNotNullOrUndefined(text, 'text');
assertNotNullOrUndefined(userId, 'userId');
const waitpoint = await context.run.createWaitpoint({
type: 'WEBHOOK',
});
const postMessage = await slackSendMessage({
token,
text: `${context.propsValue.text}`,
username,
profilePicture,
conversationId: userId,
});
const dmId = (postMessage as ChatPostMessageResponse).channel as string;
const messageTs = (postMessage as ChatPostMessageResponse).ts as string
const approvalLink = waitpoint.buildResumeUrl({
queryParams: { action: 'approve', channel: dmId, messageTs },
});
const disapprovalLink = waitpoint.buildResumeUrl({
queryParams: { action: 'disapprove', channel: dmId, messageTs },
});
const client = new WebClient(token);
await client.chat.update({
ts:messageTs,
channel:dmId,
text: context.propsValue.text,
blocks: [
...textToSectionBlocks(`${context.propsValue.text}`),
{
type: 'actions',
block_id: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: 'Approve',
},
style: 'primary',
value: approvalLink,
action_id: 'approve',
},
{
type: 'button',
text: {
type: 'plain_text',
text: 'Disapprove',
},
style: 'danger',
value: disapprovalLink,
action_id: 'disapprove',
},
],
},
...(mentionOriginFlow ? [buildFlowOriginContextBlock(context)] : []),
],
});
context.run.waitForWaitpoint(waitpoint.id);
return {
approved: false, // default approval is false
messageTs
};
} else {
const approved = context.resumePayload.queryParams['action'] === 'approve';
const channel = context.resumePayload.queryParams['channel'];
const messageTs = context.resumePayload.queryParams['messageTs'];
const token = getBotToken(context.auth as SlackAuthValue);
try {
if (token && channel && messageTs) {
const client = new WebClient(token);
const statusText = approved ? 'Approved' : 'Disapproved';
await client.chat.update({
channel,
ts: messageTs,
text: `${context.propsValue.text}\n\n${statusText}`,
blocks: [
...textToSectionBlocks(`${context.propsValue.text}`),
{
type: 'section',
text: {
type: 'mrkdwn',
text: approved
? ':white_check_mark: *Approved*'
: ':x: *Disapproved*',
},
},
],
});
}
} catch (e) {
// Ignore errors from updating the message, as it's cosmetic
}
return { approved, messageTs };
}
},
});