mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 19:48:08 +00:00
feat: Update the slack integration-flow to allow users to select the channel (#7637)
This commit is contained in:
@@ -5,9 +5,12 @@ class Integrations::Slack::ChannelBuilder
|
||||
@params = params
|
||||
end
|
||||
|
||||
def perform
|
||||
find_or_create_channel
|
||||
update_reference_id
|
||||
def fetch_channels
|
||||
channels
|
||||
end
|
||||
|
||||
def update(reference_id)
|
||||
update_reference_id(reference_id)
|
||||
end
|
||||
|
||||
private
|
||||
@@ -20,19 +23,26 @@ class Integrations::Slack::ChannelBuilder
|
||||
@slack_client ||= Slack::Web::Client.new(token: hook.access_token)
|
||||
end
|
||||
|
||||
def find_or_create_channel
|
||||
current_list = slack_client.conversations_list
|
||||
channels = current_list.channels
|
||||
while current_list.response_metadata.next_cursor.present?
|
||||
current_list = slack_client.conversations_list(cursor: current_list.response_metadata.next_cursor)
|
||||
channels.concat(current_list.channels)
|
||||
def channels
|
||||
conversations_list = slack_client.conversations_list(types: 'public_channel, private_channel')
|
||||
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)
|
||||
channel_list.concat(conversations_list.channel_list)
|
||||
end
|
||||
existing_channel = channels.find { |channel| channel['name'] == params[:channel] }
|
||||
@channel = existing_channel || slack_client.conversations_create(name: params[:channel])['channel']
|
||||
channel_list
|
||||
end
|
||||
|
||||
def update_reference_id
|
||||
slack_client.conversations_join(channel: channel[:id])
|
||||
@hook.update(reference_id: channel[:id])
|
||||
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
|
||||
|
||||
@@ -10,7 +10,7 @@ class Integrations::Slack::HookBuilder
|
||||
|
||||
hook = account.hooks.new(
|
||||
access_token: token,
|
||||
status: 'enabled',
|
||||
status: 'disabled',
|
||||
inbox_id: params[:inbox_id],
|
||||
app_id: 'slack'
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user