import { WebClient as SlackWebClient } from "npm:@slack/web-api";
type Slack = {
token: string
}
// Needed scopes are: users:read ; users:read.email ; channels:read ; channels:history
export async function main(slackResource: Slack, companyDomain: string) {
// Initialize the Slack WebClient with the provided token
const slackClient = new SlackWebClient(slackResource.token);
try {
// Call the conversations.list method to get all channels
const listResult = await slackClient.conversations.list();
// Check if the listResult was successful and channels are present
if (listResult.ok && listResult.channels) {
// Initialize an array to hold the channels with their last message date and external member status
const channelsInfo = [];
// Iterate over each channel to fetch the last message's date, check for external members
for (const channel of listResult.channels) {
// Initialize external as false
let external = false;
// Call the conversations.members method to get all members of the channel
const membersResult = await slackClient.conversations.members({
channel: channel.id,
});
// Check if the membersResult was successful and members are present
if (membersResult.ok && membersResult.members) {
// Iterate over each member to fetch their user info
for (const memberId of membersResult.members) {
const userInfo = await slackClient.users.info({ user: memberId });
// Check if the userInfo was successful and user info is present
if (userInfo.ok && userInfo.user) {
if (userInfo.user.profile && userInfo.user.profile.email) {
// Check if the email domain is not windmill.dev
if (!userInfo.user.profile.email.endsWith(companyDomain)) {
external = true;
}
} else {
// Assume external if the email is not available
external = true;
}
}
}
}
// Call the conversations.history method to get the last message of the channel
const historyResult = await slackClient.conversations.history({
channel: channel.id,
limit: 1, // We only need the last message
});
// Initialize the lastMessageDate
let lastMessageDate = "No messages found";
// Check if the historyResult was successful and messages are present
if (
historyResult.ok && historyResult.messages &&
historyResult.messages.length > 0
) {
// Convert the timestamp to a Date object
const date = new Date(
parseFloat(historyResult.messages[0].ts) * 1000,
);
// Format the Date object to "YYYYMMDD"
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0"); // Pad with zero if needed
const day = date.getDate().toString().padStart(2, "0"); // Pad with zero if needed
lastMessageDate = `${year}${month}${day}`;
}
// Add the channel info to the array
channelsInfo.push({
id: channel.id,
name: channel.name,
lastMessageDate: lastMessageDate,
external: external
});
}
// Return the list of channels with the date of the last message, external member status
return channelsInfo;
} else {
// Return an error message if the listResult was not successful or channels are not present
return "Failed to retrieve channels or no channels found.";
}
} catch (error) {
// Return the error if the API call fails
return `Error retrieving channels: ${error}`;
}
}
Submitted by henri186 239 days ago