Skip to content

Instantly share code, notes, and snippets.

@smithclay
Last active Aug 11, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@eurica 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