facebook-messenger 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6973b5c241d0124c75e08a0441e26c8190d51f24
4
- data.tar.gz: 58aae6e494b7a89f58e2d4e0f5e3c33df31b89cb
2
+ SHA256:
3
+ metadata.gz: f17a2863cfd6e0797b3a942f6fb5959557be525c40e1efc698542a6ab3662ac6
4
+ data.tar.gz: e94ac387ab7174e04429cc5afa2b27121fb1bfd03afa3067e3ed61db3b4f34ba
5
5
  SHA512:
6
- metadata.gz: 0ab183695ddbe1dcb6136bd4c2537b3f619e1cc0f96ddf63c3aac981e6ca947c19ca53437db1773c98419a0a5ea42179a31ae52dc6eb3f4f62edd78470722eb4
7
- data.tar.gz: 49e973311172b2fbc2e81bce8c3a35f89470e5240da931ddd12b08fcff00e3f3d3d571b295565b6c12542d401c1ff3de87384cc8ef46a19f892f0d5fb93fa3bf
6
+ metadata.gz: 2d673c880dfd1647805347e666586aa7478be689b2660e2a91a69084d7b63f576509c917751b7fcc10f8fa8f28147fe1ff8041a196d8aa5850a2bfa0dd1d60c5
7
+ data.tar.gz: 801a4fa53d78bfd7e03cf365b57899b25ac085f12507b8307d468b132dac2caeff2d149ee5eb521967548933ded81f0a27957995257e2a0e2a6f21870aec5555
data/README.md CHANGED
@@ -48,9 +48,21 @@ Bot.deliver({
48
48
  text: 'Human?'
49
49
  },
50
50
  message_type: Facebook::Messenger::Bot::MessagingType::UPDATE
51
- }, access_token: ENV['ACCESS_TOKEN'])
51
+ }, access_token: ENV['ACCESS_TOKEN'], app_secret_proof: app_secret_proof
52
+ )
53
+ ```
54
+
55
+ NOTE: `app_secret_proof` is an optional parameter to [secure your requests](https://developers.facebook.com/docs/graph-api/securing-requests/),
56
+ and you can generate it from your configuration provider like so:
57
+
58
+ ```ruby
59
+ configuration_provider = Facebook::Messenger::Configuration::Providers.Environment.new
60
+ app_secret_proof = configuration_provider.app_secret_proof_for(page_id)
52
61
  ```
53
62
 
63
+ For the methods you'll usually use (like `reply` and `typing_on`), the app secret proof will be set and sent
64
+ automatically if you set the environment variable `APP_SECRET_PROOF_ENABLED` to `true`.
65
+
54
66
  ##### Messages with images
55
67
 
56
68
  The human may require visual aid to understand:
@@ -432,7 +444,10 @@ Once you've configured your bot, subscribe it to the Page to get messages
432
444
  from Facebook:
433
445
 
434
446
  ```ruby
435
- Facebook::Messenger::Subscriptions.subscribe(access_token: access_token)
447
+ Facebook::Messenger::Subscriptions.subscribe(
448
+ access_token: access_token,
449
+ subscribed_fields: %w[feed mention name]
450
+ )
436
451
  ```
437
452
 
438
453
  You only need to subscribe your page once. As long as your bot works and
@@ -536,7 +551,10 @@ require 'facebook/messenger'
536
551
  include Facebook::Messenger
537
552
 
538
553
  class Listener
539
- Facebook::Messenger::Subscriptions.subscribe(access_token: ENV["FB_ACCESS_TOKEN"])
554
+ Facebook::Messenger::Subscriptions.subscribe(
555
+ access_token: ENV["FB_ACCESS_TOKEN"],
556
+ subscribed_fields: %w[feed mention name]
557
+ )
540
558
 
541
559
  Bot.on :message do |message|
542
560
  Bot.deliver({
@@ -13,7 +13,7 @@ module Facebook
13
13
  include HTTParty
14
14
 
15
15
  # Define base_uri for HTTParty.
16
- base_uri 'https://graph.facebook.com/v2.9/me'
16
+ base_uri 'https://graph.facebook.com/v3.2/me'
17
17
 
18
18
  #
19
19
  # @return [Array] Array containing the supported webhook events.
@@ -39,16 +39,22 @@ module Facebook
39
39
  #
40
40
  # @param [Hash] message A Hash describing the recipient and the message.
41
41
  # @param [String] access_token Access token.
42
+ # @param [String] app_secret_proof proof of the app_secret
43
+ # https://developers.facebook.com/docs/graph-api/securing-requests/
44
+ # Note: we provide a helper function available at
45
+ # Messenger::Configuration::Providers::Base#calculate_app_secret_proof
42
46
  #
43
47
  # Returns a String describing the message ID if the message was sent,
44
48
  # or raises an exception if it was not.
45
- def deliver(message, access_token:)
49
+ def deliver(message, access_token:, app_secret_proof: nil)
50
+ query = {
51
+ access_token: access_token
52
+ }
53
+ query[:appsecret_proof] = app_secret_proof if app_secret_proof
46
54
  response = post '/messages',
47
55
  body: JSON.dump(message),
48
56
  format: :json,
49
- query: {
50
- access_token: access_token
51
- }
57
+ query: query
52
58
 
53
59
  Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response)
54
60
 
@@ -0,0 +1,16 @@
1
+ module Facebook
2
+ module Messenger
3
+ class Configuration
4
+ # We provide a service to calculate an app_secret_proof
5
+ class AppSecretProofCalculator
6
+ def self.call(app_secret, access_token)
7
+ OpenSSL::HMAC.hexdigest(
8
+ OpenSSL::Digest.new('SHA256'.freeze),
9
+ app_secret,
10
+ access_token
11
+ )
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,5 @@
1
+ require 'facebook/messenger/configuration/app_secret_proof_calculator'
2
+
1
3
  module Facebook
2
4
  module Messenger
3
5
  class Configuration
@@ -7,6 +9,16 @@ module Facebook
7
9
  # Be sure to implement all the functions as it raises
8
10
  # NotImplementedError errors.
9
11
  class Base
12
+ # A default caching implentation of generating the app_secret_proof
13
+ # for a given page_id
14
+ def app_secret_proof_for(page_id = nil)
15
+ return unless fetch_app_secret_proof_enabled?
16
+
17
+ memo_key = [app_secret_for(page_id), access_token_for(page_id)]
18
+ memoized_app_secret_proofs[memo_key] ||=
19
+ calculate_app_secret_proof(*memo_key)
20
+ end
21
+
10
22
  def valid_verify_token?(*)
11
23
  raise NotImplementedError
12
24
  end
@@ -18,6 +30,23 @@ module Facebook
18
30
  def access_token_for(*)
19
31
  raise NotImplementedError
20
32
  end
33
+
34
+ private
35
+
36
+ def calculate_app_secret_proof(app_secret, access_token)
37
+ Facebook::Messenger::Configuration::AppSecretProofCalculator.call(
38
+ app_secret,
39
+ access_token
40
+ )
41
+ end
42
+
43
+ def memoized_app_secret_proofs
44
+ @memoized_app_secret_proofs ||= {}
45
+ end
46
+
47
+ def fetch_app_secret_proof_enabled?
48
+ false
49
+ end
21
50
  end
22
51
  end
23
52
  end
@@ -1,9 +1,11 @@
1
+ require 'facebook/messenger/configuration/providers/base'
2
+
1
3
  module Facebook
2
4
  module Messenger
3
5
  class Configuration
4
6
  module Providers
5
7
  # The default configuration provider for environment variables.
6
- class Environment
8
+ class Environment < Base
7
9
  def valid_verify_token?(verify_token)
8
10
  verify_token == ENV['VERIFY_TOKEN']
9
11
  end
@@ -20,6 +22,10 @@ module Facebook
20
22
  def access_token_for(*)
21
23
  ENV['ACCESS_TOKEN']
22
24
  end
25
+
26
+ def fetch_app_secret_proof_enabled?
27
+ ENV['APP_SECRET_PROOF_ENABLED'] == 'true'.freeze
28
+ end
23
29
  end
24
30
  end
25
31
  end
@@ -74,7 +74,7 @@ module Facebook
74
74
  sender_action: 'typing_on'
75
75
  }
76
76
 
77
- Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
77
+ deliver_payload(payload)
78
78
  end
79
79
 
80
80
  #
@@ -90,7 +90,7 @@ module Facebook
90
90
  sender_action: 'typing_off'
91
91
  }
92
92
 
93
- Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
93
+ deliver_payload(payload)
94
94
  end
95
95
 
96
96
  #
@@ -106,7 +106,7 @@ module Facebook
106
106
  sender_action: 'mark_seen'
107
107
  }
108
108
 
109
- Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
109
+ deliver_payload(payload)
110
110
  end
111
111
 
112
112
  #
@@ -123,7 +123,7 @@ module Facebook
123
123
  message_type: Facebook::Messenger::Bot::MessageType::RESPONSE
124
124
  }
125
125
 
126
- Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
126
+ deliver_payload(payload)
127
127
  end
128
128
 
129
129
  #
@@ -134,6 +134,18 @@ module Facebook
134
134
  def access_token
135
135
  Facebook::Messenger.config.provider.access_token_for(recipient)
136
136
  end
137
+
138
+ def app_secret_proof
139
+ Facebook::Messenger.config.provider.app_secret_proof_for(recipient)
140
+ end
141
+
142
+ private
143
+
144
+ def deliver_payload(payload)
145
+ Facebook::Messenger::Bot.deliver(payload,
146
+ access_token: access_token,
147
+ app_secret_proof: app_secret_proof)
148
+ end
137
149
  end
138
150
  end
139
151
  end
@@ -15,6 +15,7 @@ module Facebook
15
15
  # Return hash containing the referral information of user.
16
16
  def referral
17
17
  return if @messaging['postback']['referral'].nil?
18
+
18
19
  @referral ||= Referral::Referral.new(
19
20
  @messaging['postback']['referral']
20
21
  )
@@ -27,6 +27,11 @@ module Facebook
27
27
  def type
28
28
  @referral['type']
29
29
  end
30
+
31
+ # Return String of ad id.
32
+ def ad_id
33
+ @referral['ad_id'] if @referral.key?('ad_id')
34
+ end
30
35
  end
31
36
 
32
37
  def referral
@@ -160,6 +160,7 @@ module Facebook
160
160
  # If the application has subscribed to webhooks other than Messenger,
161
161
  # 'messaging' won't be available and it is not relevant to us.
162
162
  next unless entry['messaging'.freeze]
163
+
163
164
  # Facebook may batch several items in the 'messaging' array during
164
165
  # periods of high load.
165
166
  entry['messaging'.freeze].each do |messaging|
@@ -2,6 +2,6 @@ module Facebook
2
2
  module Messenger
3
3
  #
4
4
  # @return [String] Define the version of gem.
5
- VERSION = '1.1.2'.freeze
5
+ VERSION = '1.2.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook-messenger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Gorset
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-06 00:00:00.000000000 Z
11
+ date: 2019-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -174,6 +174,7 @@ files:
174
174
  - lib/facebook/messenger/bot/messaging_type.rb
175
175
  - lib/facebook/messenger/bot/tag.rb
176
176
  - lib/facebook/messenger/configuration.rb
177
+ - lib/facebook/messenger/configuration/app_secret_proof_calculator.rb
177
178
  - lib/facebook/messenger/configuration/providers.rb
178
179
  - lib/facebook/messenger/configuration/providers/base.rb
179
180
  - lib/facebook/messenger/configuration/providers/environment.rb
@@ -216,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
217
  version: '0'
217
218
  requirements: []
218
219
  rubyforge_project:
219
- rubygems_version: 2.6.8
220
+ rubygems_version: 2.7.6
220
221
  signing_key:
221
222
  specification_version: 4
222
223
  summary: Facebook Messenger client