meta-messenger 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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +681 -0
  3. data/bin/console +14 -0
  4. data/bin/setup +8 -0
  5. data/lib/facebook/messenger/bot/error_parser.rb +111 -0
  6. data/lib/facebook/messenger/bot/exceptions.rb +15 -0
  7. data/lib/facebook/messenger/bot/message_type.rb +13 -0
  8. data/lib/facebook/messenger/bot/messaging_type.rb +12 -0
  9. data/lib/facebook/messenger/bot/tag.rb +27 -0
  10. data/lib/facebook/messenger/bot.rb +180 -0
  11. data/lib/facebook/messenger/configuration/app_secret_proof_calculator.rb +16 -0
  12. data/lib/facebook/messenger/configuration/providers/base.rb +48 -0
  13. data/lib/facebook/messenger/configuration/providers/environment.rb +29 -0
  14. data/lib/facebook/messenger/configuration/providers.rb +13 -0
  15. data/lib/facebook/messenger/configuration.rb +12 -0
  16. data/lib/facebook/messenger/error.rb +44 -0
  17. data/lib/facebook/messenger/incoming/account_linking.rb +28 -0
  18. data/lib/facebook/messenger/incoming/common.rb +131 -0
  19. data/lib/facebook/messenger/incoming/delivery.rb +23 -0
  20. data/lib/facebook/messenger/incoming/feed.rb +16 -0
  21. data/lib/facebook/messenger/incoming/feed_common.rb +17 -0
  22. data/lib/facebook/messenger/incoming/game_play.rb +39 -0
  23. data/lib/facebook/messenger/incoming/leadgen.rb +13 -0
  24. data/lib/facebook/messenger/incoming/message.rb +159 -0
  25. data/lib/facebook/messenger/incoming/message_echo.rb +10 -0
  26. data/lib/facebook/messenger/incoming/message_reaction.rb +23 -0
  27. data/lib/facebook/messenger/incoming/message_request.rb +13 -0
  28. data/lib/facebook/messenger/incoming/optin.rb +34 -0
  29. data/lib/facebook/messenger/incoming/pass_thread_control.rb +22 -0
  30. data/lib/facebook/messenger/incoming/payment.rb +49 -0
  31. data/lib/facebook/messenger/incoming/policy_enforcement.rb +21 -0
  32. data/lib/facebook/messenger/incoming/postback.rb +26 -0
  33. data/lib/facebook/messenger/incoming/read.rb +21 -0
  34. data/lib/facebook/messenger/incoming/referral.rb +47 -0
  35. data/lib/facebook/messenger/incoming.rb +85 -0
  36. data/lib/facebook/messenger/profile.rb +92 -0
  37. data/lib/facebook/messenger/server.rb +195 -0
  38. data/lib/facebook/messenger/server_no_error.rb +36 -0
  39. data/lib/facebook/messenger/subscriptions.rb +85 -0
  40. data/lib/facebook/messenger/version.rb +7 -0
  41. data/lib/facebook/messenger.rb +32 -0
  42. metadata +229 -0
@@ -0,0 +1,23 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Delivery class represents the receipt of a delivered message.
5
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-deliveries
6
+ class Delivery
7
+ include Facebook::Messenger::Incoming::Common
8
+
9
+ def ids
10
+ @messaging['delivery']['mids']
11
+ end
12
+
13
+ def at
14
+ Time.at(@messaging['delivery']['watermark'] / 1000)
15
+ end
16
+
17
+ def seq
18
+ @messaging['delivery']['seq']
19
+ end
20
+ end
21
+ end
22
+ end
23
+ 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,39 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The GamePlay class represents an incoming Facebook Messenger
5
+ # game_play events.
6
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_game_plays
7
+ class GamePlay
8
+ include Facebook::Messenger::Incoming::Common
9
+
10
+ def game_play
11
+ @messaging['game_play']
12
+ end
13
+
14
+ def payload
15
+ game_play['payload']
16
+ end
17
+
18
+ def score
19
+ game_play['score']
20
+ end
21
+
22
+ def game
23
+ game_play['game_id']
24
+ end
25
+
26
+ def player
27
+ game_play['player_id']
28
+ end
29
+
30
+ def context
31
+ {
32
+ context_id: game_play['context_id'],
33
+ context_type: game_play['context_type']
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ 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
@@ -0,0 +1,159 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ #
5
+ # Message class represents an incoming Facebook Messenger message event.
6
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages
7
+ #
8
+ class Message
9
+ include Facebook::Messenger::Incoming::Common
10
+
11
+ #
12
+ # @return [Array] Supported attachments for message.
13
+ ATTACHMENT_TYPES = %w[image audio video file location fallback].freeze
14
+
15
+ #
16
+ # Function returns unique id of message
17
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages
18
+ # Info about received message format.
19
+ #
20
+ # @return [String] Unique id of message.
21
+ #
22
+ def id
23
+ @messaging['message']['mid']
24
+ end
25
+
26
+ def seq
27
+ @messaging['message']['seq']
28
+ end
29
+
30
+ #
31
+ # Function returns text of message
32
+ #
33
+ # @return [String] Text of message.
34
+ #
35
+ def text
36
+ @messaging['message']['text']
37
+ end
38
+
39
+ #
40
+ # Whether message is echo or not?
41
+ #
42
+ # @return [Boolean] If message is echo return true else false.
43
+ #
44
+ def echo?
45
+ @messaging['message']['is_echo']
46
+ end
47
+
48
+ #
49
+ # Function returns array containing attachment data
50
+ # @see https://developers.facebook.com/docs/messenger-platform/send-messages#sending_attachments
51
+ # More info about attachments.
52
+ #
53
+ # @return [Array] Attachment data.
54
+ #
55
+ def attachments
56
+ @messaging['message']['attachments']
57
+ end
58
+
59
+ #
60
+ # If facebook messenger built-in NLP is enabled, message will
61
+ # contain 'nlp' key in response.
62
+ # @see https://developers.facebook.com/docs/messenger-platform/built-in-nlp
63
+ # More information about built-in NLP.
64
+ #
65
+ #
66
+ # @return [Hash] NLP information about message.
67
+ #
68
+ def nlp
69
+ @messaging['message']['nlp']
70
+ end
71
+
72
+ #
73
+ # Function return app id from message.
74
+ #
75
+ # @return [String] App ID.
76
+ #
77
+ def app_id
78
+ @messaging['message']['app_id']
79
+ end
80
+
81
+ #
82
+ # This meta programming defines function for
83
+ # every attachment type to check whether the attachment
84
+ # in message is of defined type or not.
85
+ #
86
+ ATTACHMENT_TYPES.each do |attachment_type|
87
+ define_method "#{attachment_type}_attachment?" do
88
+ attachment_type?(attachment_type)
89
+ end
90
+ end
91
+
92
+ #
93
+ # Get the type of attachment in message.
94
+ #
95
+ # @return [String] Attachment type.
96
+ #
97
+ def attachment_type
98
+ return if attachments.nil?
99
+
100
+ attachments.first['type']
101
+ end
102
+
103
+ #
104
+ # Get the URL of attachment in message.
105
+ # URL is only available for attachments of type image/audio/video/file.
106
+ #
107
+ # @return [String] URL of attachment.
108
+ #
109
+ def attachment_url
110
+ return if attachments.nil?
111
+ return unless %w[image audio video file].include? attachment_type
112
+
113
+ attachments.first['payload']['url']
114
+ end
115
+
116
+ #
117
+ # Get the location coordinates if attachment type is 'location'.
118
+ # @example [LATITUDE, LONGITUDE]
119
+ #
120
+ # @return [Array] Location coordinates.
121
+ #
122
+ def location_coordinates
123
+ return [] unless attachment_type?('location')
124
+
125
+ coordinates_data = attachments.first['payload']['coordinates']
126
+ [coordinates_data['lat'], coordinates_data['long']]
127
+ end
128
+
129
+ #
130
+ # Get the payload of quick reply.
131
+ # @see https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
132
+ # More info about quick reply.
133
+ #
134
+ # @return [String] Payload string.
135
+ #
136
+ def quick_reply
137
+ return unless @messaging['message']['quick_reply']
138
+
139
+ @messaging['message']['quick_reply']['payload']
140
+ end
141
+
142
+ # @private
143
+ private
144
+
145
+ #
146
+ # Check if attachment in message is of given type or not?
147
+ #
148
+ # @param [String] attachment_type Attachment type
149
+ #
150
+ # @return [Boolean] If type of attachment in message
151
+ # and provided attachment type are same then return true else false.
152
+ #
153
+ def attachment_type?(attachment_type)
154
+ !attachments.nil? && attachments.first['type'] == attachment_type
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,10 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Message echo class represents an incoming Facebook Messenger message
5
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-echoes
6
+ class MessageEcho < Message
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Message echo class represents an incoming Facebook Messenger message
5
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-reactions
6
+ class MessageReaction
7
+ include Facebook::Messenger::Incoming::Common
8
+
9
+ def action
10
+ @messaging['reaction']['action']
11
+ end
12
+
13
+ def emoji
14
+ @messaging['reaction']['emoji']
15
+ end
16
+
17
+ def reaction
18
+ @messaging['reaction']['reaction']
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Message request class represents an
5
+ # incoming Facebook Messenger message request accepted by the user
6
+ class MessageRequest < Message
7
+ def accept?
8
+ @messaging['message_request'] == 'accept'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ #
5
+ # The Optin class represents an incoming Facebook Messenger optin,
6
+ # which occurs when a user engages by using the Send-to-Messenger Plugin.
7
+ #
8
+ # @see https://developers.facebook.com/docs/messenger-platform/plugin-reference
9
+ #
10
+ class Optin
11
+ include Facebook::Messenger::Incoming::Common
12
+
13
+ #
14
+ # Function returns 'data-ref' attribute that was defined
15
+ # with the entry point.
16
+ #
17
+ # @return [String] data-ref attribute.
18
+ #
19
+ def ref
20
+ @messaging['optin']['ref']
21
+ end
22
+
23
+ #
24
+ # Function returns 'user_ref' attribute defined in checkbox plugin.
25
+ #
26
+ # @return [String] user-ref attribute.
27
+ #
28
+ def user_ref
29
+ @messaging['optin']['user_ref']
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The PassThreadControl class represents an incoming Facebook Messenger
5
+ # pass thread control event.
6
+ #
7
+ # @see https://developers.facebook.com/docs/messenger-platform/handover-protocol/pass-thread-control
8
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/handover-protocol/pass-thread-control
9
+ class PassThreadControl
10
+ include Facebook::Messenger::Incoming::Common
11
+
12
+ def new_owner_app_id
13
+ @messaging['pass_thread_control']['new_owner_app_id']
14
+ end
15
+
16
+ def metadata
17
+ @messaging['pass_thread_control']['metadata']
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Payment class represents a successful purchase using the Buy Button
5
+ #
6
+ # https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/payment
7
+ class Payment
8
+ include Facebook::Messenger::Incoming::Common
9
+
10
+ # The payment portion of the payload.
11
+ class Payment
12
+ def initialize(payment)
13
+ @payment = payment
14
+ end
15
+
16
+ # Return String containing developer defined payload.
17
+ def payload
18
+ @payment['payload']
19
+ end
20
+
21
+ # Return hash containing the requested information from user when they
22
+ # click buy button.
23
+ def user_info
24
+ @payment['requested_user_info']
25
+ end
26
+
27
+ # Return hash containing the payment credential information.
28
+ def payment_credential
29
+ @payment['payment_credential']
30
+ end
31
+
32
+ # Return hash containing the information about amount of purchase.
33
+ def amount
34
+ @payment['amount']
35
+ end
36
+
37
+ # Return string containing option_id of selected shipping option.
38
+ def shipping_option_id
39
+ @payment['shipping_option_id']
40
+ end
41
+ end
42
+
43
+ def payment
44
+ @payment ||= Payment.new(@messaging['payment'])
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The PolicyEnforcement class represents an incoming webhook response from
5
+ # Facebook when they are notifying your app of a policy violation
6
+ #
7
+ # https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_policy_enforcement
8
+ class PolicyEnforcement
9
+ include Facebook::Messenger::Incoming::Common
10
+
11
+ def action
12
+ @messaging['policy_enforcement']['action']
13
+ end
14
+
15
+ def reason
16
+ @messaging['policy_enforcement']['reason']
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Postback class represents an incoming Facebook Messenger
5
+ # postback events.
6
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_postbacks
7
+ class Postback
8
+ include Facebook::Messenger::Incoming::Common
9
+
10
+ # Return String of developer defined payload.
11
+ def payload
12
+ @messaging['postback']['payload']
13
+ end
14
+
15
+ # Return hash containing the referral information of user.
16
+ def referral
17
+ return if @messaging['postback']['referral'].nil?
18
+
19
+ @referral ||= Referral::Referral.new(
20
+ @messaging['postback']['referral']
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # The Read class represents the user reading a delivered message.
5
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-reads
6
+ class Read
7
+ include Facebook::Messenger::Incoming::Common
8
+
9
+ # Return time object when message is read by user.
10
+ def at
11
+ Time.at(@messaging['read']['watermark'] / 1000)
12
+ end
13
+
14
+ # Return Integer defining the sequence number of message.
15
+ def seq
16
+ @messaging['read']['seq']
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ module Facebook
2
+ module Messenger
3
+ module Incoming
4
+ # Referral class represents an incoming Facebook Messenger referral event.
5
+ #
6
+ # @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_referrals
7
+ class Referral
8
+ include Facebook::Messenger::Incoming::Common
9
+
10
+ # The referral portion of the payload.
11
+ class Referral
12
+ def initialize(referral)
13
+ @referral = referral
14
+ end
15
+
16
+ # Return String of ref data set in referrer.
17
+ def ref
18
+ @referral['ref']
19
+ end
20
+
21
+ # Return String of referral source.
22
+ def source
23
+ @referral['source']
24
+ end
25
+
26
+ # Return String of referral type.
27
+ def type
28
+ @referral['type']
29
+ end
30
+
31
+ # Return String of ad id.
32
+ def ad_id
33
+ @referral['ad_id'] if @referral.key?('ad_id')
34
+ end
35
+ end
36
+
37
+ def referral
38
+ @referral ||= Referral.new(@messaging['referral'])
39
+ end
40
+
41
+ def ref
42
+ referral.ref
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,85 @@
1
+ require 'facebook/messenger/incoming/common'
2
+ require 'facebook/messenger/incoming/message'
3
+ require 'facebook/messenger/incoming/message_echo'
4
+ require 'facebook/messenger/incoming/message_request'
5
+ require 'facebook/messenger/incoming/delivery'
6
+ require 'facebook/messenger/incoming/postback'
7
+ require 'facebook/messenger/incoming/optin'
8
+ require 'facebook/messenger/incoming/read'
9
+ require 'facebook/messenger/incoming/account_linking'
10
+ require 'facebook/messenger/incoming/referral'
11
+ require 'facebook/messenger/incoming/payment'
12
+ require 'facebook/messenger/incoming/policy_enforcement'
13
+ require 'facebook/messenger/incoming/pass_thread_control'
14
+ require 'facebook/messenger/incoming/game_play'
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'
19
+
20
+ module Facebook
21
+ module Messenger
22
+ #
23
+ # Module Incoming parses and abstracts incoming requests from Messenger.
24
+ #
25
+ module Incoming
26
+ #
27
+ # @return [Hash] Hash containing facebook messenger events and its event
28
+ # handler classes.
29
+ EVENTS = {
30
+ 'message' => Message,
31
+ 'delivery' => Delivery,
32
+ 'postback' => Postback,
33
+ 'optin' => Optin,
34
+ 'read' => Read,
35
+ 'account_linking' => AccountLinking,
36
+ 'referral' => Referral,
37
+ 'message_echo' => MessageEcho,
38
+ 'message_request' => MessageRequest,
39
+ 'payment' => Payment,
40
+ 'policy_enforcement' => PolicyEnforcement,
41
+ 'pass_thread_control' => PassThreadControl,
42
+ 'game_play' => GamePlay,
43
+ 'reaction' => MessageReaction,
44
+ 'feed' => Feed,
45
+ 'leadgen' => Leadgen,
46
+ }.freeze
47
+
48
+ # Parse the given payload and create new object of class related
49
+ # to event in payload.
50
+ #
51
+ # @see https://developers.facebook.com/docs/messenger-platform/webhook-reference
52
+ #
53
+ # @raise [Facebook::Messenger::Incoming::UnknownPayload] if event is not
54
+ # registered in EVENTS constant
55
+ #
56
+ # @param [Hash] payload A Hash describing a payload from Facebook.
57
+ #
58
+ def self.parse(payload)
59
+ return MessageEcho.new(payload) if payload_is_echo?(payload)
60
+
61
+ EVENTS.each do |event, klass|
62
+ return klass.new(payload) if payload.key?(event) || payload['field'] == event
63
+ end
64
+
65
+ raise UnknownPayload, payload
66
+ end
67
+
68
+ #
69
+ # Check if event is echo.
70
+ #
71
+ # @param [Hash] payload Request payload from facebook.
72
+ #
73
+ # @return [Boolean] If event is echo return true else false.
74
+ #
75
+ def self.payload_is_echo?(payload)
76
+ payload.key?('message') && payload['message']['is_echo'] == true
77
+ end
78
+
79
+ #
80
+ # Class UnknownPayload provides errors related to incoming messages.
81
+ #
82
+ class UnknownPayload < Facebook::Messenger::Error; end
83
+ end
84
+ end
85
+ end