Class: FlowChat::Whatsapp::Configuration

Inherits:
Object
  • Object
show all
Includes:
NamedConfiguration
Defined in:
lib/flow_chat/whatsapp/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NamedConfiguration

included, #register_as

Constructor Details

#initialize(name) ⇒ Configuration

Returns a new instance of Configuration.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flow_chat/whatsapp/configuration.rb', line 14

def initialize(name)
  @name = name
  @access_token = nil
  @phone_number_id = nil
  @verify_token = nil
  @app_id = nil
  @app_secret = nil
  @webhook_verify_token = nil
  @business_account_id = nil
  @skip_signature_validation = false

  FlowChat.logger.debug { "WhatsApp::Configuration: Initialized configuration with name: #{name || "anonymous"}" }

  register_as(name) if name.present?
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def access_token
  @access_token
end

#app_idObject

Returns the value of attribute app_id.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def app_secret
  @app_secret
end

#business_account_idObject

Returns the value of attribute business_account_id.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def 
  @business_account_id
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def name
  @name
end

#phone_number_idObject

Returns the value of attribute phone_number_id.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def phone_number_id
  @phone_number_id
end

#skip_signature_validationObject

Returns the value of attribute skip_signature_validation.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def skip_signature_validation
  @skip_signature_validation
end

#verify_tokenObject

Returns the value of attribute verify_token.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def verify_token
  @verify_token
end

#webhook_verify_tokenObject

Returns the value of attribute webhook_verify_token.



11
12
13
# File 'lib/flow_chat/whatsapp/configuration.rb', line 11

def webhook_verify_token
  @webhook_verify_token
end

Class Method Details

.configuration_labelObject

"Whatsapp" is the constant, "WhatsApp" is the product.



7
8
9
# File 'lib/flow_chat/whatsapp/configuration.rb', line 7

def self.configuration_label
  "WhatsApp"
end

.from_credentialsObject

Load configuration from Rails credentials or environment variables



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flow_chat/whatsapp/configuration.rb', line 31

def self.from_credentials
  FlowChat.logger.info { "WhatsApp::Configuration: Loading configuration from credentials/environment" }

  config = new(nil)

  if defined?(Rails) && Rails.respond_to?(:application) && Rails.application&.credentials&.whatsapp
    FlowChat.logger.debug { "WhatsApp::Configuration: Loading from Rails credentials" }
    credentials = Rails.application.credentials.whatsapp
    config.access_token = credentials[:access_token]
    config.phone_number_id = credentials[:phone_number_id]
    config.verify_token = credentials[:verify_token]
    config.app_id = credentials[:app_id]
    config.app_secret = credentials[:app_secret]
    config. = credentials[:business_account_id]
    config.skip_signature_validation = credentials[:skip_signature_validation] || false
  else
    FlowChat.logger.debug { "WhatsApp::Configuration: Loading from environment variables" }
    # Fallback to environment variables
    config.access_token = ENV["WHATSAPP_ACCESS_TOKEN"]
    config.phone_number_id = ENV["WHATSAPP_PHONE_NUMBER_ID"]
    config.verify_token = ENV["WHATSAPP_VERIFY_TOKEN"]
    config.app_id = ENV["WHATSAPP_APP_ID"]
    config.app_secret = ENV["WHATSAPP_APP_SECRET"]
    config. = ENV["WHATSAPP_BUSINESS_ACCOUNT_ID"]
    config.skip_signature_validation = ENV["WHATSAPP_SKIP_SIGNATURE_VALIDATION"] == "true"
  end

  if config.valid?
    FlowChat.logger.info { "WhatsApp::Configuration: Configuration loaded successfully - phone_number_id: #{config.phone_number_id}" }
  else
    FlowChat.logger.warn { "WhatsApp::Configuration: Incomplete configuration loaded - missing required fields" }
  end

  config
end

Instance Method Details

#api_base_urlObject

Get API base URL from global config



93
94
95
# File 'lib/flow_chat/whatsapp/configuration.rb', line 93

def api_base_url
  FlowChat::Config.whatsapp.api_base_url
end

#api_headersObject

Headers for API requests



98
99
100
101
102
103
# File 'lib/flow_chat/whatsapp/configuration.rb', line 98

def api_headers
  {
    "Authorization" => "Bearer #{access_token}",
    "Content-Type" => "application/json"
  }
end

#media_url(media_id) ⇒ Object



84
85
86
# File 'lib/flow_chat/whatsapp/configuration.rb', line 84

def media_url(media_id)
  "#{FlowChat::Config.whatsapp.api_base_url}/#{media_id}"
end

#messages_urlObject

API endpoints



80
81
82
# File 'lib/flow_chat/whatsapp/configuration.rb', line 80

def messages_url
  "#{FlowChat::Config.whatsapp.api_base_url}/#{phone_number_id}/messages"
end

#phone_numbers_urlObject



88
89
90
# File 'lib/flow_chat/whatsapp/configuration.rb', line 88

def phone_numbers_url
  "#{FlowChat::Config.whatsapp.api_base_url}/#{}/phone_numbers"
end

#valid?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/flow_chat/whatsapp/configuration.rb', line 67

def valid?
  # Wrapped so a predicate answers true or false rather than nil, which
  # the bare && chain returns for a missing first field. Intercom and
  # Telegram already do this and pin it in their tests.
  is_valid = !!(access_token && !access_token.to_s.empty? &&
    phone_number_id && !phone_number_id.to_s.empty? &&
    verify_token && !verify_token.to_s.empty?)

  FlowChat.logger.debug { "WhatsApp::Configuration: Configuration valid: #{is_valid}" }
  is_valid
end