Add helpscout webhook

This commit is contained in:
Paul Chavard 2018-08-29 20:26:22 +01:00
parent 92f87b1f92
commit 5f12ea43a7
3 changed files with 56 additions and 2 deletions

View file

@ -0,0 +1,50 @@
class WebhookController < ActionController::Base
before_action :verify_signature!, only: :helpscout
def helpscout
email = params[:customer][:email]
user = User.find_by(email: email)
gestionnaire = Gestionnaire.find_by(email: email)
administrateur = Administrateur.find_by(email: email)
html = []
if user
url = view_context.manager_user_url(user)
html << link_to_manager(user, url)
end
if gestionnaire
url = view_context.manager_gestionnaire_url(gestionnaire)
html << link_to_manager(gestionnaire, url)
end
if administrateur
url = view_context.manager_administrateur_url(administrateur)
html << link_to_manager(administrateur, url)
end
if html.empty?
head :not_found
else
render json: { html: html.join('<br>') }
end
end
private
def link_to_manager(model, url)
"<a target='_blank' href='#{url}'>#{model.model_name.human}##{model.id}</a>"
end
def verify_signature!
if generate_body_signature(request.body.read) != request.headers['X-Helpscout-Signature']
request_http_token_authentication
end
end
def generate_body_signature(body)
Base64.strict_encode64(OpenSSL::HMAC.digest('sha1',
Rails.application.secrets.helpscout[:webhook_secret],
body))
end
end

View file

@ -120,8 +120,11 @@ Rails.application.routes.draw do
get "patron" => "root#patron"
get "contact" => "support#index"
post "contact" => "support#create"
get "contact", to: "support#index"
post "contact", to: "support#create"
post "webhooks/helpscout", to: "webhook#helpscout"
match "webhooks/helpscout", to: lambda { |_| [204, {}, nil] }, via: :head
#
# Deprecated UI

View file

@ -48,6 +48,7 @@ defaults: &defaults
mailbox_id: <%= ENV['HELPSCOUT_MAILBOX_ID'] %>
client_id: <%= ENV['HELPSCOUT_CLIENT_ID'] %>
client_secret: <%= ENV['HELPSCOUT_CLIENT_SECRET'] %>
webhook_secret: <%= ENV['HELPSCOUT_WEBHOOK_SECRET'] %>
development:
<<: *defaults