placetel.rb 4.0 KB

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