2022-07-26 07:11:22 +00:00
|
|
|
class AutomationRules::ActionService < ActionService
|
2022-03-21 07:42:27 +00:00
|
|
|
def initialize(rule, account, conversation)
|
2022-07-26 07:11:22 +00:00
|
|
|
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
|
2022-05-09 08:53:19 +00:00
|
|
|
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
|
|
|
|
|
2022-04-24 06:32:40 +00:00
|
|
|
def send_attachment(blob_ids)
|
2022-04-27 14:40:40 +00:00
|
|
|
return if conversation_a_tweet?
|
|
|
|
|
2022-04-24 06:32:40 +00:00
|
|
|
return unless @rule.files.attached?
|
|
|
|
|
2022-10-21 02:41:48 +00:00
|
|
|
blobs = ActiveStorage::Blob.where(id: blob_ids)
|
2022-04-24 06:32:40 +00:00
|
|
|
|
2022-10-21 02:41:48 +00:00
|
|
|
return if blobs.blank?
|
2022-04-24 06:32:40 +00:00
|
|
|
|
2022-10-21 02:41:48 +00:00
|
|
|
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)
|
2022-04-24 06:32:40 +00:00
|
|
|
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)
|
2022-04-27 14:40:40 +00:00
|
|
|
return if conversation_a_tweet?
|
|
|
|
|
2022-04-21 15:22:23 +00:00
|
|
|
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)
|
2022-04-14 08:06:55 +00:00
|
|
|
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
|