2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2018-05-28 23:45:29 +00:00
|
|
|
|
|
|
|
class Integration::CtiController < ApplicationController
|
|
|
|
skip_before_action :verify_csrf_token
|
|
|
|
before_action :check_configured, :check_token
|
|
|
|
|
|
|
|
# notify about inbound call / block inbound call
|
|
|
|
def event
|
2019-07-09 22:07:32 +00:00
|
|
|
local_params = ActiveSupport::HashWithIndifferentAccess.new(params.permit!.to_h)
|
|
|
|
|
|
|
|
cti = Cti::Driver::Cti.new(params: local_params, config: config_integration)
|
|
|
|
|
|
|
|
result = cti.process
|
|
|
|
|
|
|
|
# check if inbound call should get rejected
|
|
|
|
if result[:action] == 'reject'
|
|
|
|
response_ok(action: 'reject', reason: 'busy')
|
2018-05-28 23:45:29 +00:00
|
|
|
return true
|
2019-07-09 22:07:32 +00:00
|
|
|
end
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# check if outbound call changes the outbound caller_id
|
2019-07-09 22:07:32 +00:00
|
|
|
if result[:action] == 'set_caller_id'
|
|
|
|
data = {
|
|
|
|
action: 'dial',
|
|
|
|
caller_id: result[:params][:from_caller_id],
|
|
|
|
number: result[:params][:to_caller_id],
|
|
|
|
}
|
|
|
|
response_ok(data)
|
2018-05-28 23:45:29 +00:00
|
|
|
return true
|
|
|
|
end
|
2019-07-09 22:07:32 +00:00
|
|
|
|
|
|
|
if result[:action] == 'invalid_direction'
|
2021-11-15 15:58:19 +00:00
|
|
|
response_error(__('Invalid direction!'))
|
2019-07-09 22:07:32 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
response_ok({})
|
2018-05-28 23:45:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_token
|
|
|
|
if Setting.get('cti_token') != params[:token]
|
2021-11-15 15:58:19 +00:00
|
|
|
response_unauthorized(__('Invalid token, please contact your admin!'))
|
2018-05-28 23:45:29 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_configured
|
|
|
|
http_log_config facility: 'cti'
|
|
|
|
|
|
|
|
if !Setting.get('cti_integration')
|
2021-11-15 15:58:19 +00:00
|
|
|
response_error(__('Feature is disable, please contact your admin to enable it!'))
|
2018-05-28 23:45:29 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
|
2021-11-15 15:58:19 +00:00
|
|
|
response_error(__('Feature not configured, please contact your admin!'))
|
2018-05-28 23:45:29 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_integration
|
|
|
|
@config_integration ||= Setting.get('cti_config')
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_error(error)
|
|
|
|
render json: { error: error }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_unauthorized(error)
|
|
|
|
render json: { error: error }, status: :unauthorized
|
|
|
|
end
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
def response_ok(data)
|
|
|
|
render json: data, status: :ok
|
|
|
|
end
|
|
|
|
|
2018-05-28 23:45:29 +00:00
|
|
|
end
|