fix: mentions are not rendered properly in slack (#6762)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
||||||
|
include RegexHelper
|
||||||
pattr_initialize [:message!, :hook!]
|
pattr_initialize [:message!, :hook!]
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
@@ -34,12 +35,16 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
|||||||
def message_content
|
def message_content
|
||||||
private_indicator = message.private? ? 'private: ' : ''
|
private_indicator = message.private? ? 'private: ' : ''
|
||||||
if conversation.identifier.present?
|
if conversation.identifier.present?
|
||||||
"#{private_indicator}#{message.content}"
|
"#{private_indicator}#{message_text}"
|
||||||
else
|
else
|
||||||
"#{formatted_inbox_name}#{email_subject_line}\n#{message.content}"
|
"#{formatted_inbox_name}#{email_subject_line}\n#{message_text}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def message_text
|
||||||
|
message.content.present? ? message.content.gsub(MENTION_REGEX, '\1') : message.content
|
||||||
|
end
|
||||||
|
|
||||||
def formatted_inbox_name
|
def formatted_inbox_name
|
||||||
"\n*Inbox:* #{message.inbox.name} (#{message.inbox.inbox_type})\n"
|
"\n*Inbox:* #{message.inbox.name} (#{message.inbox.inbox_type})\n"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -132,5 +132,46 @@ describe Integrations::Slack::SendOnSlackService do
|
|||||||
expect(hook).to have_received(:authorization_error!)
|
expect(hook).to have_received(:authorization_error!)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when message contains mentions' do
|
||||||
|
it 'sends formatted message to slack along with inbox name when identifier not present' do
|
||||||
|
inbox = conversation.inbox
|
||||||
|
message.update!(content: "Hi [@#{contact.name}](mention://user/#{contact.id}/#{contact.name}), welcome to Chatwoot!")
|
||||||
|
formatted_message_text = message.content.gsub(RegexHelper::MENTION_REGEX, '\1')
|
||||||
|
|
||||||
|
expect(slack_client).to receive(:chat_postMessage).with(
|
||||||
|
channel: hook.reference_id,
|
||||||
|
text: "\n*Inbox:* #{inbox.name} (#{inbox.inbox_type})\n\n#{formatted_message_text}",
|
||||||
|
username: "#{message.sender.name} (Contact)",
|
||||||
|
thread_ts: nil,
|
||||||
|
icon_url: anything
|
||||||
|
).and_return(slack_message)
|
||||||
|
|
||||||
|
builder.perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sends formatted message to slack when identifier is present' do
|
||||||
|
conversation.update!(identifier: 'random_slack_thread_ts')
|
||||||
|
message.update!(content: "Hi [@#{contact.name}](mention://user/#{contact.id}/#{contact.name}), welcome to Chatwoot!")
|
||||||
|
formatted_message_text = message.content.gsub(RegexHelper::MENTION_REGEX, '\1')
|
||||||
|
|
||||||
|
expect(slack_client).to receive(:chat_postMessage).with(
|
||||||
|
channel: hook.reference_id,
|
||||||
|
text: formatted_message_text,
|
||||||
|
username: "#{message.sender.name} (Contact)",
|
||||||
|
thread_ts: 'random_slack_thread_ts',
|
||||||
|
icon_url: anything
|
||||||
|
).and_return(slack_message)
|
||||||
|
|
||||||
|
builder.perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not throw error if message content is nil' do
|
||||||
|
message.update!(content: nil)
|
||||||
|
conversation.update!(identifier: 'random_slack_thread_ts')
|
||||||
|
|
||||||
|
expect { builder.perform }.not_to raise_error
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user