Module: Facebook::Messenger::Incoming::Common

Included in:
AccountLinking, Delivery, Message, Optin, Payment, PolicyEnforcement, Postback, Read, Referral
Defined in:
lib/facebook/messenger/incoming/common.rb

Overview

Common attributes for all incoming data from Facebook.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messagingObject (readonly)

Returns the value of attribute messaging.



8
9
10
# File 'lib/facebook/messenger/incoming/common.rb', line 8

def messaging
  @messaging
end

Instance Method Details

#access_tokenString

Function returns the configured access token.

Returns:

  • (String)

    Access token.



134
135
136
# File 'lib/facebook/messenger/incoming/common.rb', line 134

def access_token
  Facebook::Messenger.config.provider.access_token_for(recipient)
end

#initialize(messaging) ⇒ Object

Assign message to instance variable

Parameters:

  • messaging (Object)

    Object of message.



15
16
17
# File 'lib/facebook/messenger/incoming/common.rb', line 15

def initialize(messaging)
  @messaging = messaging
end

#mark_seenObject

Function send sender_action of ‘mark_seen’ to sender.

Returns:

  • Send message to sender.

See Also:



103
104
105
106
107
108
109
110
# File 'lib/facebook/messenger/incoming/common.rb', line 103

def mark_seen
  payload = {
    recipient: sender,
    sender_action: 'mark_seen'
  }

  Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
end

#prior_messageHash

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.

Returns:

  • (Hash)

    The ‘prior_message’ hash.



50
51
52
# File 'lib/facebook/messenger/incoming/common.rb', line 50

def prior_message
  @messaging['prior_message']
end

#recipientString

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

Returns:

  • (String)

    Facebook page id.



37
38
39
# File 'lib/facebook/messenger/incoming/common.rb', line 37

def recipient
  @messaging['recipient']
end

#reply(message) ⇒ Object

Send reply to sender.

Parameters:

  • message (Hash)

    Hash defining the message.

Returns:

  • Send reply to sender.



119
120
121
122
123
124
125
126
127
# 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, access_token: access_token)
end

#senderString

Function return PSID of sender.

Returns:

  • (String)

    PSID of sender.

See Also:



28
29
30
# File 'lib/facebook/messenger/incoming/common.rb', line 28

def sender
  @messaging['sender']
end

#sent_atObject

Function return timestamp when message is sent.

Returns:

  • (Object)

    Message time sent.



60
61
62
# File 'lib/facebook/messenger/incoming/common.rb', line 60

def sent_at
  Time.at(@messaging['timestamp'] / 1000)
end

#typing_offObject

Function send sender_action of ‘typing_off’ to sender.

Returns:

  • Send message to sender.

See Also:



87
88
89
90
91
92
93
94
# File 'lib/facebook/messenger/incoming/common.rb', line 87

def typing_off
  payload = {
    recipient: sender,
    sender_action: 'typing_off'
  }

  Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
end

#typing_onObject

Function send sender_action of ‘typing_on’ to sender.

Returns:

  • Send message to sender.

See Also:



71
72
73
74
75
76
77
78
# File 'lib/facebook/messenger/incoming/common.rb', line 71

def typing_on
  payload = {
    recipient: sender,
    sender_action: 'typing_on'
  }

  Facebook::Messenger::Bot.deliver(payload, access_token: access_token)
end