cti_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Integration::CtiController < ApplicationController
  3. skip_before_action :verify_csrf_token
  4. before_action :check_configured, :check_token
  5. # notify about inbound call / block inbound call
  6. def event
  7. if params['direction'] == 'in'
  8. if params['event'] == 'newCall'
  9. config_inbound = config_integration[:inbound] || {}
  10. block_caller_ids = config_inbound[:block_caller_ids] || []
  11. # check if call need to be blocked
  12. block_caller_ids.each do |item|
  13. next unless item[:caller_id] == params['from']
  14. render json: { action: 'reject', reason: 'busy' }, status: :ok
  15. #params['Reject'] = 'busy'
  16. params['comment'] = 'reject, busy'
  17. if params['user']
  18. params['comment'] = "#{params['user']} -> reject, busy"
  19. end
  20. Cti::Log.process(params)
  21. return true
  22. end
  23. end
  24. Cti::Log.process(params)
  25. render json: {}, status: :ok
  26. return true
  27. elsif params['direction'] == 'out'
  28. config_outbound = config_integration[:outbound]
  29. routing_table = nil
  30. default_caller_id = nil
  31. if config_outbound.present?
  32. routing_table = config_outbound[:routing_table]
  33. default_caller_id = config_outbound[:default_caller_id]
  34. end
  35. # set callerId
  36. data = {}
  37. to = params[:to]
  38. from = nil
  39. if to && routing_table.present?
  40. routing_table.each do |row|
  41. dest = row[:dest].gsub(/\*/, '.+?')
  42. next if to !~ /^#{dest}$/
  43. from = row[:caller_id]
  44. data = {
  45. action: 'dial',
  46. caller_id: from,
  47. number: params[:to]
  48. }
  49. break
  50. end
  51. if data.blank? && default_caller_id.present?
  52. from = default_caller_id
  53. data = {
  54. action: 'dial',
  55. caller_id: default_caller_id,
  56. number: params[:to]
  57. }
  58. end
  59. end
  60. render json: data, status: :ok
  61. if from.present?
  62. params['from'] = from
  63. end
  64. Cti::Log.process(params)
  65. return true
  66. end
  67. render json: { error: 'Invalid direction!' }, status: :unprocessable_entity
  68. end
  69. private
  70. def check_token
  71. if Setting.get('cti_token') != params[:token]
  72. response_unauthorized('Invalid token, please contact your admin!')
  73. return
  74. end
  75. true
  76. end
  77. def check_configured
  78. http_log_config facility: 'cti'
  79. if !Setting.get('cti_integration')
  80. response_error('Feature is disable, please contact your admin to enable it!')
  81. return
  82. end
  83. if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
  84. response_error('Feature not configured, please contact your admin!')
  85. return
  86. end
  87. true
  88. end
  89. def config_integration
  90. @config_integration ||= Setting.get('cti_config')
  91. end
  92. def response_error(error)
  93. render json: { error: error }, status: :unprocessable_entity
  94. end
  95. def response_unauthorized(error)
  96. render json: { error: error }, status: :unauthorized
  97. end
  98. end