Skip to content

Commit

Permalink
Better user responses when adding incidents
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinan-96 committed Oct 11, 2024
1 parent 1df12e2 commit 2c4f2cf
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ const app = new App({
}),
});

const myStore = {};

// Function to store data with a TTL
function storeWithTTL(key, value, ttlInMilliseconds) {
myStore[key] = value;

// Set a timeout to delete the entry after the TTL
setTimeout(() => {
delete myStore[key];
}, ttlInMilliseconds);
}

app.command("/addcategory", async ({ command, ack, respond }) => {
await ack(); // Acknowledge the command

Expand Down Expand Up @@ -59,6 +71,9 @@ app.event("message", async ({ event, client }) => {
// Ignore everything except new messages
return;
}
const uuid = crypto.randomUUID();
storeWithTTL(event.text, 86400000); // Store the text with the UUID as the key for a day

console.log("Posting an ephemeral message to user:", event.user, "in channel:", event.channel);
try {
// Respond with an ephemeral message containing a dropdown menu
Expand All @@ -75,7 +90,7 @@ app.event("message", async ({ event, client }) => {
},
accessory: {
type: "external_select",
action_id: `category_select-${event.ts}`,
action_id: uuid,
placeholder: {
type: "plain_text",
text: "Select a category",
Expand Down Expand Up @@ -130,22 +145,34 @@ app.options(/category_select-.*/, async ({ options, ack }) => {
});

// Listen for the interaction from the dropdown menu
app.action(/category_select-.*/, async ({ body, ack, say }) => {
app.action(/category_select-.*/, async ({ body, ack, client }) => {
// Acknowledge the action
await ack();
console.log("body", body);
// const text = atob(body.actions[0].action_id.split("-")[1]); Currently not used since it caused bugs, so text is just an empty string
const text = "";
console.log("text", text);
const uuid = body.actions[0].action_id; // Extract the UUID from the action ID
const originalText = myStore[uuid] ?? ''; // Retrieve the original text
console.log("text", originalText);
// Respond to the user's selection
const selectedCategory = body.actions[0].selected_option.text.text;
const dropdown_id = body.actions[0].action_id;
try {
addOrUpdateInc(body.user.username, text, selectedCategory, dropdown_id);
await say(`You selected: ${selectedCategory}`);
// Add or update the incident
await addOrUpdateInc(body.user.username, originalText, selectedCategory, dropdown_id);

// Send an ephemeral message with a checkmark emoji
await client.chat.postEphemeral({
channel: body.channel.id, // Send it in the same channel
user: body.user.id, // Send it to the user who made the selection
text: `✅ You selected: ${selectedCategory}`, // Message with checkmark
});
} catch (error) {
say("There was an error adding the incident. Please try again later.");
console.error("Error adding incident:", error);
// Handle any errors
await client.chat.postEphemeral({
channel: body.channel.id,
user: body.user.id,
text: "❌ There was an error adding the incident. Please try again later.",
});
console.error("Error adding incident:", error);
}

});
Expand Down

0 comments on commit 2c4f2cf

Please sign in to comment.