Request Interactive Slack Approval

Request Interactive Slack Approval

Script· approval slack Verified

by hugo697 · 1/4/2025

The script

Submitted by hugo697 Bun
Verified 82 days ago
1
import * as wmill from "windmill-client"
2

3
type Enums = {
4
  [key: string]: string[]
5
}
6

7
type DefaultArgs = {
8
  [key: string]: any
9
}
10

11
type ResApproval = {
12
  enums?: Enums
13
  default_args?: DefaultArgs
14
}
15

16
/**
17
 * Sends an interactive approval request via Slack and optionally returns dynamic enums and default arguments.
18
 *
19
 * [Enterprise Edition Only] To include form fields in the slack approval request, go to Advanced -> Suspend -> Form and define a form
20
 * Learn more at https://www.windmill.dev/docs/flows/flow_approval#form
21
 * 
22
 * @param {string} slackResourcePath - The path to the Slack resource in Windmill.
23
 * @param {string} channelId - The Slack channel ID where the approval request will be sent.
24
 * @param {string} [message] - Optional custom message to include in the Slack approval request.
25
 * @param {string} [approver] - Optional user ID or name of the approver for the request.
26
 * @param {DefaultArgs} [defaultArgsJson] - Optional object defining or overriding the default arguments to a form field.
27
 * @param {Enums} [dynamicEnumsJson] - Optional object overriding the enum default values of an emum form field .
28
 * 
29
 * @throws {Error} If the `wmill.requestInteractiveSlackApproval` call fails.
30
 * 
31
 * Example inputs:
32
 *   slackResourcePath: "/u/alex/my_slack_resource",
33
 *   "admins-slack-channel", 
34
 *   "Please approve this request",
35
 *   "approver123",
36
 *   { foo: ["choice1", "choice2"], bar: ["optionA", "optionB"] },
37
 *   { key1: "value1", key2: 42 }
38
 * );
39
 */
40
export async function main(
41
  slackResourcePath: string,
42
  channelId: string,
43
  message?: string,
44
  approver?: string,
45
  defaultArgsJson?: DefaultArgs,
46
  dynamicEnumsJson?: Enums,
47
  resumeButtonText?: string,
48
  cancelButtonText?: string,
49
) {
50
  await wmill.requestInteractiveSlackApproval({
51
    slackResourcePath,
52
    channelId,
53
    message,
54
    approver,
55
    defaultArgsJson,
56
    dynamicEnumsJson,
57
    resumeButtonText,
58
    cancelButtonText,
59
  })
60

61
  const returnObject: ResApproval = {}
62

63
  if (dynamicEnumsJson) returnObject.enums = dynamicEnumsJson
64
  if (defaultArgsJson) returnObject.default_args = defaultArgsJson
65

66
  return returnObject
67
}