-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4123 from kishanprmr/fix/slack-pagination
fix(slack): Resolve Channels/Users Option When It's More Than 1000 Using Pagination
- Loading branch information
Showing
2 changed files
with
112 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"name": "@activepieces/piece-slack", | ||
"version": "0.3.17" | ||
"version": "0.3.18" | ||
} |
202 changes: 111 additions & 91 deletions
202
packages/pieces/community/slack/src/lib/common/props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
}; |