feat: modify getChannels method to support fetching channels by IDs from env

This commit is contained in:
5startaek
2025-04-04 23:46:42 +09:00
parent 704b9bf907
commit 3eee3e6872

View File

@@ -53,7 +53,7 @@ interface GetUserProfileArgs {
// Tool definitions // Tool definitions
const listChannelsTool: Tool = { const listChannelsTool: Tool = {
name: "slack_list_channels", name: "slack_list_channels",
description: "List public channels in the workspace with pagination", description: "List public or pre-defined channels in the workspace with pagination",
inputSchema: { inputSchema: {
type: "object", type: "object",
properties: { properties: {
@@ -221,23 +221,51 @@ class SlackClient {
} }
async getChannels(limit: number = 100, cursor?: string): Promise<any> { async getChannels(limit: number = 100, cursor?: string): Promise<any> {
const params = new URLSearchParams({ const predefinedChannelIds = process.env.SLACK_CHANNEL_IDS;
types: "public_channel", if (!predefinedChannelIds) {
exclude_archived: "true", const params = new URLSearchParams({
limit: Math.min(limit, 200).toString(), types: "public_channel",
team_id: process.env.SLACK_TEAM_ID!, exclude_archived: "true",
}); limit: Math.min(limit, 200).toString(),
team_id: process.env.SLACK_TEAM_ID!,
if (cursor) { });
params.append("cursor", cursor);
if (cursor) {
params.append("cursor", cursor);
}
const response = await fetch(
`https://slack.com/api/conversations.list?${params}`,
{ headers: this.botHeaders },
);
return response.json();
} }
const response = await fetch( const predefinedChannelIdsArray = predefinedChannelIds.split(",").map((id: string) => id.trim());
`https://slack.com/api/conversations.list?${params}`, const channels = [];
{ headers: this.botHeaders },
);
return response.json(); for (const channelId of predefinedChannelIdsArray) {
const params = new URLSearchParams({
channel: channelId,
});
const response = await fetch(
`https://slack.com/api/conversations.info?${params}`,
{ headers: this.botHeaders }
);
const data = await response.json();
if (data.ok && data.channel && !data.channel.is_archived) {
channels.push(data.channel);
}
}
return {
ok: true,
channels: channels,
response_metadata: { next_cursor: "" },
};
} }
async postMessage(channel_id: string, text: string): Promise<any> { async postMessage(channel_id: string, text: string): Promise<any> {