Skip to content

Commit 72c45d2

Browse files
committedOct 6, 2022
Update
1 parent f46c8ae commit 72c45d2

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ slack deploy
2929
slack trigger create --trigger-def triggers/setup.ts
3030
```
3131

32-
You will get a URL (e.g., `https://slack.com/shortcuts/Ft***/****`) to invoke the setup workflow. Once you can share the URL in your Slack workspace, any users in the workspace can enable the translator app in any public channels.
32+
You will get a URL (e.g., `https://slack.com/shortcuts/Ft***/****`) to invoke
33+
the setup workflow. Once you can share the URL in your Slack workspace, any
34+
users in the workspace can enable the translator app in any public channels.
3335

3436
### 2. Set DeepL API key to the app
3537

‎functions/maintain.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
2+
import { SlackAPI } from "deno-slack-api/mod.ts";
3+
import { Logger } from "../utils/logger.ts";
4+
import { FunctionSourceFile } from "../utils/function_source_file.ts";
5+
6+
export const def = DefineFunction({
7+
callback_id: "maintain-channel-memberships",
8+
title: "Maintain channel memberships for a trigger",
9+
source_file: FunctionSourceFile(import.meta.url),
10+
input_parameters: {
11+
properties: {
12+
workflowCallbackId: { type: Schema.types.string },
13+
},
14+
required: ["workflowCallbackId"],
15+
},
16+
output_parameters: {
17+
properties: {},
18+
required: [],
19+
},
20+
});
21+
22+
export default SlackFunction(def, async ({
23+
inputs,
24+
env,
25+
token,
26+
}) => {
27+
const logger = Logger(env.LOG_LEVEL);
28+
const client = SlackAPI(token);
29+
// Check the existing triggers for this workflow
30+
const allTriggers = await client.workflows.triggers.list({});
31+
let targetTrigger = undefined;
32+
// find the trigger to maintain
33+
if (allTriggers.triggers) {
34+
for (const trigger of allTriggers.triggers) {
35+
if (
36+
trigger.workflow.callback_id === inputs.workflowCallbackId &&
37+
trigger.event_type === "slack#/events/reaction_added"
38+
) {
39+
targetTrigger = trigger;
40+
}
41+
}
42+
}
43+
if (
44+
targetTrigger === undefined ||
45+
targetTrigger.channel_ids === undefined
46+
) {
47+
return { outputs: {} };
48+
}
49+
// This app's bot user joins all the channels
50+
// to perform API calls for the channels
51+
for (const channelId of targetTrigger.channel_ids) {
52+
const joinResult = await client.conversations.join({
53+
channel: channelId,
54+
});
55+
logger.debug(joinResult);
56+
}
57+
return { outputs: {} };
58+
});

‎manifest.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Manifest } from "deno-slack-sdk/mod.ts";
22
import reacjilator from "./workflows/reacjilator.ts";
33
import setup from "./workflows/setup.ts";
4+
import maintenance from "./workflows/maintenance.ts";
45

56
export default Manifest({
67
name: "DeepL for Slack (beta)",
@@ -9,6 +10,7 @@ export default Manifest({
910
workflows: [
1011
reacjilator,
1112
setup,
13+
maintenance,
1214
],
1315
outgoingDomains: [
1416
"api-free.deepl.com",

‎triggers/daily-maintenance.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Trigger } from "deno-slack-api/types.ts";
2+
import workflowDef from "../workflows/maintenance.ts";
3+
4+
const trigger: Trigger<typeof workflowDef.definition> = {
5+
type: "scheduled",
6+
name: "Trigger a scheduled maintenance job",
7+
workflow: `#/workflows/${workflowDef.definition.callback_id}`,
8+
inputs: {},
9+
schedule: {
10+
// TODO: adjust the start time to be a future date
11+
start_time: "2022-10-01T00:00:00Z",
12+
end_time: "2037-12-31T23:59:59Z",
13+
frequency: { type: "daily", repeats_every: 1 },
14+
},
15+
};
16+
17+
export default trigger;
File renamed without changes.

‎workflows/maintenance.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { DefineWorkflow } from "deno-slack-sdk/mod.ts";
2+
import { def as maintainDef } from "../functions/maintain.ts";
3+
import { default as reacjilatorDef } from "./reacjilator.ts";
4+
5+
/**
6+
* https://api.slack.com/future/workflows
7+
*/
8+
const workflow = DefineWorkflow({
9+
callback_id: "reacjilator-maintenance",
10+
title: "Maintain the configuration of the app (beta)",
11+
input_parameters: {
12+
properties: {},
13+
required: [],
14+
},
15+
});
16+
17+
workflow.addStep(maintainDef, {
18+
workflowCallbackId: reacjilatorDef.definition.callback_id,
19+
});
20+
21+
export default workflow;

0 commit comments

Comments
 (0)
Please sign in to comment.