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 =
i[
  message
  delivery
  postback
  optin
  read
  
  referral
  message_echo
  payment
  policy_enforcement
].freeze

Class Method Summary collapse

Class Method Details

.default_optionsHash

Default HTTParty options.


126
127
128
129
130
131
132
133
# File 'lib/facebook/messenger/bot.rb', line 126

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

.deliver(message, access_token:) ⇒ 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.

Raises:

See Also:


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/facebook/messenger/bot.rb', line 45

def deliver(message, access_token:)
  response = post '/messages',
                  body: JSON.dump(message),
                  format: :json,
                  query: {
                    access_token: access_token
                  }

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

  response.body
end

.hooksHash

Return a Hash of hooks.


108
109
110
# File 'lib/facebook/messenger/bot.rb', line 108

def hooks
  @hooks ||= {}
end

.on(event, &block) ⇒ Object

Register a hook for the given event.

Raises:

  • (ArgumentError)

    if received event is not registered.


66
67
68
69
70
71
72
73
74
# File 'lib/facebook/messenger/bot.rb', line 66

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.


84
85
86
87
88
# File 'lib/facebook/messenger/bot.rb', line 84

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.

Raises:

  • (KeyError)

    if hook is not registered for event


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

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

.unhookHash

Deregister all hooks.


117
118
119
# File 'lib/facebook/messenger/bot.rb', line 117

def unhook
  @hooks = {}
end