Chatwoot/app/services/automation_rules/action_service.rb

59 lines
1.6 KiB
Ruby
Raw Normal View History

class AutomationRules::ActionService < ActionService
2022-03-21 07:42:27 +00:00
def initialize(rule, account, conversation)
super(conversation)
2022-01-10 07:11:59 +00:00
@rule = rule
2022-03-21 07:42:27 +00:00
@account = account
Current.executed_by = rule
2022-01-10 07:11:59 +00:00
end
def perform
2022-03-21 07:42:27 +00:00
@rule.actions.each do |action|
2022-01-10 07:11:59 +00:00
action = action.with_indifferent_access
2022-03-21 07:42:27 +00:00
begin
send(action[:action_name], action[:action_params])
rescue StandardError => e
ChatwootExceptionTracker.new(e, account: @account).capture_exception
2022-03-21 07:42:27 +00:00
end
2022-01-10 07:11:59 +00:00
end
2022-03-21 07:42:27 +00:00
ensure
Current.reset
2022-01-10 07:11:59 +00:00
end
private
def send_attachment(blob_ids)
return if conversation_a_tweet?
return unless @rule.files.attached?
blobs = ActiveStorage::Blob.where(id: blob_ids)
return if blobs.blank?
params = { content: nil, private: false, attachments: blobs }
2022-03-30 02:38:58 +00:00
mb = Messages::MessageBuilder.new(nil, @conversation, params)
mb.perform
end
2022-03-29 07:57:16 +00:00
def send_webhook_event(webhook_url)
payload = @conversation.webhook_data.merge(event: "automation_event.#{@rule.event_name}")
2022-03-29 07:57:16 +00:00
WebhookJob.perform_later(webhook_url[0], payload)
2022-03-21 07:42:27 +00:00
end
2022-01-10 07:11:59 +00:00
def send_message(message)
return if conversation_a_tweet?
params = { content: message[0], private: false, content_attributes: { automation_rule_id: @rule.id } }
2022-03-30 02:38:58 +00:00
mb = Messages::MessageBuilder.new(nil, @conversation, params)
2022-03-21 07:42:27 +00:00
mb.perform
2022-01-10 07:11:59 +00:00
end
def send_email_to_team(params)
teams = Team.where(id: params[0][:team_ids])
teams.each do |team|
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[0][:message])&.deliver_now
2022-01-10 07:11:59 +00:00
end
end
end