facebook-messenger 2.0.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b757052cad527d03939514250e2f3fc87fbc21e0e7c86e9f78c8a2087ed925f1
4
- data.tar.gz: 9f18c41169ded5554f49e3faa2e22a94565f83461bc2de688ad87957426791a1
3
+ metadata.gz: 4f6b7cdaf4658871a6b72a385cb5e2df054abac1849f809bd7e02f760ef09bb0
4
+ data.tar.gz: 53a3dc676588d46a339bbf1d4117addafc88b28ecb35a81e14c49e91e0124905
5
5
  SHA512:
6
- metadata.gz: 23d252fce2c080a7d95f1b25795ca423e7efac86c52e7dc158203c4f0b840d3b7c14b499919a3d9fab58cdf49e1bed274a7220f25b31c235e0e93fb457e45ff2
7
- data.tar.gz: 1aa9d963cfecb848d000a4df931a1d30430005eff5b5f58cc18f8094092956077b354e589527770007e081b3f8d0cf7a67f1229da21a3b1ce453ff94a7dbca65
6
+ metadata.gz: cc00a75c7a6bacdb963a5fd71747788676cdb23c35eab75bc13b5bf289708451a279ccdb444d69f86938caec99ab6c82581382c5853155f06e785dadb06cfb88
7
+ data.tar.gz: 90eba5323395d6085bf7d4e4729e3649a046a15b9fa37497df8cca4b99969bae7e4d6fb45481a958e5a3f4c750a60593cbbff1772bf4095c49689b9cc788a1ef
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="https://rawgit.com/jgorset/facebook-messenger/master/docs/conversation_with_logo.gif">
2
+ <img src="https://github.com/jgorset/facebook-messenger/blob/master/docs/conversation.gif?raw=true">
3
3
  </p>
4
4
 
5
5
 
@@ -24,9 +24,7 @@ You can reply to messages sent by the human:
24
24
  # bot.rb
25
25
  require 'facebook/messenger'
26
26
 
27
- include Facebook::Messenger
28
-
29
- Bot.on :message do |message|
27
+ Facebook::Messenger::Bot.on :message do |message|
30
28
  message.id # => 'mid.1457764197618:41d102a3e1ae206a38'
31
29
  message.sender # => { 'id' => '1008372609250235' }
32
30
  message.recipient # => { 'id' => '2015573629214912' }
@@ -42,29 +40,17 @@ end
42
40
  ... or even send the human messages out of the blue:
43
41
 
44
42
  ```ruby
45
- Bot.deliver({
43
+ Facebook::Messenger::Bot.deliver({
46
44
  recipient: {
47
- id: '45123'
45
+ id: YOUR_RECIPIENT_ID
48
46
  },
49
47
  message: {
50
48
  text: 'Human?'
51
49
  },
52
50
  messaging_type: Facebook::Messenger::Bot::MessagingType::UPDATE
53
- }, access_token: ENV['ACCESS_TOKEN'], app_secret_proof: app_secret_proof
54
- )
51
+ }, page_id: YOUR_PAGE_ID)
55
52
  ```
56
53
 
57
- NOTE: `app_secret_proof` is an optional parameter to [secure your requests](https://developers.facebook.com/docs/graph-api/securing-requests/),
58
- and you can generate it from your configuration provider like so:
59
-
60
- ```ruby
61
- configuration_provider = Facebook::Messenger::Configuration::Providers::Environment.new
62
- app_secret_proof = configuration_provider.app_secret_proof_for(page_id)
63
- ```
64
-
65
- For the methods you'll usually use (like `reply` and `typing_on`), the app secret proof will be set and sent
66
- automatically if you set the environment variable `APP_SECRET_PROOF_ENABLED` to `true`.
67
-
68
54
  ##### Messages with images
69
55
 
70
56
  The human may require visual aid to understand:
@@ -191,7 +177,7 @@ end
191
177
  You can keep track of messages sent to the human:
192
178
 
193
179
  ```ruby
194
- Bot.on :message_echo do |message_echo|
180
+ Facebook::Messenger::Bot.on :message_echo do |message_echo|
195
181
  message_echo.id # => 'mid.1457764197618:41d102a3e1ae206a38'
196
182
  message_echo.sender # => { 'id' => '1008372609250235' }
197
183
  message_echo.seq # => 73
@@ -270,7 +256,7 @@ When the human follows a m.me link with a ref parameter like http://m.me/mybot?r
270
256
  you will receive a `referral` event.
271
257
 
272
258
  ```ruby
273
- Bot.on :referral do |referral|
259
+ Facebook::Messenger::Bot.on :referral do |referral|
274
260
  referral.sender # => { 'id' => '1008372609250235' }
275
261
  referral.recipient # => { 'id' => '2015573629214912' }
276
262
  referral.sent_at # => 2016-04-22 21:30:36 +0200
@@ -283,7 +269,7 @@ end
283
269
  Another bot can pass a human to you:
284
270
 
285
271
  ```ruby
286
- Bot.on :pass_thread_control do |pass_thread_control|
272
+ Facebook::Messenger::Bot.on :pass_thread_control do |pass_thread_control|
287
273
  pass_thread_control.new_owner_app_id # => '123456789'
288
274
  pass_thread_control.metadata # => 'Additional content that the caller wants to set'
289
275
  end
@@ -305,7 +291,7 @@ Facebook::Messenger::Profile.set({
305
291
  text: 'Bienvenue dans le bot du Wagon !'
306
292
  }
307
293
  ]
308
- }, access_token: ENV['ACCESS_TOKEN'])
294
+ }, page_id: YOUR_PAGE_ID)
309
295
  ```
310
296
 
311
297
  You can define the action to trigger when new humans click on the Get
@@ -316,7 +302,7 @@ Facebook::Messenger::Profile.set({
316
302
  get_started: {
317
303
  payload: 'GET_STARTED_PAYLOAD'
318
304
  }
319
- }, access_token: ENV['ACCESS_TOKEN'])
305
+ }, page_id: YOUR_PAGE_ID)
320
306
  ```
321
307
 
322
308
  You can show a persistent menu to humans.
@@ -362,28 +348,30 @@ Facebook::Messenger::Profile.set({
362
348
  composer_input_disabled: false
363
349
  }
364
350
  ]
365
- }, access_token: ENV['ACCESS_TOKEN'])
351
+ }, page_id: YOUR_PAGE_ID)
366
352
  ```
367
353
 
368
-
369
354
  #### Handle a Facebook Policy Violation
370
355
 
371
356
  See Facebook's documentation on [Messaging Policy Enforcement](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_policy_enforcement)
372
357
 
373
358
  ```ruby
374
- Bot.on :'policy_enforcement' do |referral|
359
+ Facebook::Messenger::Bot.on :'policy_enforcement' do |referral|
375
360
  referral.action # => 'block'
376
361
  referral.reason # => "The bot violated our Platform Policies (https://developers.facebook.com/policy/#messengerplatform). Common violations include sending out excessive spammy messages or being non-functional."
377
362
  end
378
363
  ```
364
+
379
365
  #### messaging_type
366
+
380
367
  ##### Sending Messages
368
+
381
369
  See Facebook's documentation on [Sending Messages](https://developers.facebook.com/docs/messenger-platform/send-messages#standard_messaging)
382
370
 
383
371
  As of May 7th 2018 all messages are required to include a messaging_type
384
372
 
385
373
  ```ruby
386
- Bot.deliver({
374
+ Facebook::Messenger::Bot.deliver({
387
375
  recipient: {
388
376
  id: '45123'
389
377
  },
@@ -391,16 +379,17 @@ Bot.deliver({
391
379
  text: 'Human?'
392
380
  },
393
381
  messaging_type: Facebook::Messenger::Bot::MessagingType::UPDATE
394
- }, access_token: ENV['ACCESS_TOKEN'])
382
+ }, page_id: YOUR_PAGE_ID)
395
383
  ```
396
384
 
397
385
  ##### MESSAGE_TAG
386
+
398
387
  See Facebook's documentation on [Message Tags](https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags)
399
388
 
400
389
  When sending a message with messaging_type: MESSAGE_TAG (Facebook::Messenger::Bot::MessagingType::MESSAGE_TAG) you must ensure you add a tag: parameter
401
390
 
402
391
  ```ruby
403
- Bot.deliver({
392
+ Facebook::Messenger::Bot.deliver({
404
393
  recipient: {
405
394
  id: '45123'
406
395
  },
@@ -409,7 +398,7 @@ Bot.deliver({
409
398
  },
410
399
  messaging_type: Facebook::Messenger::Bot::MessagingType::MESSAGE_TAG
411
400
  tag: Facebook::Messenger::Bot::Tag::NON_PROMOTIONAL_SUBSCRIPTION
412
- }, access_token: ENV['ACCESS_TOKEN'])
401
+ }, page_id: YOUR_PAGE_ID)
413
402
  ```
414
403
 
415
404
  ## Configuration
@@ -545,7 +534,7 @@ We suggest that you put your bot code in `app/bot`.
545
534
 
546
535
  include Facebook::Messenger
547
536
 
548
- Bot.on :message do |message|
537
+ Faceboook::Messenger::Bot.on :message do |message|
549
538
  message.reply(text: 'Hello, human!')
550
539
  end
551
540
  ```
@@ -582,6 +571,7 @@ config.autoload_paths += Dir[Rails.root.join('app', 'bot', '*')]
582
571
  ### Test it...
583
572
 
584
573
  ##### ...locally
574
+
585
575
  To test your locally running bot, you can use [ngrok]. This will create a secure
586
576
  tunnel to localhost so that Facebook can reach the webhook.
587
577
 
@@ -589,7 +579,8 @@ tunnel to localhost so that Facebook can reach the webhook.
589
579
 
590
580
  In order to test that behaviour when a new event from Facebook is registered, you can use the gem's `trigger` method. This method accepts as its first argument the type of event that it will receive, and can then be followed by other arguments that mock objects received from Messenger. Using Ruby's [Struct](https://ruby-doc.org/core-2.5.0/Struct.html) class can be very useful for creating these mock objects.
591
581
 
592
- In this case, subscribing to Messenger events has been extracted into a `Listener` class.
582
+ In this case, subscribing to Messenger events has been extracted into a `Listener` class.
583
+
593
584
  ```ruby
594
585
  # app/bot/listener.rb
595
586
  require 'facebook/messenger'
@@ -598,7 +589,7 @@ include Facebook::Messenger
598
589
 
599
590
  class Listener
600
591
  Facebook::Messenger::Subscriptions.subscribe(
601
- access_token: ENV["FB_ACCESS_TOKEN"],
592
+ access_token: ENV['ACCESS_TOKEN'],
602
593
  subscribed_fields: %w[feed mention name]
603
594
  )
604
595
 
@@ -608,11 +599,13 @@ class Listener
608
599
  message: {
609
600
  text: 'Uploading your message to skynet.'
610
601
  }
611
- }, access_token: ENV['FB_ACCESS_TOKEN'])
602
+ }, access_token: ENV['ACCESS_TOKEN'])
612
603
  end
613
604
  end
614
605
  ```
606
+
615
607
  Its respective test file then ensures that the `Bot` object receives a call to `deliver`. This is just a basic test, but check out the [RSpec docs](http://rspec.info/) for more information on testing with RSpec.
608
+
616
609
  ```ruby
617
610
  require 'rails_helper'
618
611
 
@@ -638,7 +631,6 @@ RSpec.describe Listener do
638
631
  end
639
632
  ```
640
633
 
641
-
642
634
  ## Development
643
635
 
644
636
  After checking out the repo, run `bin/setup` to install dependencies. You can also run
@@ -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/v3.2/me'
16
+ base_uri 'https://graph.facebook.com/v20.0'
17
17
 
18
18
  #
19
19
  # @return [Array] Array containing the supported webhook events.
@@ -31,6 +31,8 @@ module Facebook
31
31
  pass_thread_control
32
32
  game_play
33
33
  reaction
34
+ feed
35
+ leadgen
34
36
  ].freeze
35
37
 
36
38
  class << self
@@ -40,19 +42,47 @@ module Facebook
40
42
  # @raise [Facebook::Messenger::Bot::SendError] if there is any error
41
43
  # in response while sending message.
42
44
  #
43
- # @param [Hash] message A Hash describing the recipient and the message.
44
- # @param [String] page_id A String describing the Facebook Page ID to send the message from.
45
+ # @param [Hash] message The message payload
46
+ # @param [String] page_id The page to send the message from
45
47
  #
46
48
  # Returns a String describing the message ID if the message was sent,
47
49
  # or raises an exception if it was not.
48
50
  def deliver(message, page_id:)
49
- access_token = Facebook::Messenger.config.provider.access_token_for(page_id)
50
- app_secret_proof = Facebook::Messenger.config.provider.app_secret_proof_for(page_id)
51
+ access_token = config.provider.access_token_for(page_id)
52
+ app_secret_proof = config.provider.app_secret_proof_for(page_id)
51
53
 
52
54
  query = { access_token: access_token }
53
55
  query[:appsecret_proof] = app_secret_proof if app_secret_proof
54
56
 
55
- response = post '/messages',
57
+ response = post '/me/messages',
58
+ body: JSON.dump(message),
59
+ format: :json,
60
+ query: query
61
+
62
+ Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response)
63
+
64
+ response.body
65
+ end
66
+
67
+ # Reply to a Facebook comment.
68
+ # @see https://developers.facebook.com/docs/graph-api/reference/v20.0/object/comments
69
+ #
70
+ # @raise [Facebook::Messenger::Bot::SendError] if there is any error
71
+ # in response while sending the comment.
72
+ #
73
+ # @param [Hash] comment The comment payload
74
+ # @param [String] page_id The page ID to send the comment from
75
+ #
76
+ # Returns a Hash describing the API response if the comment was sent,
77
+ # or raises an exception if it was not.
78
+ def reply_to_comment(comment_id, message, page_id:)
79
+ access_token = config.provider.access_token_for(page_id)
80
+ app_secret_proof = config.provider.app_secret_proof_for(page_id)
81
+
82
+ query = { access_token: access_token }
83
+ query[:appsecret_proof] = app_secret_proof if app_secret_proof
84
+
85
+ response = post "/#{comment_id}/comments",
56
86
  body: JSON.dump(message),
57
87
  format: :json,
58
88
  query: query
@@ -138,6 +168,12 @@ module Facebook
138
168
  }
139
169
  )
140
170
  end
171
+
172
+ private
173
+
174
+ def config
175
+ Facebook::Messenger.config
176
+ end
141
177
  end
142
178
  end
143
179
  end
@@ -12,8 +12,6 @@ module Facebook
12
12
  # A default caching implentation of generating the app_secret_proof
13
13
  # for a given page_id
14
14
  def app_secret_proof_for(page_id = nil)
15
- return unless fetch_app_secret_proof_enabled?
16
-
17
15
  memo_key = [app_secret_for(page_id), access_token_for(page_id)]
18
16
  memoized_app_secret_proofs[memo_key] ||=
19
17
  calculate_app_secret_proof(*memo_key)
@@ -43,10 +41,6 @@ module Facebook
43
41
  def memoized_app_secret_proofs
44
42
  @memoized_app_secret_proofs ||= {}
45
43
  end
46
-
47
- def fetch_app_secret_proof_enabled?
48
- false
49
- end
50
44
  end
51
45
  end
52
46
  end
@@ -22,10 +22,6 @@ module Facebook
22
22
  def access_token_for(*)
23
23
  ENV['ACCESS_TOKEN']
24
24
  end
25
-
26
- def fetch_app_secret_proof_enabled?
27
- ENV['APP_SECRET_PROOF_ENABLED'] == 'true'.freeze
28
- end
29
25
  end
30
26
  end
31
27
  end
@@ -0,0 +1,16 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ class Feed
5
+ include Facebook::Messenger::Incoming::FeedCommon
6
+
7
+ def id
8
+ @change['value']['post_id']
9
+ end
10
+ def message
11
+ @change['value']['message']
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ module FeedCommon
5
+ attr_reader :change
6
+
7
+ def initialize(change)
8
+ @change = change
9
+ end
10
+
11
+ def item
12
+ @change['item']
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ class Leadgen
5
+ include Facebook::Messenger::Incoming::FeedCommon
6
+
7
+ def id
8
+ @change['value']['leadgen_id']
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -13,6 +13,9 @@ require 'facebook/messenger/incoming/policy_enforcement'
13
13
  require 'facebook/messenger/incoming/pass_thread_control'
14
14
  require 'facebook/messenger/incoming/game_play'
15
15
  require 'facebook/messenger/incoming/message_reaction'
16
+ require 'facebook/messenger/incoming/feed_common'
17
+ require 'facebook/messenger/incoming/feed'
18
+ require 'facebook/messenger/incoming/leadgen'
16
19
 
17
20
  module Facebook
18
21
  module Messenger
@@ -37,7 +40,9 @@ module Facebook
37
40
  'policy_enforcement' => PolicyEnforcement,
38
41
  'pass_thread_control' => PassThreadControl,
39
42
  'game_play' => GamePlay,
40
- 'reaction' => MessageReaction
43
+ 'reaction' => MessageReaction,
44
+ 'feed' => Feed,
45
+ 'leadgen' => Leadgen,
41
46
  }.freeze
42
47
 
43
48
  # Parse the given payload and create new object of class related
@@ -54,7 +59,7 @@ module Facebook
54
59
  return MessageEcho.new(payload) if payload_is_echo?(payload)
55
60
 
56
61
  EVENTS.each do |event, klass|
57
- return klass.new(payload) if payload.key?(event)
62
+ return klass.new(payload) if payload.key?(event) || payload['field'] == event
58
63
  end
59
64
 
60
65
  raise UnknownPayload, payload
@@ -10,7 +10,7 @@ module Facebook
10
10
  include HTTParty
11
11
 
12
12
  # Define base_uri for HTTParty.
13
- base_uri 'https://graph.facebook.com/v3.2/me'
13
+ base_uri 'https://graph.facebook.com/v20.0/me'
14
14
 
15
15
  format :json
16
16
 
@@ -144,8 +144,8 @@ module Facebook
144
144
  #
145
145
  def parsed_body
146
146
  @parsed_body ||= JSON.parse(body)
147
- rescue JSON::ParserError
148
- raise BadRequestError, 'Error parsing request body format'
147
+ rescue JSON::ParserError => error
148
+ raise BadRequestError, error
149
149
  end
150
150
 
151
151
  #
@@ -157,15 +157,26 @@ module Facebook
157
157
  # Facebook may batch several items in the 'entry' array during
158
158
  # periods of high load.
159
159
  events['entry'.freeze].each do |entry|
160
- # If the application has subscribed to webhooks other than Messenger,
161
- # 'messaging' won't be available and it is not relevant to us.
162
- next unless entry['messaging'.freeze]
163
160
 
164
161
  # Facebook may batch several items in the 'messaging' array during
165
162
  # periods of high load.
166
- entry['messaging'.freeze].each do |messaging|
167
- Facebook::Messenger::Bot.receive(messaging)
163
+ if entry['messaging'.freeze]
164
+ entry['messaging'.freeze].each do |messaging|
165
+ Facebook::Messenger::Bot.receive(messaging)
166
+ end
168
167
  end
168
+ if entry['changes'.freeze]
169
+ entry['changes'.freeze].each do |change|
170
+ Facebook::Messenger::Bot.receive(change)
171
+ end
172
+ end
173
+ # d99d
174
+ if entry['standby'.freeze]
175
+ entry['standby'.freeze].each do |messaging|
176
+ Facebook::Messenger::Bot.receive(messaging)
177
+ end
178
+ end
179
+ # .
169
180
  end
170
181
  end
171
182
 
@@ -9,7 +9,7 @@ module Facebook
9
9
  module Subscriptions
10
10
  include HTTParty
11
11
 
12
- base_uri 'https://graph.facebook.com/v3.2/me'
12
+ base_uri 'https://graph.facebook.com/v20.0/me'
13
13
 
14
14
  format :json
15
15
 
@@ -2,6 +2,6 @@ module Facebook
2
2
  module Messenger
3
3
  #
4
4
  # @return [String] Define the version of gem.
5
- VERSION = '2.0.0'.freeze
5
+ VERSION = '2.1.2'.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: 2.0.0
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Gorset
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-31 00:00:00.000000000 Z
11
+ date: 2025-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: 2.1.4
53
+ version: '2.1'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: 2.1.4
60
+ version: '2.1'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: coveralls
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -183,7 +183,10 @@ files:
183
183
  - lib/facebook/messenger/incoming/account_linking.rb
184
184
  - lib/facebook/messenger/incoming/common.rb
185
185
  - lib/facebook/messenger/incoming/delivery.rb
186
+ - lib/facebook/messenger/incoming/feed.rb
187
+ - lib/facebook/messenger/incoming/feed_common.rb
186
188
  - lib/facebook/messenger/incoming/game_play.rb
189
+ - lib/facebook/messenger/incoming/leadgen.rb
187
190
  - lib/facebook/messenger/incoming/message.rb
188
191
  - lib/facebook/messenger/incoming/message_echo.rb
189
192
  - lib/facebook/messenger/incoming/message_reaction.rb
@@ -219,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
222
  - !ruby/object:Gem::Version
220
223
  version: '0'
221
224
  requirements: []
222
- rubygems_version: 3.0.6
225
+ rubygems_version: 3.0.3.1
223
226
  signing_key:
224
227
  specification_version: 4
225
228
  summary: Facebook Messenger client