@@ -20,6 +20,18 @@ const app = new App({
20
20
} ) ,
21
21
} ) ;
22
22
23
+ const myStore = { } ;
24
+
25
+ // Function to store data with a TTL
26
+ function storeWithTTL ( key , value , ttlInMilliseconds ) {
27
+ myStore [ key ] = value ;
28
+
29
+ // Set a timeout to delete the entry after the TTL
30
+ setTimeout ( ( ) => {
31
+ delete myStore [ key ] ;
32
+ } , ttlInMilliseconds ) ;
33
+ }
34
+
23
35
app . command ( "/addcategory" , async ( { command, ack, respond } ) => {
24
36
await ack ( ) ; // Acknowledge the command
25
37
@@ -59,6 +71,9 @@ app.event("message", async ({ event, client }) => {
59
71
// Ignore everything except new messages
60
72
return ;
61
73
}
74
+ const uuid = crypto . randomUUID ( ) ;
75
+ storeWithTTL ( event . text , 86400000 ) ; // Store the text with the UUID as the key for a day
76
+
62
77
console . log ( "Posting an ephemeral message to user:" , event . user , "in channel:" , event . channel ) ;
63
78
try {
64
79
// Respond with an ephemeral message containing a dropdown menu
@@ -75,7 +90,7 @@ app.event("message", async ({ event, client }) => {
75
90
} ,
76
91
accessory : {
77
92
type : "external_select" ,
78
- action_id : `category_select- ${ event . ts } ` ,
93
+ action_id : uuid ,
79
94
placeholder : {
80
95
type : "plain_text" ,
81
96
text : "Select a category" ,
@@ -130,22 +145,34 @@ app.options(/category_select-.*/, async ({ options, ack }) => {
130
145
} ) ;
131
146
132
147
// Listen for the interaction from the dropdown menu
133
- app . action ( / c a t e g o r y _ s e l e c t - .* / , async ( { body, ack, say } ) => {
148
+ app . action ( / c a t e g o r y _ s e l e c t - .* / , async ( { body, ack, client } ) => {
134
149
// Acknowledge the action
135
150
await ack ( ) ;
136
151
console . log ( "body" , body ) ;
137
- // const text = atob( body.actions[0].action_id.split("-")[1]); Currently not used since it caused bugs, so text is just an empty string
138
- const text = "" ;
139
- console . log ( "text" , text ) ;
152
+ const uuid = body . actions [ 0 ] . action_id ; // Extract the UUID from the action ID
153
+ const originalText = myStore [ uuid ] ?? '' ; // Retrieve the original text
154
+ console . log ( "text" , originalText ) ;
140
155
// Respond to the user's selection
141
156
const selectedCategory = body . actions [ 0 ] . selected_option . text . text ;
142
157
const dropdown_id = body . actions [ 0 ] . action_id ;
143
158
try {
144
- addOrUpdateInc ( body . user . username , text , selectedCategory , dropdown_id ) ;
145
- await say ( `You selected: ${ selectedCategory } ` ) ;
159
+ // Add or update the incident
160
+ await addOrUpdateInc ( body . user . username , originalText , selectedCategory , dropdown_id ) ;
161
+
162
+ // Send an ephemeral message with a checkmark emoji
163
+ await client . chat . postEphemeral ( {
164
+ channel : body . channel . id , // Send it in the same channel
165
+ user : body . user . id , // Send it to the user who made the selection
166
+ text : `✅ You selected: ${ selectedCategory } ` , // Message with checkmark
167
+ } ) ;
146
168
} catch ( error ) {
147
- say ( "There was an error adding the incident. Please try again later." ) ;
148
- console . error ( "Error adding incident:" , error ) ;
169
+ // Handle any errors
170
+ await client . chat . postEphemeral ( {
171
+ channel : body . channel . id ,
172
+ user : body . user . id ,
173
+ text : "❌ There was an error adding the incident. Please try again later." ,
174
+ } ) ;
175
+ console . error ( "Error adding incident:" , error ) ;
149
176
}
150
177
151
178
} ) ;
0 commit comments