class Facebook::Messenger::Incoming::Message

Message class represents an incoming Facebook Messenger message event. @see developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages

Constants

ATTACHMENT_TYPES

@return [Array] Supported attachments for message.

Public Instance Methods

app_id() click to toggle source

Function return app id from message.

@return [String] App ID.

# File lib/facebook/messenger/incoming/message.rb, line 77
def app_id
  @messaging['message']['app_id']
end
attachment_type() click to toggle source

Get the type of attachment in message.

@return [String] Attachment type.

# File lib/facebook/messenger/incoming/message.rb, line 97
def attachment_type
  return if attachments.nil?

  attachments.first['type']
end
attachment_url() click to toggle source

Get the URL of attachment in message. URL is only available for attachments of type image/audio/video/file.

@return [String] URL of attachment.

# File lib/facebook/messenger/incoming/message.rb, line 109
def attachment_url
  return if attachments.nil?
  return unless %w[image audio video file].include? attachment_type

  attachments.first['payload']['url']
end
attachments() click to toggle source

Function returns array containing attachment data @see developers.facebook.com/docs/messenger-platform/send-messages#sending_attachments

More info about attachments.

@return [Array] Attachment data.

# File lib/facebook/messenger/incoming/message.rb, line 55
def attachments
  @messaging['message']['attachments']
end
echo?() click to toggle source

Whether message is echo or not?

@return [Boolean] If message is echo return true else false.

# File lib/facebook/messenger/incoming/message.rb, line 44
def echo?
  @messaging['message']['is_echo']
end
id() click to toggle source

Function returns unique id of message @see developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages

Info about received message format.

@return [String] Unique id of message.

# File lib/facebook/messenger/incoming/message.rb, line 22
def id
  @messaging['message']['mid']
end
location_coordinates() click to toggle source

Get the location coordinates if attachment type is 'location'. @example [LATITUDE, LONGITUDE]

@return [Array] Location coordinates.

# File lib/facebook/messenger/incoming/message.rb, line 122
def location_coordinates
  return [] unless attachment_type?('location')

  coordinates_data = attachments.first['payload']['coordinates']
  [coordinates_data['lat'], coordinates_data['long']]
end
nlp() click to toggle source

If facebook messenger built-in NLP is enabled, message will

contain 'nlp' key in response.

@see developers.facebook.com/docs/messenger-platform/built-in-nlp

More information about built-in NLP.

@return [Hash] NLP information about message.

# File lib/facebook/messenger/incoming/message.rb, line 68
def nlp
  @messaging['message']['nlp']
end
quick_reply() click to toggle source

Get the payload of quick reply. @see developers.facebook.com/docs/messenger-platform/send-messages/quick-replies

More info about quick reply.

@return [String] Payload string.

# File lib/facebook/messenger/incoming/message.rb, line 136
def quick_reply
  return unless @messaging['message']['quick_reply']

  @messaging['message']['quick_reply']['payload']
end
seq() click to toggle source
# File lib/facebook/messenger/incoming/message.rb, line 26
def seq
  @messaging['message']['seq']
end
text() click to toggle source

Function returns text of message

@return [String] Text of message.

# File lib/facebook/messenger/incoming/message.rb, line 35
def text
  @messaging['message']['text']
end

Private Instance Methods

attachment_type?(attachment_type) click to toggle source

Check if attachment in message is of given type or not?

@param [String] attachment_type Attachment type

@return [Boolean] If type of attachment in message

and provided attachment type are same then return true else false.
# File lib/facebook/messenger/incoming/message.rb, line 153
def attachment_type?(attachment_type)
  !attachments.nil? && attachments.first['type'] == attachment_type
end