@@ -3,9 +3,6 @@ import { SlackAPI } from "deno-slack-api/mod.ts";
3
3
import { Logger } from "../utils/logger.ts" ;
4
4
import { FunctionSourceFile } from "../utils/function_source_file.ts" ;
5
5
6
- /**
7
- * See https://api.slack.com/future/functions/custom
8
- */
9
6
export const def = DefineFunction ( {
10
7
callback_id : "manage-reaction-added-event-trigger" ,
11
8
title : "Manage a reaction_added event trigger" ,
@@ -28,12 +25,11 @@ export default SlackFunction(def, async ({
28
25
env,
29
26
token,
30
27
} ) => {
31
- const logger = Logger ( env . logLevel ) ;
32
- logger . debug ( inputs ) ;
28
+ const logger = Logger ( env . LOG_LEVEL ) ;
33
29
34
30
const client = SlackAPI ( token ) ;
31
+ // Check the existing triggers for this workflow
35
32
const allTriggers = await client . workflows . triggers . list ( { } ) ;
36
- logger . info ( allTriggers ) ;
37
33
let triggerToUpdate = undefined ;
38
34
// find the trigger to update
39
35
if ( allTriggers . triggers ) {
@@ -46,9 +42,13 @@ export default SlackFunction(def, async ({
46
42
}
47
43
}
48
44
}
45
+ logger . info ( `triggerToUpdate: ${ JSON . stringify ( triggerToUpdate ) } ` ) ;
46
+
49
47
const channelIds = triggerToUpdate ?. channel_ids != undefined
50
48
? triggerToUpdate . channel_ids
51
49
: [ ] ;
50
+
51
+ // Open the modal to configure the channel list to enable this workflow
52
52
await client . views . open ( {
53
53
interactivity_pointer : inputs . interactivity . interactivity_pointer ,
54
54
view : {
@@ -91,19 +91,13 @@ export default SlackFunction(def, async ({
91
91
. addViewSubmissionHandler (
92
92
[ "configure-workflow" ] ,
93
93
async ( { view, inputs, env, token } ) => {
94
- const logger = Logger ( env . logLevel ) ;
94
+ const logger = Logger ( env . LOG_LEVEL ) ;
95
95
const { workflowCallbackId } = inputs ;
96
96
const channelIds = view . state . values . block . channels . selected_channels ;
97
97
const triggerInputs = {
98
- channelId : {
99
- value : "{{data.channel_id}}" ,
100
- } ,
101
- messageTs : {
102
- value : "{{data.message_ts}}" ,
103
- } ,
104
- reaction : {
105
- value : "{{data.reaction}}" ,
106
- } ,
98
+ channelId : { value : "{{data.channel_id}}" } ,
99
+ messageTs : { value : "{{data.message_ts}}" } ,
100
+ reaction : { value : "{{data.reaction}}" } ,
107
101
} ;
108
102
109
103
const client = SlackAPI ( token ) ;
@@ -125,6 +119,7 @@ export default SlackFunction(def, async ({
125
119
}
126
120
127
121
if ( triggerToUpdate === undefined ) {
122
+ // Create a new trigger
128
123
const creation = await client . workflows . triggers . create ( {
129
124
type : "event" ,
130
125
name : "reaction_added event trigger" ,
@@ -137,6 +132,7 @@ export default SlackFunction(def, async ({
137
132
} ) ;
138
133
logger . info ( `A new trigger created: ${ JSON . stringify ( creation ) } ` ) ;
139
134
} else {
135
+ // Update the existing trigger
140
136
const update = await client . workflows . triggers . update ( {
141
137
trigger_id : triggerToUpdate . id ,
142
138
type : "event" ,
@@ -150,6 +146,9 @@ export default SlackFunction(def, async ({
150
146
} ) ;
151
147
logger . info ( `A new trigger updated: ${ JSON . stringify ( update ) } ` ) ;
152
148
}
149
+
150
+ // This app's bot user joins all the channels
151
+ // to perform API calls for the channels
153
152
for ( const channelId of channelIds ) {
154
153
const joinResult = await client . conversations . join ( {
155
154
channel : channelId ,
@@ -191,8 +190,8 @@ export default SlackFunction(def, async ({
191
190
. addViewClosedHandler (
192
191
[ "configure-workflow" ] ,
193
192
( { view, env } ) => {
194
- const logger = Logger ( env . logLevel ) ;
195
- logger . debug ( JSON . stringify ( view , null , 2 ) ) ;
193
+ const logger = Logger ( env . LOG_LEVEL ) ;
194
+ logger . debug ( JSON . stringify ( view ) ) ;
196
195
return {
197
196
outputs : { } ,
198
197
completed : true ,
0 commit comments