module Facebook::Messenger::Incoming::Common

Common attributes for all incoming data from Facebook.

Attributes

messaging[R]

Public Class Methods

new(messaging) click to toggle source

Assign message to instance variable

@param [Object] messaging Object of message.

# File lib/facebook/messenger/incoming/common.rb, line 15
def initialize(messaging)
  @messaging = messaging
end

Public Instance Methods

mark_seen() click to toggle source

Function send sender_action of 'mark_seen' to sender. @see developers.facebook.com/docs/messenger-platform/send-messages/sender-actions

Info about sender actions.

@return Send message to sender.

# File lib/facebook/messenger/incoming/common.rb, line 103
def mark_seen
  payload = {
    recipient: sender,
    sender_action: 'mark_seen'
  }

  Facebook::Messenger::Bot.deliver(payload, page_id: recipient['id'])
end
prior_message() click to toggle source

If the user responds to your message, the appropriate event (messages, messaging_postbacks, etc.) will be sent to your webhook, with a prior_message object appended. The prior_message object includes the source of the message the user is responding to, as well as the user_ref used for the original message send.

@return [Hash] The 'prior_message' hash.

# File lib/facebook/messenger/incoming/common.rb, line 51
def prior_message
  @messaging['prior_message']
end
recipient() click to toggle source

Function return id of the page from which the message has arrived.

@return [String] Facebook page id.

# File lib/facebook/messenger/incoming/common.rb, line 38
def recipient
  @messaging['recipient']
end
reply(message) click to toggle source

Send reply to sender.

@param [Hash] message Hash defining the message.

@return Send reply to sender.

# File lib/facebook/messenger/incoming/common.rb, line 119
def reply(message)
  payload = {
    recipient: sender,
    message: message,
    message_type: Facebook::Messenger::Bot::MessageType::RESPONSE
  }

  Facebook::Messenger::Bot.deliver(payload, page_id: recipient['id'])
end
sender() click to toggle source

Function return PSID of sender.

@see developers.facebook.com/docs/messenger-platform/identity

Info about PSID.

@see developers.facebook.com/docs/messenger-platform/webhook#format

Webhook event format.

@return [String] PSID of sender.

# File lib/facebook/messenger/incoming/common.rb, line 29
def sender
  @messaging['sender']
end
sent_at() click to toggle source

Function return timestamp when message is sent.

@return [Object] Message time sent.

# File lib/facebook/messenger/incoming/common.rb, line 60
def sent_at
  Time.at(@messaging['timestamp'] / 1000)
end
typing_off() click to toggle source

Function send sender_action of 'typing_off' to sender. @see developers.facebook.com/docs/messenger-platform/send-messages/sender-actions

Info about sender actions.

@return Send message to sender.

# File lib/facebook/messenger/incoming/common.rb, line 87
def typing_off
  payload = {
    recipient: sender,
    sender_action: 'typing_off'
  }

  Facebook::Messenger::Bot.deliver(payload, page_id: recipient['id'])
end
typing_on() click to toggle source

Function send sender_action of 'typing_on' to sender. @see developers.facebook.com/docs/messenger-platform/send-messages/sender-actions

Info about sender actions.

@return Send message to sender.

# File lib/facebook/messenger/incoming/common.rb, line 71
def typing_on
  payload = {
    recipient: sender,
    sender_action: 'typing_on'
  }

  Facebook::Messenger::Bot.deliver(payload, page_id: recipient['id'])
end