sipgate_io.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Cti::Driver::SipgateIo < Cti::Driver::Base
  3. def config
  4. Setting.get('sipgate_config')
  5. end
  6. def push_open_ticket_screen_recipient
  7. # try to find answering which answered call
  8. user = nil
  9. # based on peer
  10. if @params['userId'].present?
  11. user_id = get_user_id_by_sipgate_user_id(@params['userId'])
  12. if user_id.present?
  13. user = if User.exists?(user_id)
  14. User.find(user_id)
  15. else
  16. User.find_by(email: user_id.downcase)
  17. end
  18. end
  19. end
  20. user
  21. end
  22. def load_voip_users
  23. return {} if @config.blank? || @config[:api_user].blank? || @config[:api_password].blank?
  24. list = Rails.cache.read('sipgateUserList')
  25. return list if list
  26. url = 'https://api.sipgate.com/v2/users'
  27. response = UserAgent.get(
  28. url,
  29. {},
  30. {
  31. user: @config[:api_user],
  32. password: @config[:api_password],
  33. log: {
  34. facility: 'sipagte.io',
  35. },
  36. json: true,
  37. open_timeout: 4,
  38. read_timeout: 6,
  39. total_timeout: 6,
  40. verify_ssl: true,
  41. },
  42. )
  43. if !response.success?
  44. Rails.logger.error "Can't fetch users from '#{url}', http code: #{response.code}"
  45. Rails.cache.write('sipgateUserList', {}, { expires_in: 1.hour })
  46. return {}
  47. end
  48. result = response.data
  49. if result.blank?
  50. Rails.logger.error "Can't fetch users from '#{url}', result: #{response.inspect}"
  51. Rails.cache.write('sipgateUserList', {}, { expires_in: 1.hour })
  52. return {}
  53. end
  54. if result.is_a?(Array) && (result['result'] == '-1' || result['result_code'] == 'error')
  55. Rails.logger.error "Can't fetch users from '#{url}', result: #{result.inspect}"
  56. Rails.cache.write('sipgateUserList', {}, { expires_in: 1.hour })
  57. return {}
  58. end
  59. if !result.is_a?(Hash)
  60. Rails.logger.error "Can't fetch users from '#{url}', result: #{result.inspect}"
  61. Rails.cache.write('sipgateUserList', {}, { expires_in: 1.hour })
  62. return {}
  63. end
  64. if result['items'].blank?
  65. Rails.logger.error "Can't fetch users from '#{url}', no items found, result: #{result.inspect}"
  66. Rails.cache.write('sipgateUserList', {}, { expires_in: 1.hour })
  67. return {}
  68. end
  69. list = {}
  70. items = %w[firstname lastname email]
  71. result['items'].each do |entry|
  72. next if entry['id'].blank?
  73. name = ''
  74. items.each do |item|
  75. next if entry[item].blank?
  76. name += ' ' if name.present?
  77. name += entry[item]
  78. end
  79. list[entry['id']] = name
  80. end
  81. Rails.cache.write('sipgateUserList', list, { expires_in: 24.hours })
  82. list
  83. end
  84. def get_user_id_by_sipgate_user_id(user_id)
  85. return if @config.blank? || @config[:user_remote_map].blank?
  86. @config[:user_remote_map].each do |row|
  87. next if row[:user_id].blank?
  88. return row[:user_id] if row[:remote_user_id] == user_id
  89. end
  90. nil
  91. end
  92. end