-
-
Save dpup/24c822570d613464667106b0ab04b88e to your computer and use it in GitHub Desktop.
Sample script for integrating with Range's incoming webhooks.
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
// oncall-notifier.js is a sample script that demonstrates using the incoming | |
// webhook to create suggestions for users. It generates a snippet that indicates | |
// the user is now oncall. | |
const crypto = require('crypto'); | |
const querystring = require('querystring'); | |
const https = require('https'); | |
const webhookURL = '[YOUR-WEBHOOK-URL]'; | |
const targetUser = '[YOUR-EMAIL-ADDRESS]'; | |
// For the purposes of this test script, use the date as the source ID. | |
const rotationIdentifier = new Date().toISOString().split('T')[0]; | |
// The payload is serialized JSON. | |
const payload = JSON.stringify({ | |
email_hash: emailHash(targetUser), | |
is_future: true, | |
reason: 'ASSIGNED', | |
dedupe_strategy: 'UPSERT_PENDING', | |
attachment: { | |
source_id: rotationIdentifier, | |
provider: 'oncall', | |
provider_name: 'On Call Notifier', | |
html_url: `https://oncall.acme.corp/rotation/${rotationIdentifier}`, | |
name: 'Primary on call', | |
type: 'LINK' | |
} | |
}); | |
// The payload should be application/json and sent via a POST request. | |
const opts = { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(payload) | |
} | |
}; | |
// Make the request. | |
let req = https.request(webhookURL, opts, res => { | |
res.setEncoding('utf8'); | |
res.on('data', chunk => { | |
console.log(chunk); | |
}); | |
}); | |
req.write(payload); | |
req.end(); | |
function emailHash(email) { | |
let shasum = crypto.createHash('sha1'); | |
shasum.update(email); | |
return shasum.digest('hex'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment