Skip to content

Commit a79e329

Browse files
authored
Scripts to list (non-functional) and delete slack messages (#650)
1 parent 2dfe417 commit a79e329

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"scripts:removeTestDatabases": "lerna run --scope='scripts' removeTestDatabases -- ",
2525
"scripts:materializeScrubbedMessagesStats:all": "lerna run --scope='scripts' materializeScrubbedMessagesStats:all",
2626
"scripts:materializeScrubbedMessagesStats:latest": "lerna run --scope='scripts' materializeScrubbedMessagesStats:latest",
27+
"scripts:listSlackMessages": "lerna run --scope='scripts' listSlackMessages -- ",
28+
"scripts:removeSlackMessage": "lerna run --scope='scripts' removeSlackMessage -- ",
2729
"server:start": "lerna run start --scope='chatbot-server-mongodb-public'",
2830
"eval:conversationQualityCheckPipeline": "lerna run pipeline:conversationQualityCheck --scope='chatbot-eval-mongodb-public'",
2931
"eval:faqConversationQualityCheckPipeline": "lerna run pipeline:faqConversationQualityCheck --scope='chatbot-eval-mongodb-public'"

packages/scripts/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"removeTestDatabases": "npm run build && node ./build/removeTestDatabases.js",
2525
"getConversationText": "npm run build && node ./build/getConversationText.js",
2626
"findPageTitles": "npm run build && node ./build/main/findPageTitlesMain.js",
27+
"listSlackMessages": "npm run build && node ./build/main/listSlackMessagesMain.js",
28+
"removeSlackMessage": "npm run build && node ./build/main/removeSlackMessageMain.js",
2729
"test": "jest --forceExit",
2830
"build": "npm run clean && tsc -b tsconfig.build.json",
2931
"postbuild": "cp ./src/MessageAnalysis.d.ts ./build/",
@@ -38,6 +40,7 @@
3840
"dependencies": {
3941
"@cdxoo/dbscan": "^1.1.1",
4042
"@release-it/bumper": "^5.1.0",
43+
"@slack/web-api": "^7.8.0",
4144
"@stdlib/random-sample": "^0.2.1",
4245
"csv": "^6.3.1",
4346
"dotenv": "^16.3.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import dotenv from "dotenv";
2+
import { WebClient } from "@slack/web-api";
3+
import path from "path";
4+
5+
// Load environment variables from .env file
6+
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
7+
8+
/**
9+
Lists the last 25 messages from a Slack channel
10+
11+
@param channelId - The ID of the Slack channel to fetch messages from
12+
*/
13+
export async function listSlackMessages(channelId: string): Promise<void> {
14+
// Check if SLACK_BOT_TOKEN is available in environment variables
15+
const slackToken = process.env.SLACK_BOT_TOKEN;
16+
if (!slackToken) {
17+
console.error("Error: SLACK_BOT_TOKEN environment variable is not set");
18+
console.error("Please add it to your .env file");
19+
process.exit(1);
20+
}
21+
22+
try {
23+
// Initialize the Slack Web API client
24+
const client = new WebClient(slackToken);
25+
26+
console.log(`Fetching the last 25 messages from channel: ${channelId}`);
27+
28+
// Call the conversations.history method
29+
const result = await client.conversations.history({
30+
channel: channelId,
31+
limit: 25,
32+
});
33+
34+
if (!result.messages || result.messages.length === 0) {
35+
console.log("No messages found in this channel");
36+
return;
37+
}
38+
39+
// Display the messages
40+
console.log(`Found ${result.messages.length} messages:`);
41+
console.log("-----------------------------------");
42+
43+
result.messages.forEach((message, index) => {
44+
const timestamp = new Date(Number(message.ts) * 1000).toLocaleString();
45+
console.log(`[${index + 1}] ${timestamp}`);
46+
console.log(`User: ${message.user || "Unknown"}`);
47+
console.log(`Text: ${message.text || "No text content"}`);
48+
console.log("-----------------------------------");
49+
});
50+
} catch (error) {
51+
console.error("Error fetching messages:", error);
52+
process.exit(1);
53+
}
54+
}
55+
56+
// Main function to run when script is executed directly
57+
if (require.main === module) {
58+
// Get channel ID from command line arguments
59+
const args = process.argv.slice(2);
60+
if (args.length !== 1) {
61+
console.error("Usage: npm run listSlackMessages -- <channel_id>");
62+
process.exit(1);
63+
}
64+
65+
console.info(
66+
`NOTE: You can find a specific message ID (i.e. its \`ts\` value) by opening a channel in the Slack Web UI and inspecting the message in dev tools. For example, id="1741289525.588509".\n`
67+
);
68+
69+
const channelId = args[0];
70+
listSlackMessages(channelId).catch(console.error);
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import dotenv from "dotenv";
2+
import { WebClient } from "@slack/web-api";
3+
import path from "path";
4+
5+
// Load environment variables from .env file
6+
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
7+
8+
/**
9+
Removes a message from a Slack channel
10+
11+
@param channelId - The ID of the Slack channel
12+
@param messageTs - The timestamp of the message to remove
13+
*/
14+
export async function removeSlackMessage(
15+
channelId: string,
16+
messageTs: string
17+
): Promise<void> {
18+
// Check if SLACK_BOT_TOKEN is available in environment variables
19+
const slackToken = process.env.SLACK_BOT_TOKEN;
20+
if (!slackToken) {
21+
console.error("Error: SLACK_BOT_TOKEN environment variable is not set");
22+
console.error("Please add it to your .env file");
23+
process.exit(1);
24+
}
25+
26+
try {
27+
// Initialize the Slack Web API client
28+
const client = new WebClient(slackToken);
29+
30+
console.log(
31+
`Attempting to delete message with timestamp ${messageTs} from channel ${channelId}`
32+
);
33+
34+
// Call the chat.delete method to remove the message
35+
const result = await client.chat.delete({
36+
channel: channelId,
37+
ts: messageTs,
38+
as_user: true, // Delete as the authenticated user
39+
});
40+
41+
if (result.ok) {
42+
console.log("Message successfully deleted");
43+
} else {
44+
console.error("Failed to delete message:", result.error);
45+
}
46+
} catch (error) {
47+
console.error("Error deleting message:", error);
48+
process.exit(1);
49+
}
50+
}
51+
52+
// Main function to run when script is executed directly
53+
if (require.main === module) {
54+
// Get channel ID and message timestamp from command line arguments
55+
const args = process.argv.slice(2);
56+
if (args.length !== 2) {
57+
console.error(
58+
"Usage: npm run removeSlackMessage -- <channel_id> <message_timestamp>"
59+
);
60+
console.error(
61+
"Example: npm run removeSlackMessage -- C01234ABCDE 1623456789.123456"
62+
);
63+
process.exit(1);
64+
}
65+
66+
const [channelId, messageTs] = args;
67+
removeSlackMessage(channelId, messageTs).catch(console.error);
68+
}

0 commit comments

Comments
 (0)