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