-
-
Save smithclay/b0a21736fc2f3be2d918 to your computer and use it in GitHub Desktop.
PostcardDuty A webscript.io endpoint that sends a postcard to PagerDuty HQ when an incident changes.
A lob.com API key is required.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- PostcardDuty | |
-- webscript.io lua script that sends a postcard using the lob.com api | |
LOB_API_ENDPOINT = 'https://api.lob.com/v1/postcards' | |
LOB_API_KEY = '<INSERT YOUR LOB API KEY HERE>' | |
-- Only accept POST requests with JSON | |
if request.method ~= "POST" then return 403, "Not allowed." end | |
if not request.body then return 400, "No request body." end | |
-- Parse PagerDuty incidents | |
local json = json.parse(request.body) | |
local messages = '' | |
for i,msg in ipairs(json.messages) do | |
local data = msg.data | |
log('foo') | |
log(data.status) | |
local msg = string.format( | |
'PagerDuty Alert: Incident %s on %s', | |
data.status, | |
data.service.name | |
) | |
messages = messages .. msg .. '\n' | |
log(msg) | |
end | |
-- Create a Postcard in LOB | |
local response = http.request { | |
auth = {LOB_API_KEY, ''}, | |
url = LOB_API_ENDPOINT, | |
method = 'post', | |
data = { | |
['name'] = 'PagerDuty Postcard Job', | |
['to[name]'] = 'PagerDuty', | |
['to[address_line1]'] = '55 Federal Street', | |
['to[address_line2]'] = 'Floor 4', | |
['to[address_city]'] = 'San Francisco', | |
['to[address_state]'] = 'CA', | |
['to[address_zip]'] = '94103', | |
['message'] = messages | |
} | |
} | |
local postcard_job = json.parse(response) | |
log(postcard_job) | |
return postcard_job.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes I hate your evil brilliance, Clay.