-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsample_function.ts
More file actions
78 lines (71 loc) · 2.29 KB
/
sample_function.ts
File metadata and controls
78 lines (71 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
import type SampleObjectDatastore from "../datastores/sample_datastore.ts";
/**
* Functions are reusable building blocks of automation that accept
* inputs, perform calculations, and provide outputs. Functions can
* be used independently or as steps in workflows.
* https://api.slack.com/automation/functions/custom
*/
export const SampleFunctionDefinition = DefineFunction({
callback_id: "sample_function",
title: "Sample function",
description: "A sample function",
source_file: "functions/sample_function.ts",
input_parameters: {
properties: {
message: {
type: Schema.types.string,
description: "Message to be posted",
},
user: {
type: Schema.slack.types.user_id,
description: "The user invoking the workflow",
},
},
required: ["message", "user"],
},
output_parameters: {
properties: {
updatedMsg: {
type: Schema.types.string,
description: "Updated message to be posted",
},
},
required: ["updatedMsg"],
},
});
/**
* SlackFunction takes in two arguments: the CustomFunction
* definition (see above), as well as a function that contains
* handler logic that's run when the function is executed.
* https://api.slack.com/automation/functions/custom
*/
export default SlackFunction(
SampleFunctionDefinition,
async ({ inputs, client }) => {
const uuid = crypto.randomUUID();
// inputs.user is set from the interactivity_context defined in sample_trigger.ts
// https://api.slack.com/automation/forms#add-interactivity
const updatedMsg =
`:wave: <@${inputs.user}> submitted the following message: \n\n>${inputs.message}`;
const sampleObject = {
original_msg: inputs.message,
updated_msg: updatedMsg,
object_id: uuid,
};
// Save the sample object to the datastore
// https://api.slack.com/automation/datastores
const putResponse = await client.apps.datastore.put<
typeof SampleObjectDatastore.definition
>({
datastore: "SampleObjects",
item: sampleObject,
});
if (!putResponse.ok) {
return {
error: `Failed to put item into the datastore: ${putResponse.error}`,
};
}
return { outputs: { updatedMsg } };
},
);