Module: Facebook::Messenger::Bot

Includes:
HTTParty
Defined in:
lib/facebook/messenger/bot.rb,
lib/facebook/messenger/bot/tag.rb,
lib/facebook/messenger/bot/exceptions.rb,
lib/facebook/messenger/bot/error_parser.rb,
lib/facebook/messenger/bot/message_type.rb,
lib/facebook/messenger/bot/messaging_type.rb

Overview

Module Bot provides functionality to sends and receives messages.

Defined Under Namespace

Modules: MessageType, MessagingType, Tag Classes: AccessTokenError, AccountLinkingError, BadParameterError, ErrorParser, InternalError, LimitError, PermissionError, SendError

Constant Summary collapse

EVENTS =

Returns Array containing the supported webhook events.

Returns:

  • (Array)

    Array containing the supported webhook events.

i[
  message
  delivery
  postback
  optin
  read
  
  referral
  message_echo
  payment
  policy_enforcement
  pass_thread_control
  game_play
  reaction
  feed
  leadgen
].freeze

Class Method Summary collapse

Class Method Details

.default_optionsHash

Default HTTParty options.

Returns:

  • (Hash)

    Default HTTParty options.



163
164
165
166
167
168
169
170
# File 'lib/facebook/messenger/bot.rb', line 163

def default_options
  super.merge(
    read_timeout: 300,
    headers: {
      'Content-Type' => 'application/json'
    }
  )
end

.deliver(message, page_id:) ⇒ Object

Deliver a message with the given payload. Returns a String describing the message ID if the message was sent, or raises an exception if it was not.

Parameters:

  • message (Hash)

    The message payload

  • page_id (String)

    The page to send the message from

Raises:

See Also:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/facebook/messenger/bot.rb', line 50

def deliver(message, page_id:)
  access_token = config.provider.access_token_for(page_id)
  app_secret_proof = config.provider.app_secret_proof_for(page_id)

  query = { access_token: access_token }
  query[:appsecret_proof] = app_secret_proof if app_secret_proof

  response = post '/me/messages',
                  body: JSON.dump(message),
                  format: :json,
                  query: query

  Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response)

  response.body
end

.hooksHash

Return a Hash of hooks.

Returns:

  • (Hash)

    Hash of hooks.



145
146
147
# File 'lib/facebook/messenger/bot.rb', line 145

def hooks
  @hooks ||= {}
end

.on(event, &block) ⇒ Object

Register a hook for the given event.

Parameters:

  • event (String)

    A String describing a Messenger event.

  • block (Block)

    A code block to run upon the event.

Returns:

  • Save event and its block in hooks.

Raises:

  • (ArgumentError)

    if received event is not registered.



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

def on(event, &block)
  unless EVENTS.include? event
    raise ArgumentError,
          "#{event} is not a valid event; " \
          "available events are #{EVENTS.join(',')}"
  end

  hooks[event] = block
end

.receive(payload) ⇒ Object

Receive a given message from Messenger.

Parameters:

  • payload (Hash)

    A Hash describing the message.

Returns:

  • pass event and object of callback class to trigger function.

See Also:



121
122
123
124
125
# File 'lib/facebook/messenger/bot.rb', line 121

def receive(payload)
  callback = Facebook::Messenger::Incoming.parse(payload)
  event = Facebook::Messenger::Incoming::EVENTS.invert[callback.class]
  trigger(event.to_sym, callback)
end

.reply_to_comment(comment_id, message, page_id:) ⇒ Object

Reply to a Facebook comment. Returns a Hash describing the API response if the comment was sent, or raises an exception if it was not.

Parameters:

  • comment (Hash)

    The comment payload

  • page_id (String)

    The page ID to send the comment from

Raises:

See Also:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/facebook/messenger/bot.rb', line 78

def reply_to_comment(comment_id, message, page_id:)
  access_token = config.provider.access_token_for(page_id)
  app_secret_proof = config.provider.app_secret_proof_for(page_id)

  query = { access_token: access_token }
  query[:appsecret_proof] = app_secret_proof if app_secret_proof

  response = post "/#{comment_id}/comments",
                  body: JSON.dump(message),
                  format: :json,
                  query: query

  Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response)

  response.body
end

.trigger(event, *args) ⇒ Object

Trigger the hook for the given event. Fetch callback for event from hooks and call it.

Parameters:

  • event (String)

    A String describing a Messenger event.

  • args (Object)

    Arguments to pass to the hook.

Raises:

  • (KeyError)

    if hook is not registered for event



134
135
136
137
138
# File 'lib/facebook/messenger/bot.rb', line 134

def trigger(event, *args)
  hooks.fetch(event).call(*args)
rescue KeyError
  warn "Ignoring #{event} (no hook registered)"
end

.unhookHash

Deregister all hooks.

Returns:

  • (Hash)

    Assign empty hash to hooks and return it.



154
155
156
# File 'lib/facebook/messenger/bot.rb', line 154

def unhook
  @hooks = {}
end