Class: FlowChat::Messenger::Configuration

Inherits:
Object
  • Object
show all
Includes:
NamedConfiguration
Defined in:
lib/flow_chat/messenger/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.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/flow_chat/messenger/configuration.rb', line 9

def initialize(name)
  @name = name
  @access_token = nil
  @page_id = nil
  @verify_token = nil
  @app_id = nil
  @app_secret = nil
  @skip_signature_validation = false

  FlowChat.logger.debug { "Messenger::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.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def access_token
  @access_token
end

#app_idObject

Returns the value of attribute app_id.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def app_secret
  @app_secret
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def name
  @name
end

#page_idObject

Returns the value of attribute page_id.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def page_id
  @page_id
end

#skip_signature_validationObject

Returns the value of attribute skip_signature_validation.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def skip_signature_validation
  @skip_signature_validation
end

#verify_tokenObject

Returns the value of attribute verify_token.



6
7
8
# File 'lib/flow_chat/messenger/configuration.rb', line 6

def verify_token
  @verify_token
end

Class Method Details

.from_credentialsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flow_chat/messenger/configuration.rb', line 23

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

  config = new(nil)

  if defined?(Rails) && Rails.respond_to?(:application) && Rails.application&.credentials&.messenger
    FlowChat.logger.debug { "Messenger::Configuration: Loading from Rails credentials" }
    credentials = Rails.application.credentials.messenger
    config.access_token = credentials[:access_token]
    config.page_id = credentials[:page_id]
    config.verify_token = credentials[:verify_token]
    config.app_id = credentials[:app_id]
    config.app_secret = credentials[:app_secret]
    config.skip_signature_validation = credentials[:skip_signature_validation] || false
  else
    FlowChat.logger.debug { "Messenger::Configuration: Loading from environment variables" }
    config.access_token = ENV["MESSENGER_ACCESS_TOKEN"]
    config.page_id = ENV["MESSENGER_PAGE_ID"]
    config.verify_token = ENV["MESSENGER_VERIFY_TOKEN"]
    config.app_id = ENV["MESSENGER_APP_ID"]
    config.app_secret = ENV["MESSENGER_APP_SECRET"]
    config.skip_signature_validation = ENV["MESSENGER_SKIP_SIGNATURE_VALIDATION"] == "true"
  end

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

  config
end

Instance Method Details

#account_idObject

The account this configuration speaks for. Named generically, not page_id, so the shared gateway can check an inbound event's account without knowing which platform it holds.



71
72
73
# File 'lib/flow_chat/messenger/configuration.rb', line 71

def 
  page_id
end

#api_base_urlObject



91
92
93
# File 'lib/flow_chat/messenger/configuration.rb', line 91

def api_base_url
  FlowChat::Config.messenger.api_base_url
end

#api_headersObject



95
96
97
98
99
100
# File 'lib/flow_chat/messenger/configuration.rb', line 95

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

#attachment_upload_urlObject



87
88
89
# File 'lib/flow_chat/messenger/configuration.rb', line 87

def attachment_upload_url
  "#{api_base_url}/#{page_id}/message_attachments"
end

#messages_urlObject



83
84
85
# File 'lib/flow_chat/messenger/configuration.rb', line 83

def messages_url
  "#{api_base_url}/#{page_id}/messages"
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/flow_chat/messenger/configuration.rb', line 56

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? &&
    page_id && !page_id.to_s.empty? &&
    verify_token && !verify_token.to_s.empty?)

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

#webhook_account_idObject

The id an inbound webhook's entry.id names. Messenger only ever handles page, which names the Page, so this is the same id a send is addressed to. Instagram's two differ, which is why the gateway asks for this rather than reusing account_id.



79
80
81
# File 'lib/flow_chat/messenger/configuration.rb', line 79

def 
  
end