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
  account_linking
  referral
  message_echo
  payment
  policy_enforcement
  pass_thread_control
].freeze

Class Method Summary collapse

Class Method Details

.default_optionsHash

Default HTTParty options.

Returns:

  • (Hash)

    Default HTTParty options.



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

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

.deliver(message, access_token:, app_secret_proof: nil) ⇒ Object

Deliver a message with the given payload. developers.facebook.com/docs/graph-api/securing-requests/ Note: we provide a helper function available at Messenger::Configuration::Providers::Base#calculate_app_secret_proof

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

Parameters:

  • message (Hash)

    A Hash describing the recipient and the message.

  • access_token (String)

    Access token.

  • app_secret_proof (String) (defaults to: nil)

    proof of the app_secret

Raises:

See Also:



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

def deliver(message, access_token:, app_secret_proof: nil)
  query = {
    access_token: access_token
  }
  query[:appsecret_proof] = app_secret_proof if app_secret_proof
  response = post '/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.



115
116
117
# File 'lib/facebook/messenger/bot.rb', line 115

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.



73
74
75
76
77
78
79
80
81
# File 'lib/facebook/messenger/bot.rb', line 73

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:



91
92
93
94
95
# File 'lib/facebook/messenger/bot.rb', line 91

def receive(payload)
  callback = Facebook::Messenger::Incoming.parse(payload)
  event = Facebook::Messenger::Incoming::EVENTS.invert[callback.class]
  trigger(event.to_sym, callback)
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



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

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.



124
125
126
# File 'lib/facebook/messenger/bot.rb', line 124

def unhook
  @hooks = {}
end