Class: Facebook::Messenger::Incoming::Message

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/facebook/messenger/incoming/message.rb

Overview

Message class represents an incoming Facebook Messenger message event.

Direct Known Subclasses

MessageEcho, MessageRequest

Constant Summary collapse

ATTACHMENT_TYPES =

Returns Supported attachments for message.

Returns:

  • (Array)

    Supported attachments for message.

%w[image audio video file location fallback].freeze

Instance Attribute Summary

Attributes included from Common

#messaging

Instance Method Summary collapse

Methods included from Common

#initialize, #mark_seen, #prior_message, #recipient, #reply, #sender, #sent_at, #typing_off, #typing_on

Instance Method Details

#app_idString

Function return app id from message.

Returns:

  • (String)

    App ID.



77
78
79
# File 'lib/facebook/messenger/incoming/message.rb', line 77

def app_id
  @messaging['message']['app_id']
end

#attachment_typeString

Get the type of attachment in message.

Returns:

  • (String)

    Attachment type.



97
98
99
100
101
# File 'lib/facebook/messenger/incoming/message.rb', line 97

def attachment_type
  return if attachments.nil?

  attachments.first['type']
end

#attachment_urlString

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

Returns:

  • (String)

    URL of attachment.



109
110
111
112
113
114
# 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

#attachmentsArray

Function returns array containing attachment data

Returns:

  • (Array)

    Attachment data.

See Also:



55
56
57
# File 'lib/facebook/messenger/incoming/message.rb', line 55

def attachments
  @messaging['message']['attachments']
end

#echo?Boolean

Whether message is echo or not?

Returns:

  • (Boolean)

    If message is echo return true else false.



44
45
46
# File 'lib/facebook/messenger/incoming/message.rb', line 44

def echo?
  @messaging['message']['is_echo']
end

#idString

Function returns unique id of message

Returns:

  • (String)

    Unique id of message.

See Also:



22
23
24
# File 'lib/facebook/messenger/incoming/message.rb', line 22

def id
  @messaging['message']['mid']
end

#location_coordinatesArray

Get the location coordinates if attachment type is ‘location’.

Examples:

LATITUDE, LONGITUDE

Returns:

  • (Array)

    Location coordinates.



122
123
124
125
126
127
# 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

#nlpHash

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

contain 'nlp' key in response.

Returns:

  • (Hash)

    NLP information about message.

See Also:



68
69
70
# File 'lib/facebook/messenger/incoming/message.rb', line 68

def nlp
  @messaging['message']['nlp']
end

#quick_replyString

Get the payload of quick reply.

Returns:

  • (String)

    Payload string.

See Also:



136
137
138
139
140
# File 'lib/facebook/messenger/incoming/message.rb', line 136

def quick_reply
  return unless @messaging['message']['quick_reply']

  @messaging['message']['quick_reply']['payload']
end

#seqObject



26
27
28
# File 'lib/facebook/messenger/incoming/message.rb', line 26

def seq
  @messaging['message']['seq']
end

#textString

Function returns text of message

Returns:

  • (String)

    Text of message.



35
36
37
# File 'lib/facebook/messenger/incoming/message.rb', line 35

def text
  @messaging['message']['text']
end