Merge pull request #2484 from tchak/helpscout-webhook
Add helpscout webhook
This commit is contained in:
commit
d8a91bf57f
3 changed files with 56 additions and 2 deletions
50
app/controllers/webhook_controller.rb
Normal file
50
app/controllers/webhook_controller.rb
Normal 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
|
|
@ -120,8 +120,11 @@ Rails.application.routes.draw do
|
||||||
|
|
||||||
get "patron" => "root#patron"
|
get "patron" => "root#patron"
|
||||||
|
|
||||||
get "contact" => "support#index"
|
get "contact", to: "support#index"
|
||||||
post "contact" => "support#create"
|
post "contact", to: "support#create"
|
||||||
|
|
||||||
|
post "webhooks/helpscout", to: "webhook#helpscout"
|
||||||
|
match "webhooks/helpscout", to: lambda { |_| [204, {}, nil] }, via: :head
|
||||||
|
|
||||||
#
|
#
|
||||||
# Deprecated UI
|
# Deprecated UI
|
||||||
|
|
|
@ -48,6 +48,7 @@ defaults: &defaults
|
||||||
mailbox_id: <%= ENV['HELPSCOUT_MAILBOX_ID'] %>
|
mailbox_id: <%= ENV['HELPSCOUT_MAILBOX_ID'] %>
|
||||||
client_id: <%= ENV['HELPSCOUT_CLIENT_ID'] %>
|
client_id: <%= ENV['HELPSCOUT_CLIENT_ID'] %>
|
||||||
client_secret: <%= ENV['HELPSCOUT_CLIENT_SECRET'] %>
|
client_secret: <%= ENV['HELPSCOUT_CLIENT_SECRET'] %>
|
||||||
|
webhook_secret: <%= ENV['HELPSCOUT_WEBHOOK_SECRET'] %>
|
||||||
|
|
||||||
development:
|
development:
|
||||||
<<: *defaults
|
<<: *defaults
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue