base.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Service::Channel::Whatsapp
  3. class Base < Service::Base
  4. attr_reader :params
  5. private
  6. def area
  7. 'WhatsApp::Business'.freeze
  8. end
  9. def area_channel_list
  10. Channel.in_area(area)
  11. end
  12. def attributes_hash
  13. {
  14. group_id:,
  15. options:
  16. }
  17. end
  18. def group_id
  19. params[:group_id]
  20. end
  21. def options
  22. params.slice(
  23. :business_id, :access_token, :app_secret, :phone_number_id, :welcome, :reminder_active, :reminder_message
  24. )
  25. end
  26. def add_metadata(channel:, initial: false)
  27. phone_number_info = get_phone_number_info(channel)
  28. raise __('Could not fetch WhatsApp phone number details.') if phone_number_info.nil?
  29. channel.options.merge! phone_number_info
  30. channel.options.merge! initial_options if initial
  31. channel.save!
  32. end
  33. def get_phone_number_info(channel)
  34. Whatsapp::Account::PhoneNumbers
  35. .new(**channel.options.slice(:business_id, :access_token).symbolize_keys)
  36. .get(channel.options[:phone_number_id])
  37. end
  38. def initial_options
  39. {
  40. adapter: 'whatsapp',
  41. callback_url_uuid: SecureRandom.uuid,
  42. verify_token: SecureRandom.urlsafe_base64(12),
  43. }
  44. end
  45. end
  46. end