Files
chatwoot/lib/integrations/slack/channel_builder.rb
Muhsin Keloth 5487d4c615 fix: Include private channels in Slack integration pagination (#11751)
Fixes
https://linear.app/chatwoot/issue/CW-4507/slack-integration-not-showing-private-channels
  ## Problem

When the Slack workspace has many channels (requiring multiple API
requests to fetch all of them), our system was only looking for private
channels in the first batch of results. All subsequent batches were
missing the instruction to include private channels, so they only
returned public channels.

  ## Root Cause
- Initial API call correctly specified `types:
'public_channel,private_channel'`
- Pagination loop only passed `cursor` parameter, omitting `types` and
`exclude_archived`
  - Subsequent pages defaulted to public channels only

  ## Changes
- Fixed parameter formatting in `types` (removed space:
`'public_channel, private_channel'` →
`'public_channel,private_channel'`)
- Added missing `types` and `exclude_archived` parameters to paginated
`conversations_list` calls

---------

Co-authored-by: Sojan Jose <sojan@pepalo.com>
2025-06-17 16:44:56 -07:00

53 lines
1.3 KiB
Ruby

class Integrations::Slack::ChannelBuilder
attr_reader :params, :channel
def initialize(params)
@params = params
end
def fetch_channels
channels
end
def update(reference_id)
update_reference_id(reference_id)
end
private
def hook
@hook ||= params[:hook]
end
def slack_client
@slack_client ||= Slack::Web::Client.new(token: hook.access_token)
end
def channels
conversations_list = slack_client.conversations_list(types: 'public_channel,private_channel', exclude_archived: true)
channel_list = conversations_list.channels
while conversations_list.response_metadata.next_cursor.present?
conversations_list = slack_client.conversations_list(
cursor: conversations_list.response_metadata.next_cursor,
types: 'public_channel,private_channel',
exclude_archived: true
)
channel_list.concat(conversations_list.channels)
end
channel_list
end
def find_channel(reference_id)
channels.find { |channel| channel['id'] == reference_id }
end
def update_reference_id(reference_id)
channel = find_channel(reference_id)
return if channel.blank?
slack_client.conversations_join(channel: channel[:id]) if channel[:is_private] == false
@hook.update!(reference_id: channel[:id], settings: { channel_name: channel[:name] }, status: 'enabled')
@hook
end
end