Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slack): iterate all pages of slack channels / users #4123

Merged
merged 3 commits into from Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
};
};