Skip to content

Commit

Permalink
Enabled Placetel integration. Added user info via Placetel API lookup…
Browse files Browse the repository at this point in the history
… (optional).
  • Loading branch information
znuny-robo committed Oct 30, 2018
1 parent 66e8d58 commit a2c5253
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 33 deletions.
Expand Up @@ -60,6 +60,8 @@ class Form extends App.Controller
config = @config
cleanupInput = @cleanupInput

config.api_token = @$('input[name=api_token]').val()

# default caller_id
default_caller_id = @$('input[name=default_caller_id]').val()
config.outbound.default_caller_id = cleanupInput(default_caller_id)
Expand Down
16 changes: 16 additions & 0 deletions app/assets/javascripts/app/views/integration/placetel.jst.eco
Expand Up @@ -19,6 +19,22 @@
</table>
</div>

<p><%- @T('In order for Zammad to access %s, the %s API token must be stored here', 'Placetel', 'Placetel') %>:<p>
<div class="settings-entry">
<table class="settings-list" style="width: 100%;">
<thead>
<tr>
<th width="20%"><%- @T('Type') %>
<th width="80%"><%- @T('Content') %>
</thead>
<tbody>
<tr>
<td class="settings-list-row-control"><%- @T('API Token') %>
<td class="settings-list-control-cell"><input type="input" class="form-control form-control--small js-select" value="<%= @config.api_token %>" name="api_token" placeholder="">
</tbody>
</table>
</div>

<h2><%- @T('Inbound') %></h2>

<p><%- @T('Blocked caller ids based on sender caller id.') %>
Expand Down
65 changes: 65 additions & 0 deletions app/controllers/integration/placetel_controller.rb
Expand Up @@ -20,6 +20,10 @@ def event
local_params['event'] = 'answer'
end

if local_params['user'].blank? && local_params['peer']
local_params['user'] = get_voip_user_by_peer(local_params['peer'])
end

if local_params['direction'].blank?
entry = Cti::Log.find_by(call_id: params[:call_id])
if entry
Expand Down Expand Up @@ -164,4 +168,65 @@ def response_unauthorized(error)
xml_error(error, 401)
end

def get_voip_user_by_peer(peer)
load_voip_users[peer]
end

def load_voip_users
return {} if config_integration.blank? || config_integration[:api_token].blank?

list = Cache.get('placetelGetVoipUsers')
return list if list

response = UserAgent.post(
'https://api.placetel.de/api/getVoIPUsers.json',
{
api_key: config_integration[:api_token],
},
{
log: {
facility: 'placetel',
},
json: true,
open_timeout: 4,
read_timeout: 6,
total_timeout: 6,
},
)
if !response.success?
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?
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')
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)
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?

if entry['uid'].present?
list[entry['uid']] = entry['name']
end
next if entry['uid2'].blank?

list[entry['uid2']] = entry['name']
end
Cache.write('placetelGetVoipUsers', list, { expires_in: 24.hours })
list
end
end
15 changes: 13 additions & 2 deletions app/models/setting.rb
Expand Up @@ -7,8 +7,8 @@ class Setting < ApplicationModel
store :preferences
before_create :state_check, :set_initial, :check_broadcast
before_update :state_check, :check_broadcast
after_create :reset_change_id
after_update :reset_change_id
after_create :reset_change_id, :reset_cache
after_update :reset_change_id, :reset_cache

attr_accessor :state

Expand Down Expand Up @@ -40,6 +40,7 @@ def self.set(name, value)
setting.state_current = { value: value }
setting.save!
logger.info "Setting.set('#{name}', #{value.inspect})"
true
end

=begin
Expand Down Expand Up @@ -75,6 +76,7 @@ def self.reset(name, force = false)
setting.state_current = setting.state_initial
setting.save!
logger.info "Setting.reset('#{name}', #{setting.state_current.inspect})"
true
end

=begin
Expand Down Expand Up @@ -145,6 +147,15 @@ def reset_change_id
true
end

def reset_cache
return true if preferences[:cache].blank?

preferences[:cache].each do |key|
Cache.delete(key)
end
true
end

# check if cache is still valid
def self.cache_valid?
if @@lookup_at && @@lookup_at > Time.zone.now - @@lookup_timeout
Expand Down
@@ -1,4 +1,4 @@
class SettingAddPlacetel < ActiveRecord::Migration[5.1]
class SettingAddPlacetel1 < ActiveRecord::Migration[5.1]
def change

# return if it's a new setup
Expand Down Expand Up @@ -32,19 +32,26 @@ def change
},
frontend: true
)
Setting.create_if_not_exists(
title: 'Placetel config',
name: 'placetel_config',
area: 'Integration::Placetel',
description: 'Defines the Placetel config.',
options: {},
state: { 'outbound' => { 'routing_table' => [], 'default_caller_id' => '' }, 'inbound' => { 'block_caller_ids' => [] } },
preferences: {
prio: 2,
permission: ['admin.integration'],
},
frontend: false,
)
placetel_config = Setting.find_by(name: 'placetel_config')
if !placetel_config
Setting.create!(
title: 'Placetel config',
name: 'placetel_config',
area: 'Integration::Placetel',
description: 'Defines the Placetel config.',
options: {},
state: { 'outbound' => { 'routing_table' => [], 'default_caller_id' => '' }, 'inbound' => { 'block_caller_ids' => [] } },
preferences: {
prio: 2,
permission: ['admin.integration'],
cache: ['placetelGetVoipUsers'],
},
frontend: false,
)
else
placetel_config.preferences[:cache] = ['placetelGetVoipUsers']
placetel_config.save!
end
Setting.create_if_not_exists(
title: 'PLACETEL Token',
name: 'placetel_token',
Expand All @@ -66,6 +73,5 @@ def change
},
frontend: false
)

end
end
3 changes: 2 additions & 1 deletion db/seeds/settings.rb
Expand Up @@ -4034,14 +4034,15 @@
preferences: {
prio: 2,
permission: ['admin.integration'],
cache: ['placetelGetVoipUsers'],
},
frontend: false,
)
Setting.create_if_not_exists(
title: 'PLACETEL Token',
name: 'placetel_token',
area: 'Integration::Placetel',
description: 'Token for placetel.',
description: 'Token for Placetel.',
options: {
form: [
{
Expand Down
1 change: 0 additions & 1 deletion script/build/cleanup.sh
Expand Up @@ -5,4 +5,3 @@ set -ex
rm app/assets/javascripts/app/controllers/layout_ref.coffee
rm -rf app/assets/javascripts/app/views/layout_ref/
rm app/assets/javascripts/app/controllers/karma.coffee
rm app/assets/javascripts/app/controllers/_integration/placetel.coffee

0 comments on commit a2c5253

Please sign in to comment.