Hey there, fellow JavaScript aficionado! Ready to supercharge your Confluence integration with webhooks? You're in the right place. Webhooks are like your app's personal news feed from Confluence, keeping you in the loop about all the juicy changes happening in real-time. Let's dive in and get those webhooks up and running!
Before we jump into the code, make sure you've got:
Got all that? Great! Let's roll up our sleeves and get coding.
First things first, we need a place for Confluence to send all those sweet, sweet updates. Let's whip up a quick Express server:
const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { console.log('Webhook received:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Boom! You've got a basic server ready to catch those webhooks. Easy peasy, right?
Now, let's tell Confluence where to send the goods. We'll use the Confluence API to register our webhook:
const axios = require('axios'); const registerWebhook = async () => { try { const response = await axios.post( 'https://your-domain.atlassian.net/wiki/rest/api/webhook', { name: 'My Awesome Webhook', url: 'https://your-webhook-url.com/webhook', events: ['page_created', 'page_updated', 'page_deleted'] }, { auth: { username: '[email protected]', password: 'your-api-token' } } ); console.log('Webhook registered:', response.data); } catch (error) { console.error('Error registering webhook:', error); } }; registerWebhook();
Just replace those placeholders with your actual Confluence domain, webhook URL, and API credentials, and you're golden!
Now that the webhooks are flowing, let's do something useful with them:
app.post('/webhook', (req, res) => { const { event, page } = req.body; switch (event) { case 'page_created': console.log(`New page created: ${page.title}`); break; case 'page_updated': console.log(`Page updated: ${page.title}`); break; case 'page_deleted': console.log(`Page deleted: ${page.title}`); break; default: console.log(`Unhandled event: ${event}`); } res.sendStatus(200); });
Look at you go! You're now handling those events like a pro.
Security is sexy, so let's add some webhook signature verification:
const crypto = require('crypto'); const verifyWebhookSignature = (req, res, next) => { const signature = req.headers['x-atlassian-webhook-signature']; const payload = JSON.stringify(req.body); const hmac = crypto.createHmac('sha256', 'your-webhook-secret'); const calculatedSignature = hmac.update(payload).digest('base64'); if (signature === calculatedSignature) { next(); } else { res.sendStatus(401); } }; app.post('/webhook', verifyWebhookSignature, (req, res) => { // Your webhook handling code here });
Now you're not just handling webhooks, you're handling them securely. Nice work!
Want to test locally? ngrok is your new best friend. Just run:
ngrok http 3000
And use the generated URL when registering your webhook. Confluence's webhook logs are also a goldmine for troubleshooting, so don't forget to check those out if things get weird.
A few pro tips to keep your webhook game strong:
And there you have it! You're now a Confluence webhook wizard. Remember, with great webhook power comes great responsibility. Use them wisely, and your Confluence integration will be the talk of the town.
Happy coding, and may your webhooks always be plentiful and error-free!
Here's a full example putting it all together:
const express = require('express'); const axios = require('axios'); const crypto = require('crypto'); const app = express(); app.use(express.json()); const verifyWebhookSignature = (req, res, next) => { // Signature verification code here }; app.post('/webhook', verifyWebhookSignature, (req, res) => { const { event, page } = req.body; switch (event) { case 'page_created': console.log(`New page created: ${page.title}`); break; case 'page_updated': console.log(`Page updated: ${page.title}`); break; case 'page_deleted': console.log(`Page deleted: ${page.title}`); break; default: console.log(`Unhandled event: ${event}`); } res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000')); const registerWebhook = async () => { // Webhook registration code here }; registerWebhook();
Now go forth and webhook like a champion!