Skip to content

Instantly share code, notes, and snippets.

@andergmartins
Created April 6, 2016 19:36
Show Gist options
  • Save andergmartins/147f2dcb4264e9ee232c359e94f63552 to your computer and use it in GitHub Desktop.
Save andergmartins/147f2dcb4264e9ee232c359e94f63552 to your computer and use it in GitHub Desktop.
LUA script to use in https://www.webscript.io to redirect webhook calls to different endpoints, according to the mailbox
-- Mirror the request to multiple endpoints
-- Used to propagate HelpScout webhooks
if request.method == 'POST' then
-- List here the url for each Mailbox's webhook listener
local urlMailbox = {
Mailbox1 = 'https://www.endpoin1.test/endpoint',
Mailbox2 = 'https://www.endpoin2.test/endpoint',
}
payload = json.parse(request.body)
mailbox = payload.mailbox.name
if urlMailbox[mailbox] ~= nil then
-- Make the request to another server
http.request {
method = "post",
url = urlMailbox[mailbox],
headers = {
['X-Helpscout-Signature'] = request.headers['X-Helpscout-Signature'],
['X-Helpscout-Event'] = request.headers['X-Helpscout-Event'],
['Content-Type'] = 'application/json',
['User-Agent'] = 'Help Scout/Webhooks'
},
data = request.body
}
print('Conversation: ' .. payload.id)
print('Mailbox: ' .. mailbox)
print('Request mirrored to: ' .. urlMailbox[mailbox])
else
print('Mailbox ' .. mailbox .. ' not mapped to an url')
end
return 200
end
print('Invalid Method')
return 405
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment