placetel_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'builder'
  3. class Integration::PlacetelController < ApplicationController
  4. skip_before_action :verify_csrf_token
  5. before_action :check_configured, :check_token
  6. # notify about inbound call / block inbound call
  7. def event
  8. local_params = ActiveSupport::HashWithIndifferentAccess.new(params.permit!.to_h)
  9. cti = Cti::Driver::Placetel.new(params: local_params, config: config_integration)
  10. result = cti.process
  11. # check if inbound call should get rejected
  12. if result[:action] == 'reject'
  13. response_reject(result)
  14. return true
  15. end
  16. # check if outbound call changes the outbound caller_id
  17. if result[:action] == 'set_caller_id'
  18. response_set_caller_id(result)
  19. return true
  20. end
  21. if result[:action] == 'invalid_direction'
  22. response_error(__("Invalid 'direction'!"))
  23. return true
  24. end
  25. response_ok(response)
  26. true
  27. end
  28. private
  29. def check_token
  30. if Setting.get('placetel_token') != params[:token]
  31. response_unauthorized(__('Invalid token, please contact your admin!'))
  32. return
  33. end
  34. true
  35. end
  36. def check_configured
  37. http_log_config facility: 'placetel'
  38. if !Setting.get('placetel_integration')
  39. response_error(__('Feature is disabled, please contact your administrator!'))
  40. return
  41. end
  42. if config_integration.blank? || config_integration[:inbound].blank? || config_integration[:outbound].blank?
  43. response_error(__('Feature not configured, please contact your admin!'))
  44. return
  45. end
  46. true
  47. end
  48. def xml_error(error, code)
  49. xml = Builder::XmlMarkup.new(indent: 2)
  50. xml.instruct!
  51. content = xml.Response() do
  52. xml.Error(error)
  53. end
  54. send_data content, type: 'application/xml; charset=UTF-8;', status: code
  55. end
  56. def config_integration
  57. @config_integration ||= Setting.get('placetel_config')
  58. end
  59. def response_error(error)
  60. xml_error(error, 422)
  61. end
  62. def response_unauthorized(error)
  63. xml_error(error, 401)
  64. end
  65. def response_reject(_result)
  66. xml = Builder::XmlMarkup.new(indent: 2)
  67. xml.instruct!
  68. content = xml.Response() do
  69. xml.Reject({ reason: 'busy' })
  70. end
  71. send_data content, type: 'application/xml; charset=UTF-8;'
  72. end
  73. def response_set_caller_id(result)
  74. xml = Builder::XmlMarkup.new(indent: 2)
  75. xml.instruct!
  76. content = xml.Response() do
  77. xml.Dial(callerId: result[:params][:from_caller_id]) { xml.Number(result[:params][:to_caller_id]) }
  78. end
  79. send_data(content, type: 'application/xml; charset=UTF-8;')
  80. end
  81. def response_ok(_result)
  82. xml = Builder::XmlMarkup.new(indent: 2)
  83. xml.instruct!
  84. content = xml.Response()
  85. send_data content, type: 'application/xml; charset=UTF-8;'
  86. end
  87. end