facebook-messenger 2.0.0 → 2.0.1
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
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b91271ca1dfd46644af4c45a6e64670c36cf2c8de5efa288ec2bf04e43bef44
|
4
|
+
data.tar.gz: 57611671a09c7784c4cfe92e461a9b9444e6d18e3f1ee02784291f5af2effaa1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c91d9b3fc8bf84c8acf14034f2f916477e67418b8baf7df1c6f87fb35f0e4a5f3d207a1386e33a70ed18b8ad1466023e93d50eb248a7c110b05c460f0154450f
|
7
|
+
data.tar.gz: aa8656efd52e1cb39980c11a86596739e1045848bb78edd13c6a568febaa1ed60f8b2ede932baa8f3f27917506f64ade3cbe2690be5892be555a7c83aaa61d4a
|
data/README.md
CHANGED
@@ -44,27 +44,15 @@ end
|
|
44
44
|
```ruby
|
45
45
|
Bot.deliver({
|
46
46
|
recipient: {
|
47
|
-
id:
|
47
|
+
id: YOUR_RECIPIENT_ID
|
48
48
|
},
|
49
49
|
message: {
|
50
50
|
text: 'Human?'
|
51
51
|
},
|
52
52
|
messaging_type: Facebook::Messenger::Bot::MessagingType::UPDATE
|
53
|
-
},
|
54
|
-
)
|
53
|
+
}, page_id: YOUR_PAGE_ID)
|
55
54
|
```
|
56
55
|
|
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
56
|
##### Messages with images
|
69
57
|
|
70
58
|
The human may require visual aid to understand:
|
@@ -305,7 +293,7 @@ Facebook::Messenger::Profile.set({
|
|
305
293
|
text: 'Bienvenue dans le bot du Wagon !'
|
306
294
|
}
|
307
295
|
]
|
308
|
-
},
|
296
|
+
}, page_id: YOUR_PAGE_ID)
|
309
297
|
```
|
310
298
|
|
311
299
|
You can define the action to trigger when new humans click on the Get
|
@@ -316,7 +304,7 @@ Facebook::Messenger::Profile.set({
|
|
316
304
|
get_started: {
|
317
305
|
payload: 'GET_STARTED_PAYLOAD'
|
318
306
|
}
|
319
|
-
},
|
307
|
+
}, page_id: YOUR_PAGE_ID)
|
320
308
|
```
|
321
309
|
|
322
310
|
You can show a persistent menu to humans.
|
@@ -362,10 +350,9 @@ Facebook::Messenger::Profile.set({
|
|
362
350
|
composer_input_disabled: false
|
363
351
|
}
|
364
352
|
]
|
365
|
-
},
|
353
|
+
}, page_id: YOUR_PAGE_ID)
|
366
354
|
```
|
367
355
|
|
368
|
-
|
369
356
|
#### Handle a Facebook Policy Violation
|
370
357
|
|
371
358
|
See Facebook's documentation on [Messaging Policy Enforcement](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_policy_enforcement)
|
@@ -376,8 +363,11 @@ Bot.on :'policy_enforcement' do |referral|
|
|
376
363
|
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
364
|
end
|
378
365
|
```
|
366
|
+
|
379
367
|
#### messaging_type
|
368
|
+
|
380
369
|
##### Sending Messages
|
370
|
+
|
381
371
|
See Facebook's documentation on [Sending Messages](https://developers.facebook.com/docs/messenger-platform/send-messages#standard_messaging)
|
382
372
|
|
383
373
|
As of May 7th 2018 all messages are required to include a messaging_type
|
@@ -391,10 +381,11 @@ Bot.deliver({
|
|
391
381
|
text: 'Human?'
|
392
382
|
},
|
393
383
|
messaging_type: Facebook::Messenger::Bot::MessagingType::UPDATE
|
394
|
-
},
|
384
|
+
}, page_id: YOUR_PAGE_ID)
|
395
385
|
```
|
396
386
|
|
397
387
|
##### MESSAGE_TAG
|
388
|
+
|
398
389
|
See Facebook's documentation on [Message Tags](https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags)
|
399
390
|
|
400
391
|
When sending a message with messaging_type: MESSAGE_TAG (Facebook::Messenger::Bot::MessagingType::MESSAGE_TAG) you must ensure you add a tag: parameter
|
@@ -409,7 +400,7 @@ Bot.deliver({
|
|
409
400
|
},
|
410
401
|
messaging_type: Facebook::Messenger::Bot::MessagingType::MESSAGE_TAG
|
411
402
|
tag: Facebook::Messenger::Bot::Tag::NON_PROMOTIONAL_SUBSCRIPTION
|
412
|
-
},
|
403
|
+
}, page_id: YOUR_PAGE_ID)
|
413
404
|
```
|
414
405
|
|
415
406
|
## Configuration
|
@@ -582,6 +573,7 @@ config.autoload_paths += Dir[Rails.root.join('app', 'bot', '*')]
|
|
582
573
|
### Test it...
|
583
574
|
|
584
575
|
##### ...locally
|
576
|
+
|
585
577
|
To test your locally running bot, you can use [ngrok]. This will create a secure
|
586
578
|
tunnel to localhost so that Facebook can reach the webhook.
|
587
579
|
|
@@ -589,7 +581,8 @@ tunnel to localhost so that Facebook can reach the webhook.
|
|
589
581
|
|
590
582
|
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
583
|
|
592
|
-
In this case, subscribing to Messenger events has been extracted into a `Listener` class.
|
584
|
+
In this case, subscribing to Messenger events has been extracted into a `Listener` class.
|
585
|
+
|
593
586
|
```ruby
|
594
587
|
# app/bot/listener.rb
|
595
588
|
require 'facebook/messenger'
|
@@ -598,7 +591,7 @@ include Facebook::Messenger
|
|
598
591
|
|
599
592
|
class Listener
|
600
593
|
Facebook::Messenger::Subscriptions.subscribe(
|
601
|
-
access_token: ENV[
|
594
|
+
access_token: ENV['ACCESS_TOKEN'],
|
602
595
|
subscribed_fields: %w[feed mention name]
|
603
596
|
)
|
604
597
|
|
@@ -608,11 +601,13 @@ class Listener
|
|
608
601
|
message: {
|
609
602
|
text: 'Uploading your message to skynet.'
|
610
603
|
}
|
611
|
-
}, access_token: ENV['
|
604
|
+
}, access_token: ENV['ACCESS_TOKEN'])
|
612
605
|
end
|
613
606
|
end
|
614
607
|
```
|
608
|
+
|
615
609
|
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.
|
610
|
+
|
616
611
|
```ruby
|
617
612
|
require 'rails_helper'
|
618
613
|
|
@@ -638,7 +633,6 @@ RSpec.describe Listener do
|
|
638
633
|
end
|
639
634
|
```
|
640
635
|
|
641
|
-
|
642
636
|
## Development
|
643
637
|
|
644
638
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run
|
@@ -40,14 +40,14 @@ module Facebook
|
|
40
40
|
# @raise [Facebook::Messenger::Bot::SendError] if there is any error
|
41
41
|
# in response while sending message.
|
42
42
|
#
|
43
|
-
# @param [Hash] message
|
44
|
-
# @param [String] page_id
|
43
|
+
# @param [Hash] message The message payload
|
44
|
+
# @param [String] page_id The page to send the message from
|
45
45
|
#
|
46
46
|
# Returns a String describing the message ID if the message was sent,
|
47
47
|
# or raises an exception if it was not.
|
48
48
|
def deliver(message, page_id:)
|
49
|
-
access_token =
|
50
|
-
app_secret_proof =
|
49
|
+
access_token = config.provider.access_token_for(page_id)
|
50
|
+
app_secret_proof = config.provider.app_secret_proof_for(page_id)
|
51
51
|
|
52
52
|
query = { access_token: access_token }
|
53
53
|
query[:appsecret_proof] = app_secret_proof if app_secret_proof
|
@@ -138,6 +138,12 @@ module Facebook
|
|
138
138
|
}
|
139
139
|
)
|
140
140
|
end
|
141
|
+
|
142
|
+
private
|
143
|
+
|
144
|
+
def config
|
145
|
+
Facebook::Messenger.config
|
146
|
+
end
|
141
147
|
end
|
142
148
|
end
|
143
149
|
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
|