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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +9 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +593 -0
- data/Rakefile +8 -0
- data/app/controllers/messenger/messenger_controller.rb +89 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/circle.yml +6 -0
- data/config/routes.rb +8 -0
- data/lib/messenger-ruby.rb +57 -0
- data/lib/messenger/client.rb +19 -0
- data/lib/messenger/components/attachment.rb +33 -0
- data/lib/messenger/components/element.rb +13 -0
- data/lib/messenger/components/elements/bubble.rb +23 -0
- data/lib/messenger/components/elements/button.rb +25 -0
- data/lib/messenger/components/elements/image.rb +18 -0
- data/lib/messenger/components/elements/quick_reply.rb +24 -0
- data/lib/messenger/components/elements/receipt/address.rb +20 -0
- data/lib/messenger/components/elements/receipt/adjustment.rb +16 -0
- data/lib/messenger/components/elements/receipt/item.rb +20 -0
- data/lib/messenger/components/elements/receipt/order.rb +19 -0
- data/lib/messenger/components/elements/receipt/summary.rb +18 -0
- data/lib/messenger/components/elements/sender_action.rb +15 -0
- data/lib/messenger/components/elements/text.rb +15 -0
- data/lib/messenger/components/elements/video.rb +18 -0
- data/lib/messenger/components/templates/buttons.rb +20 -0
- data/lib/messenger/components/templates/generic.rb +19 -0
- data/lib/messenger/components/templates/quick_replies.rb +18 -0
- data/lib/messenger/components/templates/receipt.rb +40 -0
- data/lib/messenger/configuration.rb +10 -0
- data/lib/messenger/engine.rb +7 -0
- data/lib/messenger/parameters/account_linking.rb +14 -0
- data/lib/messenger/parameters/attachment.rb +23 -0
- data/lib/messenger/parameters/callback.rb +15 -0
- data/lib/messenger/parameters/delivery.rb +15 -0
- data/lib/messenger/parameters/entry.rb +17 -0
- data/lib/messenger/parameters/message.rb +29 -0
- data/lib/messenger/parameters/messaging.rb +24 -0
- data/lib/messenger/parameters/optin.rb +13 -0
- data/lib/messenger/parameters/postback.rb +13 -0
- data/lib/messenger/parameters/quick_reply.rb +11 -0
- data/lib/messenger/parameters/read.rb +14 -0
- data/lib/messenger/parameters/referral.rb +15 -0
- data/lib/messenger/params.rb +25 -0
- data/lib/messenger/request.rb +35 -0
- data/lib/messenger/version.rb +3 -0
- data/messenger-ruby.gemspec +31 -0
- 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,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,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,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,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
         |