Class: Bobot::WebhookController

Inherits:
ApplicationController show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/controllers/bobot/webhook_controller.rb

Defined Under Namespace

Classes: BadRequestError

Constant Summary collapse

X_HUB_SIGNATURE_MISSING_WARNING =
"  The X-Hub-Signature header is not present in the request. This is\n  expected for the first webhook requests. If it continues after\n  some time, check your app's secret token.\n".freeze

Instance Method Summary collapse

Instance Method Details

#receiveObject



36
37
38
39
40
41
42
# File 'app/controllers/bobot/webhook_controller.rb', line 36

def receive
  check_integrity
  return_json = trigger(parsed_body)
  render plain: ActiveSupport::JSON.encode(return_json), status: :ok
rescue BadRequestError => error
  render plain: error.message, status: :bad_request
end

#verifyObject



27
28
29
30
31
32
33
34
# File 'app/controllers/bobot/webhook_controller.rb', line 27

def verify
  if params['hub.mode'.freeze] == 'subscribe' &&
     params['hub.verify_token'.freeze] == Bobot.config.verify_token
    render plain: params['hub.challenge'.freeze], status: :ok
  else
    render plain: "Error wrong verify token".freeze, status: :forbidden
  end
end

#webhookObject



17
18
19
20
21
22
23
24
25
# File 'app/controllers/bobot/webhook_controller.rb', line 17

def webhook
  if request.get?
    verify
  elsif request.post?
    receive
  else
    head :method_not_allowed
  end
end