Class: FlowChat::Instagram::Configuration

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

Constant Summary collapse

LOGIN_PATHS =

:facebook is the Instagram API with Facebook Login: the linked Page speaks through graph.facebook.com. :instagram is the Instagram API with Instagram Login: the Instagram professional account speaks for itself through graph.instagram.com, with no Page in the picture.

[:facebook, :instagram].freeze

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.



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

def initialize(name)
  @name = name
  @access_token = nil
  @page_id = nil
  @instagram_account_id = nil
  @verify_token = nil
  @app_id = nil
  @app_secret = nil
  @skip_signature_validation = false
  @login = :facebook

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



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def access_token
  @access_token
end

#app_idObject

Returns the value of attribute app_id.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def app_secret
  @app_secret
end

#instagram_account_idObject

Returns the value of attribute instagram_account_id.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def 
  @instagram_account_id
end

#loginObject

Returns the value of attribute login.



14
15
16
# File 'lib/flow_chat/instagram/configuration.rb', line 14

def 
  @login
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def name
  @name
end

#page_idObject

Returns the value of attribute page_id.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def page_id
  @page_id
end

#skip_signature_validationObject

Returns the value of attribute skip_signature_validation.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def skip_signature_validation
  @skip_signature_validation
end

#verify_tokenObject

Returns the value of attribute verify_token.



12
13
14
# File 'lib/flow_chat/instagram/configuration.rb', line 12

def verify_token
  @verify_token
end

Class Method Details

.from_credentialsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flow_chat/instagram/configuration.rb', line 44

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

  config = new(nil)

  if defined?(Rails) && Rails.respond_to?(:application) && Rails.application&.credentials&.instagram
    FlowChat.logger.debug { "Instagram::Configuration: Loading from Rails credentials" }
    credentials = Rails.application.credentials.instagram
    config.access_token = credentials[:access_token]
    config.page_id = credentials[:page_id]
    config. = credentials[:instagram_account_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
    config. = (credentials[:login] || "facebook").to_sym
  else
    FlowChat.logger.debug { "Instagram::Configuration: Loading from environment variables" }
    config.access_token = ENV["INSTAGRAM_ACCESS_TOKEN"]
    config.page_id = ENV["INSTAGRAM_PAGE_ID"]
    config. = ENV["INSTAGRAM_ACCOUNT_ID"]
    config.verify_token = ENV["INSTAGRAM_VERIFY_TOKEN"]
    config.app_id = ENV["INSTAGRAM_APP_ID"]
    config.app_secret = ENV["INSTAGRAM_APP_SECRET"]
    config.skip_signature_validation = ENV["INSTAGRAM_SKIP_SIGNATURE_VALIDATION"] == "true"
    config. = (ENV["INSTAGRAM_LOGIN"] || "facebook").to_sym
  end

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

  config
end

Instance Method Details

#account_idObject

What a send is addressed to. An account reached through a Page answers as that Page over graph.facebook.com; an account with no Page answers for itself over graph.instagram.com. This is not the id an inbound delivery names, which is webhook_account_id below.



111
112
113
# File 'lib/flow_chat/instagram/configuration.rb', line 111

def 
  ( == :instagram) ?  : page_id
end

#api_base_urlObject



135
136
137
# File 'lib/flow_chat/instagram/configuration.rb', line 135

def api_base_url
  ( == :instagram) ? FlowChat::Config.instagram. : FlowChat::Config.instagram.api_base_url
end

#api_headersObject



139
140
141
142
143
144
# File 'lib/flow_chat/instagram/configuration.rb', line 139

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

#attachment_upload_urlObject



131
132
133
# File 'lib/flow_chat/instagram/configuration.rb', line 131

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

#messages_urlObject



127
128
129
# File 'lib/flow_chat/instagram/configuration.rb', line 127

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

#valid?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/flow_chat/instagram/configuration.rb', line 81

def valid?
  # Both ids are required, because sending and receiving key on
  # different ones and only on the :facebook path do they differ.
  # account_id is what a send is addressed as (the Page there);
  # instagram_account_id is what an inbound delivery names in entry.id
  # on both paths, which is what webhook_account_id encodes.
  #
  # Checking only account_id passed a :facebook configuration that had
  # never been given an instagram_account_id, and the gateway then
  # rejected every delivery it received: the id it compares against was
  # blank, and a blank expectation matches nothing. A configuration that
  # answers the handshake and then refuses all traffic is worse than one
  # that admits up front it is incomplete.
  #
  # 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? &&
    verify_token && !verify_token.to_s.empty? &&
     && !.to_s.empty? &&
     && !.to_s.empty?)

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

#webhook_account_idObject

The id an inbound webhook's entry.id names, which is a different question from account_id above and has a different answer on the Facebook Login path.

The top-level object of a delivery decides the id space, and this gateway only ever handles instagram (see expected_webhook_object), which names the Instagram professional account. That holds on both paths, so unlike account_id this does not depend on login.



123
124
125
# File 'lib/flow_chat/instagram/configuration.rb', line 123

def 
  
end