Skip to content

Instantly share code, notes, and snippets.

@dpup
Created October 3, 2018 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpup/24c822570d613464667106b0ab04b88e to your computer and use it in GitHub Desktop.
Save dpup/24c822570d613464667106b0ab04b88e to your computer and use it in GitHub Desktop.
Sample script for integrating with Range's incoming webhooks.
// 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