Skip to content

Commit

Permalink
Merge pull request #4123 from kishanprmr/fix/slack-pagination
Browse files Browse the repository at this point in the history
fix(slack): Resolve Channels/Users Option When It's More Than 1000 Using Pagination
  • Loading branch information
abuaboud committed Mar 7, 2024
2 parents cba84ba + 636fac9 commit bd667f9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 92 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/community/slack/package.json
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-slack",
"version": "0.3.17"
"version": "0.3.18"
}
202 changes: 111 additions & 91 deletions packages/pieces/community/slack/src/lib/common/props.ts
@@ -1,122 +1,142 @@
import { OAuth2PropertyValue, Property } from '@activepieces/pieces-framework';
import {
AuthenticationType,
httpClient,
HttpMethod,
HttpRequest,
AuthenticationType,
httpClient,
HttpMethod,
HttpRequest,
} from '@activepieces/pieces-common';

export const slackChannel = Property.Dropdown({
displayName: 'Channel',
description: 'Channel, private group, or IM channel to send message to.',
required: true,
refreshers: [],
async options({ auth }) {
if (!auth) {
return {
disabled: true,
placeholder: 'connect slack account',
options: [],
};
}
const authentication = auth as OAuth2PropertyValue;
const accessToken = authentication['access_token'];
const request: HttpRequest = {
method: HttpMethod.GET,
url: `https://slack.com/api/conversations.list?types=public_channel,private_channel&limit=1000`,
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
};
const response = await httpClient.sendRequest<{
channels: { id: string; name: string }[];
}>(request);
return {
disabled: false,
placeholder: 'Select channel',
options: response.body.channels.map((ch) => {
return {
label: ch.name,
value: ch.id,
};
}),
};
},
displayName: 'Channel',
description: 'Channel, private group, or IM channel to send message to.',
required: true,
refreshers: [],
async options({ auth }) {
if (!auth) {
return {
disabled: true,
placeholder: 'connect slack account',
options: [],
};
}
const authentication = auth as OAuth2PropertyValue;
const accessToken = authentication['access_token'];
const channels: { label: string; value: string }[] = [];
let cursor;
do {
const request: HttpRequest = {
method: HttpMethod.GET,
url: 'https://slack.com/api/conversations.list',
queryParams: {
types: 'public_channel,private_channel',
limit: '200',
cursor: cursor ?? '',
},

authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
};
const response = await httpClient.sendRequest<{
channels: { id: string; name: string }[];
response_metadata: { next_cursor: string };
}>(request);
channels.push(
...response.body.channels.map((channel) => ({ label: channel.name, value: channel.id })),
);
cursor = response.body.response_metadata.next_cursor;
} while (cursor !== '');
return {
disabled: false,
placeholder: 'Select channel',
options: channels,
};
},
});

export const username = Property.ShortText({
displayName: 'Username',
description: 'The username of the bot',
required: false,
displayName: 'Username',
description: 'The username of the bot',
required: false,
});

export const profilePicture = Property.ShortText({
displayName: 'Profile Picture',
description: 'The profile picture of the bot',
required: false,
displayName: 'Profile Picture',
description: 'The profile picture of the bot',
required: false,
});

export const blocks = Property.Json({
displayName: 'Block Kit blocks',
description: 'See https://api.slack.com/block-kit for specs',
required: false,
displayName: 'Block Kit blocks',
description: 'See https://api.slack.com/block-kit for specs',
required: false,
});

export const userId = Property.Dropdown<string>({
displayName: 'User',
description: 'Message receiver',
required: true,
refreshers: [],
async options({ auth }) {
if (!auth) {
return {
disabled: true,
placeholder: 'connect slack account',
options: [],
};
}

const accessToken = (auth as OAuth2PropertyValue).access_token;
displayName: 'User',
description: 'Message receiver',
required: true,
refreshers: [],
async options({ auth }) {
if (!auth) {
return {
disabled: true,
placeholder: 'connect slack account',
options: [],
};
}

const request: HttpRequest = {
method: HttpMethod.GET,
url: 'https://slack.com/api/users.list',
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
};
const accessToken = (auth as OAuth2PropertyValue).access_token;
const users: { label: string; value: string }[] = [];

const response = await httpClient.sendRequest<UserListResponse>(request);
let cursor;
do {
const request: HttpRequest = {
method: HttpMethod.GET,
url: 'https://slack.com/api/users.list',
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
queryParams: {
limit: '200',
cursor: cursor ?? '',
},
};

const options = response.body.members.map((member) => ({
label: member.name,
value: member.id,
}));
const response = await httpClient.sendRequest<UserListResponse>(request);
users.push(
...response.body.members.map((member) => ({ label: member.name, value: member.id })),
);
cursor = response.body.response_metadata.next_cursor;
} while (cursor !== '');

return {
disabled: false,
placeholder: 'Select channel',
options,
};
},
return {
disabled: false,
placeholder: 'Select channel',
options: users,
};
},
});

export const text = Property.LongText({
displayName: 'Message',
description: 'The text of your message',
required: true,
displayName: 'Message',
description: 'The text of your message',
required: true,
});

export const actions = Property.Array({
displayName: 'Action Buttons',
required: true,
displayName: 'Action Buttons',
required: true,
});

type UserListResponse = {
members: {
id: string;
name: string;
}[];
members: {
id: string;
name: string;
}[];
response_metadata: {
next_cursor: string;
};
};

0 comments on commit bd667f9

Please sign in to comment.