Files
leadchat/lib/integrations/slack/outgoing_message_builder.rb
Subin T P ed1c871633 Feature: Slack integration (#783)
- Integrations architecture
- Slack integration
2020-06-12 23:12:47 +05:30

51 lines
938 B
Ruby

class Integrations::Slack::OutgoingMessageBuilder
attr_reader :hook, :message
def self.perform(hook, message)
new(hook, message).perform
end
def initialize(hook, message)
@hook = hook
@message = message
end
def perform
send_message
update_reference_id
end
private
def conversation
@conversation ||= message.conversation
end
def contact
@contact ||= conversation.contact
end
def send_message
@slack_message = slack_client.chat_postMessage(
channel: hook.reference_id,
text: message.content,
username: contact.try(:name),
thread_ts: conversation.identifier
)
end
def update_reference_id
return if conversation.identifier
conversation.identifier = @slack_message['ts']
conversation.save!
end
def slack_client
Slack.configure do |config|
config.token = hook.access_token
end
Slack::Web::Client.new
end
end