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
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
class Cti::Driver::Placetel < Cti::Driver::Base
|
|
|
|
|
|
|
|
def config
|
|
|
|
Setting.get('placetel_config')
|
|
|
|
end
|
|
|
|
|
|
|
|
def mapping(params)
|
|
|
|
|
|
|
|
# do event mapping
|
2020-07-13 12:46:08 +00:00
|
|
|
case params['event']
|
|
|
|
when 'IncomingCall'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['direction'] = 'in'
|
|
|
|
params['event'] = 'newCall'
|
2020-07-13 12:46:08 +00:00
|
|
|
when 'HungUp'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['event'] = 'hangup'
|
2020-07-13 12:46:08 +00:00
|
|
|
when 'OutgoingCall'
|
2019-08-16 12:10:36 +00:00
|
|
|
params['direction'] = 'out'
|
|
|
|
params['event'] = 'newCall'
|
2020-07-13 12:46:08 +00:00
|
|
|
when 'CallAccepted'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['event'] = 'answer'
|
|
|
|
end
|
|
|
|
|
|
|
|
# lookup current direction if not given
|
|
|
|
if params['direction'].blank?
|
|
|
|
entry = Cti::Log.find_by(call_id: params[:call_id])
|
|
|
|
if entry
|
|
|
|
params['direction'] = entry.direction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-16 12:10:36 +00:00
|
|
|
# lookup caller if not given
|
|
|
|
if params['user'].blank?
|
|
|
|
# by from parameter for outgoing calls
|
|
|
|
if params['direction'] == 'out' && params['from']&.include?('@')
|
|
|
|
params['user'] = get_voip_user_by_peer(params['from'])
|
|
|
|
end
|
|
|
|
|
|
|
|
# by peer parameter for incoming calls
|
|
|
|
if params['direction'] == 'in' && params['peer'].present?
|
|
|
|
params['user'] = get_voip_user_by_peer(params['peer'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-09 22:07:32 +00:00
|
|
|
# do case mapping
|
2020-07-13 12:46:08 +00:00
|
|
|
case params['type']
|
|
|
|
when 'missed'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['cause'] = 'cancel'
|
2020-07-13 12:46:08 +00:00
|
|
|
when 'voicemail'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['cause'] = 'voicemail'
|
2020-07-13 12:46:08 +00:00
|
|
|
when 'blocked'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['cause'] = 'blocked'
|
2020-07-13 12:46:08 +00:00
|
|
|
when 'accepted'
|
2019-07-09 22:07:32 +00:00
|
|
|
params['cause'] = 'normalClearing'
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def push_open_ticket_screen_recipient
|
|
|
|
|
|
|
|
# try to find answering which answered call
|
|
|
|
user = nil
|
|
|
|
|
|
|
|
# based on peer
|
|
|
|
if @params['peer'].present?
|
|
|
|
user_id = get_user_id_by_peer(@params['peer'])
|
|
|
|
if user_id.present?
|
|
|
|
user = if User.exists?(user_id)
|
|
|
|
User.find(user_id)
|
|
|
|
else
|
|
|
|
User.find_by(email: user_id.downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_voip_user_by_peer(peer)
|
|
|
|
load_voip_users[peer]
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_voip_users
|
|
|
|
return {} if @config.blank? || @config[:api_token].blank?
|
|
|
|
|
2021-05-31 13:05:54 +00:00
|
|
|
list = Cache.read('placetelGetVoipUsers')
|
2019-07-09 22:07:32 +00:00
|
|
|
return list if list
|
|
|
|
|
2022-01-07 11:11:12 +00:00
|
|
|
response = UserAgent.get(
|
|
|
|
'https://api.placetel.de/v2/sip_users',
|
|
|
|
{},
|
2019-07-09 22:07:32 +00:00
|
|
|
{
|
2022-01-07 11:11:12 +00:00
|
|
|
headers: {
|
|
|
|
Authorization: "Bearer #{@config[:api_token]}",
|
|
|
|
},
|
2019-07-09 22:07:32 +00:00
|
|
|
log: {
|
|
|
|
facility: 'placetel',
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
open_timeout: 4,
|
|
|
|
read_timeout: 6,
|
|
|
|
total_timeout: 6,
|
2021-07-20 13:31:46 +00:00
|
|
|
verify_ssl: true,
|
2019-07-09 22:07:32 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if !response.success?
|
|
|
|
Rails.logger.error "Can't fetch getVoipUsers from '#{url}', http code: #{response.code}"
|
|
|
|
Cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
result = response.data
|
|
|
|
if result.blank?
|
|
|
|
Rails.logger.error "Can't fetch getVoipUsers from '#{url}', result: #{response.inspect}"
|
|
|
|
Cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
if result.is_a?(Hash) && (result['result'] == '-1' || result['result_code'] == 'error')
|
|
|
|
Rails.logger.error "Can't fetch getVoipUsers from '#{url}', result: #{result.inspect}"
|
|
|
|
Cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
if !result.is_a?(Array)
|
|
|
|
Rails.logger.error "Can't fetch getVoipUsers from '#{url}', result: #{result.inspect}"
|
|
|
|
Cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
|
|
|
list = {}
|
|
|
|
result.each do |entry|
|
|
|
|
next if entry['name'].blank?
|
2022-01-07 11:11:12 +00:00
|
|
|
next if entry['sipuid'].blank?
|
2019-07-09 22:07:32 +00:00
|
|
|
|
2022-01-07 11:11:12 +00:00
|
|
|
list[entry['sipuid']] = entry['name']
|
2019-07-09 22:07:32 +00:00
|
|
|
end
|
|
|
|
Cache.write('placetelGetVoipUsers', list, { expires_in: 24.hours })
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_user_id_by_peer(peer)
|
|
|
|
return if @config.blank? || @config[:user_device_map].blank?
|
|
|
|
|
|
|
|
@config[:user_device_map].each do |row|
|
|
|
|
next if row[:user_id].blank?
|
|
|
|
return row[:user_id] if row[:device_id] == peer
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|