2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-10-15 09:47:59 +00:00
|
|
|
require 'builder'
|
|
|
|
|
|
|
|
class Integration::PlacetelController < ApplicationController
|
|
|
|
skip_before_action :verify_csrf_token
|
|
|
|
before_action :check_configured, :check_token
|
|
|
|
|
|
|
|
# notify about inbound call / block inbound call
|
|
|
|
def event
|
|
|
|
|
|
|
|
local_params = ActiveSupport::HashWithIndifferentAccess.new(params.permit!.to_h)
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
cti = Cti::Driver::Placetel.new(params: local_params, config: config_integration)
|
2018-10-15 09:47:59 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
result = cti.process
|
2018-10-30 16:00:33 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
# check if inbound call should get rejected
|
|
|
|
if result[:action] == 'reject'
|
|
|
|
response_reject(result)
|
|
|
|
return true
|
2018-10-15 09:47:59 +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'
|
|
|
|
response_set_caller_id(result)
|
|
|
|
return true
|
2018-10-15 09:47:59 +00:00
|
|
|
end
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
if result[:action] == 'invalid_direction'
|
2022-01-18 12:52:57 +00:00
|
|
|
response_error(__("Invalid 'direction'!"))
|
2018-10-15 09:47:59 +00:00
|
|
|
return true
|
|
|
|
end
|
2019-07-09 22:07:32 +00:00
|
|
|
|
|
|
|
response_ok(response)
|
|
|
|
true
|
2018-10-15 09:47:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_token
|
|
|
|
if Setting.get('placetel_token') != params[:token]
|
2021-11-15 15:58:19 +00:00
|
|
|
response_unauthorized(__('Invalid token, please contact your admin!'))
|
2018-10-15 09:47:59 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_configured
|
|
|
|
http_log_config facility: 'placetel'
|
|
|
|
|
|
|
|
if !Setting.get('placetel_integration')
|
2021-12-15 21:00:53 +00:00
|
|
|
response_error(__('Feature is disabled, please contact your administrator!'))
|
2018-10-15 09:47:59 +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-10-15 09:47:59 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_error(error, code)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response() do
|
|
|
|
xml.Error(error)
|
|
|
|
end
|
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;', status: code
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_integration
|
|
|
|
@config_integration ||= Setting.get('placetel_config')
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_error(error)
|
|
|
|
xml_error(error, 422)
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_unauthorized(error)
|
|
|
|
xml_error(error, 401)
|
|
|
|
end
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
def response_reject(_result)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response() do
|
|
|
|
xml.Reject({ reason: 'busy' })
|
|
|
|
end
|
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;'
|
2018-10-30 16:00:33 +00:00
|
|
|
end
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
def response_set_caller_id(result)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response() do
|
|
|
|
xml.Dial(callerId: result[:params][:from_caller_id]) { xml.Number(result[:params][:to_caller_id]) }
|
2018-10-30 16:00:33 +00:00
|
|
|
end
|
2019-07-09 22:07:32 +00:00
|
|
|
send_data(content, type: 'application/xml; charset=UTF-8;')
|
|
|
|
end
|
2018-10-30 16:00:33 +00:00
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
def response_ok(_result)
|
|
|
|
xml = Builder::XmlMarkup.new(indent: 2)
|
|
|
|
xml.instruct!
|
|
|
|
content = xml.Response()
|
|
|
|
send_data content, type: 'application/xml; charset=UTF-8;'
|
2018-10-30 16:00:33 +00:00
|
|
|
end
|
2019-07-09 22:07:32 +00:00
|
|
|
|
2018-10-15 09:47:59 +00:00
|
|
|
end
|