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.
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:, app_secret_proof: nil) ⇒ 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.
132 133 134 135 136 137 138 139 |
# File 'lib/facebook/messenger/bot.rb', line 132 def 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/facebook/messenger/bot.rb', line 49 def deliver(, 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(), format: :json, query: query Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response) response.body end |
.hooks ⇒ Hash
Return a Hash of hooks.
114 115 116 |
# File 'lib/facebook/messenger/bot.rb', line 114 def hooks @hooks ||= {} end |
.on(event, &block) ⇒ Object
Register a hook for the given event.
72 73 74 75 76 77 78 79 80 |
# File 'lib/facebook/messenger/bot.rb', line 72 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.
90 91 92 93 94 |
# File 'lib/facebook/messenger/bot.rb', line 90 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.
103 104 105 106 107 |
# File 'lib/facebook/messenger/bot.rb', line 103 def trigger(event, *args) hooks.fetch(event).call(*args) rescue KeyError warn "Ignoring #{event} (no hook registered)" end |
.unhook ⇒ Hash
Deregister all hooks.
123 124 125 |
# File 'lib/facebook/messenger/bot.rb', line 123 def unhook @hooks = {} end |