cti_controller.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://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. local_params = ActiveSupport::HashWithIndifferentAccess.new(params.permit!.to_h)
  8. cti = Cti::Driver::Cti.new(params: local_params, config: config_integration)
  9. result = cti.process
  10. # check if inbound call should get rejected
  11. if result[:action] == 'reject'
  12. response_ok(action: 'reject', reason: 'busy')
  13. return true
  14. end
  15. # check if outbound call changes the outbound caller_id
  16. if result[:action] == 'set_caller_id'
  17. data = {
  18. action: 'dial',
  19. caller_id: result[:params][:from_caller_id],
  20. number: result[:params][:to_caller_id],
  21. }
  22. response_ok(data)
  23. return true
  24. end
  25. if result[:action] == 'invalid_direction'
  26. response_error(__("Invalid 'direction'!"))
  27. return true
  28. end
  29. response_ok({})
  30. end
  31. private
  32. def check_token
  33. if Setting.get('cti_token') != params[:token]
  34. response_unauthorized(__('Invalid token, please contact your admin!'))
  35. return
  36. end
  37. true
  38. end
  39. def check_configured
  40. http_log_config facility: 'cti'
  41. if !Setting.get('cti_integration')
  42. response_error(__('Feature is disabled, please contact your administrator!'))
  43. return
  44. end
  45. if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
  46. response_error(__('Feature not configured, please contact your admin!'))
  47. return
  48. end
  49. true
  50. end
  51. def config_integration
  52. @config_integration ||= Setting.get('cti_config')
  53. end
  54. def response_error(error)
  55. render json: { error: error }, status: :unprocessable_entity
  56. end
  57. def response_unauthorized(error)
  58. render json: { error: error }, status: :unauthorized
  59. end
  60. def response_ok(data)
  61. render json: data, status: :ok
  62. end
  63. end