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[ delivery postback optin read account_linking referral payment policy_enforcement ].freeze
Class Method Summary collapse
-
.default_options ⇒ Hash
Default HTTParty options.
-
.deliver(message, access_token:) ⇒ Object
Deliver a message with the given payload.
-
.hooks ⇒ Hash
Return a Hash of hooks.
-
.on(event, &block) ⇒ Object
Register a hook for the given event.
-
.receive(payload) ⇒ Object
Receive a given message from Messenger.
-
.trigger(event, *args) ⇒ Object
Trigger the hook for the given event.
-
.unhook ⇒ Hash
Deregister all hooks.
Class Method Details
.default_options ⇒ Hash
Default HTTParty options.
126 127 128 129 130 131 132 133 |
# File 'lib/facebook/messenger/bot.rb', line 126 def 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.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/facebook/messenger/bot.rb', line 45 def deliver(, access_token:) response = post '/messages', body: JSON.dump(), format: :json, query: { access_token: access_token } Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response) response.body end |
.hooks ⇒ Hash
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.
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.
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 |
.unhook ⇒ Hash
Deregister all hooks.
117 118 119 |
# File 'lib/facebook/messenger/bot.rb', line 117 def unhook @hooks = {} end |