Feature: Slack integration (#783)
- Integrations architecture - Slack integration
This commit is contained in:
50
lib/integrations/slack/outgoing_message_builder.rb
Normal file
50
lib/integrations/slack/outgoing_message_builder.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
Reference in New Issue
Block a user