const http = require('http');
const crypto = require('crypto');
http.createServer((req, res) => {
const webhookId = req.headers['x-webhook-id'];
const webhookTimestamp = req.headers['x-webhook-timestamp'];
const signature = req.headers['x-webhook-signature'];
let body = '';
req.on('data', chunk => {
body += chunk;
});
req.on('end', () => {
// Obtain Secret
const secret = "xxx"; // Obtain from https://pollo.ai/api-platform
const signedContent = `${webhookId}.${webhookTimestamp}.${body}`;
// Calculate the signature
const secretBytes = Buffer.from(secret, 'base64');
const computedSigBase64 = crypto
.createHmac('sha256', secretBytes)
.update(signedContent)
.digest('base64');
// Verify the signature
if (crypto.timingSafeEqual(Buffer.from(signature, 'base64'), Buffer.from(computedSigBase64, 'base64'))) {
// Signature is valid, process the message
const event = JSON.parse(body);
if (event.status === 'succeed') {
// Handle success event
console.log('Task succeeded:', event.taskId);
} else {
// Handle success event
console.log('Task failed:', event.taskId);
}
} else {
// Signature verification failed, reject processing
console.log('Invalid signature');
}
res.end('OK');
});
}).listen(8080);