placetel.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Cti::Driver::Placetel < Cti::Driver::Base
  3. PLACETEL_SIP_USERS_URL = 'https://api.placetel.de/v2/sip_users'.freeze
  4. def config
  5. Setting.get('placetel_config')
  6. end
  7. def mapping(params)
  8. # do event mapping
  9. case params['event']
  10. when 'IncomingCall'
  11. params['direction'] = 'in'
  12. params['event'] = 'newCall'
  13. when 'HungUp'
  14. params['event'] = 'hangup'
  15. when 'OutgoingCall'
  16. params['direction'] = 'out'
  17. params['event'] = 'newCall'
  18. when 'CallAccepted'
  19. params['event'] = 'answer'
  20. end
  21. # lookup current direction if not given
  22. if params['direction'].blank?
  23. entry = Cti::Log.find_by(call_id: params[:call_id])
  24. if entry
  25. params['direction'] = entry.direction
  26. end
  27. end
  28. # lookup caller if not given
  29. if params['user'].blank?
  30. # by from parameter for outgoing calls
  31. if params['direction'] == 'out' && params['from']&.include?('@')
  32. params['user'] = get_voip_user_by_peer(params['from'])
  33. end
  34. # by peer parameter for incoming calls
  35. if params['direction'] == 'in' && params['peer'].present?
  36. params['user'] = get_voip_user_by_peer(params['peer'])
  37. end
  38. end
  39. # do case mapping
  40. case params['type']
  41. when 'missed'
  42. params['cause'] = 'cancel'
  43. when 'voicemail'
  44. params['cause'] = 'voicemail'
  45. when 'blocked'
  46. params['cause'] = 'blocked'
  47. when 'accepted'
  48. params['cause'] = 'normalClearing'
  49. end
  50. params
  51. end
  52. def push_open_ticket_screen_recipient
  53. # try to find answering which answered call
  54. user = nil
  55. # based on peer
  56. if @params['peer'].present?
  57. user_id = get_user_id_by_peer(@params['peer'])
  58. if user_id.present?
  59. user = if User.exists?(user_id)
  60. User.find(user_id)
  61. else
  62. User.find_by(email: user_id.downcase)
  63. end
  64. end
  65. end
  66. user
  67. end
  68. def get_voip_user_by_peer(peer)
  69. load_voip_users[peer]
  70. end
  71. def load_voip_users
  72. return {} if @config.blank? || @config[:api_token].blank?
  73. list = Rails.cache.read('placetelGetVoipUsers')
  74. return list if list
  75. response = UserAgent.get(
  76. PLACETEL_SIP_USERS_URL,
  77. {},
  78. {
  79. headers: {
  80. Authorization: "Bearer #{@config[:api_token]}",
  81. },
  82. log: {
  83. facility: 'placetel',
  84. },
  85. json: true,
  86. open_timeout: 4,
  87. read_timeout: 6,
  88. total_timeout: 6,
  89. verify_ssl: true,
  90. },
  91. )
  92. if !response.success?
  93. Rails.logger.error "Can't fetch getVoipUsers from '#{PLACETEL_SIP_USERS_URL}', http code: #{response.code}"
  94. Rails.cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
  95. return {}
  96. end
  97. result = response.data
  98. if result.blank?
  99. Rails.logger.error "Can't fetch getVoipUsers from '#{PLACETEL_SIP_USERS_URL}', result: #{response.inspect}"
  100. Rails.cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
  101. return {}
  102. end
  103. if result.is_a?(Hash) && (result['result'] == '-1' || result['result_code'] == 'error')
  104. Rails.logger.error "Can't fetch getVoipUsers from '#{PLACETEL_SIP_USERS_URL}', result: #{result.inspect}"
  105. Rails.cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
  106. return {}
  107. end
  108. if !result.is_a?(Array)
  109. Rails.logger.error "Can't fetch getVoipUsers from '#{PLACETEL_SIP_USERS_URL}', result: #{result.inspect}"
  110. Rails.cache.write('placetelGetVoipUsers', {}, { expires_in: 1.hour })
  111. return {}
  112. end
  113. list = {}
  114. result.each do |entry|
  115. next if entry['name'].blank?
  116. next if entry['sipuid'].blank?
  117. list[entry['sipuid']] = entry['name']
  118. end
  119. Rails.cache.write('placetelGetVoipUsers', list, { expires_in: 24.hours })
  120. list
  121. end
  122. def get_user_id_by_peer(peer)
  123. return if @config.blank? || @config[:user_device_map].blank?
  124. @config[:user_device_map].each do |row|
  125. next if row[:user_id].blank?
  126. return row[:user_id] if row[:device_id] == peer
  127. end
  128. nil
  129. end
  130. end