diff --git a/lib/integrations/slack/send_on_slack_service.rb b/lib/integrations/slack/send_on_slack_service.rb index fc7ffe5ab..c840dd1d9 100644 --- a/lib/integrations/slack/send_on_slack_service.rb +++ b/lib/integrations/slack/send_on_slack_service.rb @@ -44,7 +44,7 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService def message_content private_indicator = message.private? ? 'private: ' : '' - sanitized_content = ActionView::Base.full_sanitizer.sanitize(message_text) + sanitized_content = ActionView::Base.full_sanitizer.sanitize(format_message_content) if conversation.identifier.present? "#{private_indicator}#{sanitized_content}" @@ -53,6 +53,10 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService end end + def format_message_content + message.message_type == 'activity' ? "_#{message_text}_" : message_text + end + def message_text if message.content.present? message.content.gsub(MENTION_REGEX, '\1') @@ -79,7 +83,7 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService end def avatar_url(sender) - sender_type = sender.instance_of?(Contact) ? 'contact' : 'user' + sender_type = sender_type(sender).downcase blob_key = sender&.avatar&.attached? ? sender.avatar.blob.key : nil generate_url(sender_type, blob_key) end @@ -137,7 +141,15 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService end def sender_type(sender) - sender.instance_of?(Contact) ? 'Contact' : 'Agent' + if sender.instance_of?(Contact) + 'Contact' + elsif message.message_type == 'template' && sender.nil? + 'Bot' + elsif message.message_type == 'activity' && sender.nil? + 'System' + else + 'Agent' + end end def update_reference_id diff --git a/public/integrations/slack/bot.png b/public/integrations/slack/bot.png new file mode 100644 index 000000000..4b5a2d686 Binary files /dev/null and b/public/integrations/slack/bot.png differ diff --git a/public/integrations/slack/system.png b/public/integrations/slack/system.png new file mode 100644 index 000000000..c700e55e0 Binary files /dev/null and b/public/integrations/slack/system.png differ diff --git a/spec/lib/integrations/slack/send_on_slack_service_spec.rb b/spec/lib/integrations/slack/send_on_slack_service_spec.rb index aa0f37bde..3474cbfcd 100644 --- a/spec/lib/integrations/slack/send_on_slack_service_spec.rb +++ b/spec/lib/integrations/slack/send_on_slack_service_spec.rb @@ -183,11 +183,11 @@ describe Integrations::Slack::SendOnSlackService do it 'sent a template message on slack' do builder = described_class.new(message: template_message, hook: hook) allow(builder).to receive(:slack_client).and_return(slack_client) - + template_message.update!(sender: nil) expect(slack_client).to receive(:chat_postMessage).with( channel: hook.reference_id, text: template_message.content, - username: "#{template_message.sender.name} (Contact)", + username: 'Bot', thread_ts: conversation.identifier, icon_url: anything, unfurl_links: true @@ -198,6 +198,24 @@ describe Integrations::Slack::SendOnSlackService do expect(template_message.external_source_id_slack).to eq 'cw-origin-6789.12345' end + it 'sent a activity message on slack' do + template_message.update!(message_type: :activity) + template_message.update!(sender: nil) + builder = described_class.new(message: template_message, hook: hook) + allow(builder).to receive(:slack_client).and_return(slack_client) + expect(slack_client).to receive(:chat_postMessage).with( + channel: hook.reference_id, + text: "_#{template_message.content}_", + username: 'System', + thread_ts: conversation.identifier, + icon_url: anything, + unfurl_links: true + ).and_return(slack_message) + + builder.perform + expect(template_message.external_source_id_slack).to eq 'cw-origin-6789.12345' + end + it 'disables hook on Slack AccountInactive error' do expect(slack_client).to receive(:chat_postMessage).with( channel: hook.reference_id,