Chatwoot/app/services/automation_rules/action_service.rb

105 lines
2.6 KiB
Ruby
Raw Normal View History

2022-01-10 07:11:59 +00:00
class AutomationRules::ActionService
2022-03-21 07:42:27 +00:00
def initialize(rule, account, conversation)
2022-01-10 07:11:59 +00:00
@rule = rule
2022-03-21 07:42:27 +00:00
@account = account
2022-01-10 07:11:59 +00:00
@conversation = conversation
2022-03-21 07:42:27 +00:00
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
Sentry.capture_exception(e)
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 unless @rule.files.attached?
blob = ActiveStorage::Blob.find(blob_ids)
return if blob.blank?
params = { content: nil, private: false, attachments: blob }
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_email_transcript(emails)
emails.each do |email|
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, email)&.deliver_later
end
2022-03-21 07:42:27 +00:00
end
def mute_conversation(_params)
@conversation.mute!
end
2022-03-29 07:57:16 +00:00
def snooze_conversation(_params)
2022-04-12 14:53:34 +00:00
@conversation.snoozed!
2022-03-29 07:57:16 +00:00
end
def resolve_conversation(_params)
@conversation.resolved!
end
2022-03-21 07:42:27 +00:00
def change_status(status)
@conversation.update!(status: status[0])
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)
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 assign_team(team_ids = [])
return unless team_belongs_to_account?(team_ids)
@conversation.update!(team_id: team_ids[0])
end
2022-03-21 07:42:27 +00:00
def assign_best_agent(agent_ids = [])
2022-01-10 07:11:59 +00:00
return unless agent_belongs_to_account?(agent_ids)
@agent = @account.users.find_by(id: agent_ids)
2022-03-21 07:42:27 +00:00
@conversation.update!(assignee_id: @agent.id) if @agent.present?
2022-01-10 07:11:59 +00:00
end
2022-03-21 07:42:27 +00:00
def add_label(labels)
return if labels.empty?
2022-01-10 07:11:59 +00:00
@conversation.add_labels(labels)
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
def agent_belongs_to_account?(agent_ids)
@account.agents.pluck(:id).include?(agent_ids[0])
end
def team_belongs_to_account?(team_ids)
@account.team_ids.include?(team_ids[0])
end
end