Skip to content

Instantly share code, notes, and snippets.

@smithclay
Last active August 11, 2021 17:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smithclay/b0a21736fc2f3be2d918 to your computer and use it in GitHub Desktop.
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.
-- 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
@eurica
Copy link

eurica commented Jul 31, 2013

Sometimes I hate your evil brilliance, Clay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment