module Facebook::Messenger::Bot
Module Bot
provides functionality to sends and receives messages.
Constants
- EVENTS
@return [Array] Array containing the supported webhook events.
Public Class Methods
Default HTTParty options.
@return [Hash] Default HTTParty options.
# File lib/facebook/messenger/bot.rb, line 133 def default_options super.merge( read_timeout: 300, headers: { 'Content-Type' => 'application/json' } ) end
Deliver a message with the given payload. @see developers.facebook.com/docs/messenger-platform/send-api-reference#request
@raise [Facebook::Messenger::Bot::SendError] if there is any error
in response while sending message.
@param [Hash] message The message payload @param [String] page_id The page to send the message from
Returns a String describing the message ID if the message was sent, or raises an exception if it was not.
# File lib/facebook/messenger/bot.rb, line 48 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 '/messages', body: JSON.dump(message), format: :json, query: query Facebook::Messenger::Bot::ErrorParser.raise_errors_from(response) response.body end
Return a Hash of hooks.
@return [Hash] Hash of hooks.
# File lib/facebook/messenger/bot.rb, line 115 def hooks @hooks ||= {} end
Register a hook for the given event.
@raise [ArgumentError] if received event is not registered.
@param [String] event A String describing a Messenger
event. @param [Block] block A code block to run upon the event.
@return Save event and its block in hooks.
# 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 a given message from Messenger
.
@see developers.facebook.com/docs/messenger-platform/webhook-reference
@param [Hash] payload A Hash describing the message.
@return pass event and object of callback class to trigger function.
# 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 the hook for the given event. Fetch callback for event from hooks and call it.
@raise [KeyError] if hook is not registered for event
@param [String] event A String describing a Messenger
event. @param [Object] args Arguments to pass to the hook.
# 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
Deregister all hooks.
@return [Hash] Assign empty hash to hooks and return it.
# File lib/facebook/messenger/bot.rb, line 124 def unhook @hooks = {} end
Private Class Methods
# File lib/facebook/messenger/bot.rb, line 144 def config Facebook::Messenger.config end