messenger-bot-client 1.2.1

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 (51) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +9 -0
  3. data/.gitignore +6 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +4 -0
  6. data/README.md +593 -0
  7. data/Rakefile +8 -0
  8. data/app/controllers/messenger/messenger_controller.rb +89 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/circle.yml +6 -0
  12. data/config/routes.rb +8 -0
  13. data/lib/messenger-ruby.rb +57 -0
  14. data/lib/messenger/client.rb +19 -0
  15. data/lib/messenger/components/attachment.rb +33 -0
  16. data/lib/messenger/components/element.rb +13 -0
  17. data/lib/messenger/components/elements/bubble.rb +23 -0
  18. data/lib/messenger/components/elements/button.rb +25 -0
  19. data/lib/messenger/components/elements/image.rb +18 -0
  20. data/lib/messenger/components/elements/quick_reply.rb +24 -0
  21. data/lib/messenger/components/elements/receipt/address.rb +20 -0
  22. data/lib/messenger/components/elements/receipt/adjustment.rb +16 -0
  23. data/lib/messenger/components/elements/receipt/item.rb +20 -0
  24. data/lib/messenger/components/elements/receipt/order.rb +19 -0
  25. data/lib/messenger/components/elements/receipt/summary.rb +18 -0
  26. data/lib/messenger/components/elements/sender_action.rb +15 -0
  27. data/lib/messenger/components/elements/text.rb +15 -0
  28. data/lib/messenger/components/elements/video.rb +18 -0
  29. data/lib/messenger/components/templates/buttons.rb +20 -0
  30. data/lib/messenger/components/templates/generic.rb +19 -0
  31. data/lib/messenger/components/templates/quick_replies.rb +18 -0
  32. data/lib/messenger/components/templates/receipt.rb +40 -0
  33. data/lib/messenger/configuration.rb +10 -0
  34. data/lib/messenger/engine.rb +7 -0
  35. data/lib/messenger/parameters/account_linking.rb +14 -0
  36. data/lib/messenger/parameters/attachment.rb +23 -0
  37. data/lib/messenger/parameters/callback.rb +15 -0
  38. data/lib/messenger/parameters/delivery.rb +15 -0
  39. data/lib/messenger/parameters/entry.rb +17 -0
  40. data/lib/messenger/parameters/message.rb +29 -0
  41. data/lib/messenger/parameters/messaging.rb +24 -0
  42. data/lib/messenger/parameters/optin.rb +13 -0
  43. data/lib/messenger/parameters/postback.rb +13 -0
  44. data/lib/messenger/parameters/quick_reply.rb +11 -0
  45. data/lib/messenger/parameters/read.rb +14 -0
  46. data/lib/messenger/parameters/referral.rb +15 -0
  47. data/lib/messenger/params.rb +25 -0
  48. data/lib/messenger/request.rb +35 -0
  49. data/lib/messenger/version.rb +3 -0
  50. data/messenger-ruby.gemspec +31 -0
  51. metadata +219 -0
@@ -0,0 +1,18 @@
1
+ require 'messenger/components/element'
2
+
3
+ module Messenger
4
+ module Elements
5
+ class Summary
6
+ include Components::Element
7
+
8
+ attr_accessor :subtotal, :shipping_cost, :total_tax, :total_cost
9
+
10
+ def initialize(subtotal: nil, shipping_cost: nil, total_tax: nil, total_cost:)
11
+ @subtotal = subtotal
12
+ @shipping_cost = shipping_cost
13
+ @total_tax = total_tax
14
+ @total_cost = total_cost
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Messenger
2
+ module Elements
3
+ class SenderAction
4
+ attr_accessor :sender_action
5
+
6
+ def initialize(sender_action:)
7
+ @sender_action = sender_action
8
+ end
9
+
10
+ def build
11
+ { 'sender_action' => @sender_action }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Messenger
2
+ module Elements
3
+ class Text
4
+ attr_accessor :text
5
+
6
+ def initialize(text:)
7
+ @text = text
8
+ end
9
+
10
+ def build
11
+ instance_values
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'messenger/components/attachment'
2
+
3
+ module Messenger
4
+ module Elements
5
+ class Video
6
+ include Components::Attachment
7
+
8
+ attr_accessor :url
9
+
10
+ ATTRIBUTES = %w(url).freeze
11
+
12
+ def initialize(url:)
13
+ @url = url
14
+ @type = 'video'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'messenger/components/attachment'
2
+
3
+ module Messenger
4
+ module Templates
5
+ class Buttons
6
+ include Components::Attachment
7
+
8
+ attr_accessor :text, :buttons, :template_type
9
+
10
+ ATTRIBUTES = %w(template_type text buttons).freeze
11
+
12
+ def initialize(text:, buttons:)
13
+ @type = 'template'
14
+ @template_type = 'button'
15
+ @text = text
16
+ @buttons = build_elements(buttons)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'messenger/components/attachment'
2
+
3
+ module Messenger
4
+ module Templates
5
+ class Generic
6
+ include Components::Attachment
7
+
8
+ attr_accessor :template_type, :elements
9
+
10
+ ATTRIBUTES = %w(template_type elements).freeze
11
+
12
+ def initialize(elements:)
13
+ @type = 'template'
14
+ @template_type = 'generic'
15
+ @elements = build_elements(elements)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require 'messenger/components/attachment'
2
+
3
+ module Messenger
4
+ module Templates
5
+ class QuickReplies
6
+ include Components::Element
7
+
8
+ attr_accessor :text, :quick_replies
9
+
10
+ ATTRIBUTES = %w(text quick_replies).freeze
11
+
12
+ def initialize(text:, quick_replies:)
13
+ @text = text
14
+ @quick_replies = build_elements(quick_replies)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ require 'messenger/components/attachment'
2
+
3
+ module Messenger
4
+ module Templates
5
+ class Receipt
6
+ include Components::Attachment
7
+
8
+ attr_accessor :template_type, :recipient_name, :order, :elements, :address, :summary, :adjustments
9
+
10
+ ATTRIBUTES = %w(
11
+ template_type
12
+ recipient_name
13
+ order_number
14
+ currency
15
+ payment_method
16
+ timestamp
17
+ order_url
18
+ elements
19
+ address
20
+ summary
21
+ adjustments
22
+ ).freeze
23
+
24
+ def initialize(recipient_name:, order:, elements:, address: nil, summary:, adjustments: nil)
25
+ @type = 'template'
26
+ @template_type = 'receipt'
27
+ @recipient_name = recipient_name
28
+ @order = order
29
+ @elements = build_elements(elements)
30
+ @address = address.build if address.present?
31
+ @summary = summary.build
32
+ @adjustments = build_elements(adjustments)
33
+ end
34
+
35
+ def flattened_attributes
36
+ @order.build
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ module Messenger
2
+ class Configuration
3
+ attr_accessor :verify_token, :page_access_token
4
+
5
+ def initialize(verify_token: nil, page_access_token: nil)
6
+ @verify_token = verify_token
7
+ @page_access_token = page_access_token
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails'
2
+
3
+ module Messenger
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace Messenger
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module Messenger
2
+ module Parameters
3
+ class AccountLinking
4
+ include Callback
5
+
6
+ attr_accessor :status, :authorization_code
7
+
8
+ def initialize(status:, authorization_code: nil)
9
+ @status = status
10
+ @authorization_code = authorization_code
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Attachment
4
+ attr_accessor :type, :url, :long, :lat
5
+
6
+ def initialize(type:, payload:)
7
+ @type = type
8
+ set_payload_attributes(payload)
9
+ end
10
+
11
+ private
12
+
13
+ def set_payload_attributes(payload)
14
+ if @type == 'location'
15
+ @long = payload['coordinates']['long']
16
+ @lat = payload['coordinates']['lat']
17
+ else
18
+ @url = payload['url']
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module Messenger
2
+ module Parameters
3
+ module Callback
4
+ %w(message delivery optin postback read account_linking referral).each do |method|
5
+ define_method("#{method}?") do
6
+ type.include?(method)
7
+ end
8
+ end
9
+
10
+ def type
11
+ self.class.to_s.underscore
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Delivery
4
+ include Callback
5
+
6
+ attr_accessor :mids, :watermark, :seq
7
+
8
+ def initialize(mids: nil, watermark:, seq:)
9
+ @mids = mids
10
+ @watermark = watermark
11
+ @seq = seq
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Entry
4
+ attr_accessor :id, :time, :messagings
5
+
6
+ def initialize(id:, time:, messaging: nil)
7
+ @id = id
8
+ @time = time
9
+ @messagings = build_messagings(messaging) if messaging.present?
10
+ end
11
+
12
+ def build_messagings(messagings)
13
+ messagings.map { |messaging| Messaging.new(messaging.transform_keys(&:to_sym)) }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Message
4
+ include Callback
5
+
6
+ attr_accessor :mid, :seq, :sticker_id, :text, :attachments, :quick_reply, :is_echo, :app_id, :metadata
7
+
8
+ def initialize(mid:, seq:, sticker_id: nil, text: nil, attachments: nil, quick_reply: nil, is_echo: nil, app_id: nil, metadata: nil)
9
+ @mid = mid
10
+ @seq = seq
11
+ @sticker_id = sticker_id if sticker_id.present?
12
+ @text = text if text.present?
13
+ @attachments = build_attachments(attachments) if attachments.present?
14
+ @quick_reply = build_quick_reply(quick_reply) if quick_reply.present?
15
+ @is_echo = is_echo
16
+ @app_id = app_id
17
+ @metadata = metadata
18
+ end
19
+
20
+ def build_attachments(attachments)
21
+ attachments.map { |attachment| Attachment.new(attachment.transform_keys(&:to_sym).slice(:type, :payload)) }
22
+ end
23
+
24
+ def build_quick_reply(quick_reply)
25
+ QuickReply.new(quick_reply.transform_keys(&:to_sym))
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Messaging
4
+ attr_accessor :sender_id, :recipient_id, :callback
5
+
6
+ def initialize(sender:, recipient:, timestamp: nil, message: nil, delivery: nil, postback: nil, optin: nil, read: nil, account_linking: nil)
7
+ @sender_id = sender['id']
8
+ @recipient_id = recipient['id']
9
+ @callback = set_callback(message: message, delivery: delivery, postback: postback, optin: optin, read: read, account_linking: account_linking)
10
+ end
11
+
12
+ def set_callback(callbacks)
13
+ type = callbacks.select { |_, v| v.present? }.keys.first
14
+ @callback = constant(type).new(callbacks[type].transform_keys(&:to_sym))
15
+ end
16
+
17
+ private
18
+
19
+ def constant(symbol)
20
+ "Messenger::Parameters::#{symbol.to_s.camelize}".constantize
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Optin
4
+ include Callback
5
+
6
+ attr_accessor :ref
7
+
8
+ def initialize(ref:)
9
+ @ref = ref
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Postback
4
+ include Callback
5
+
6
+ attr_accessor :payload
7
+
8
+ def initialize(payload:)
9
+ @payload = payload
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Messenger
2
+ module Parameters
3
+ class QuickReply
4
+ attr_accessor :payload
5
+
6
+ def initialize(payload:)
7
+ @payload = payload
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Read
4
+ include Callback
5
+
6
+ attr_accessor :watermark, :seq
7
+
8
+ def initialize(watermark:, seq:)
9
+ @watermark = watermark
10
+ @seq = seq
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module Messenger
2
+ module Parameters
3
+ class Referral
4
+ include Callback
5
+
6
+ attr_accessor :ref, :source, :type
7
+
8
+ def initialize(ref:, source:, type:)
9
+ @ref = ref
10
+ @source = source
11
+ @type = type
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Messenger
2
+ class Params
3
+ attr_accessor :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def entries
10
+ @entries_objects ||= build_entries
11
+ end
12
+
13
+ def first_entry
14
+ entries[0].messagings[0]
15
+ end
16
+
17
+ alias_method :first_messaging, :first_entry
18
+
19
+ private
20
+
21
+ def build_entries
22
+ params['entry'].map { |entry| Parameters::Entry.new(entry.to_h.transform_keys(&:to_sym)) }
23
+ end
24
+ end
25
+ end