Last active
August 19, 2024 09:34
-
-
Save shellking4/8927c91f63f06501074c153cf6394aaf to your computer and use it in GitHub Desktop.
bomboo-webhook-snippet
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
@Post('bomboo-webhook') | |
@Accessible() | |
async bombooWebhook( | |
@Req() request: RawBodyRequest<Request>, | |
@Res() response: Response | |
) { | |
const endpointSecret = this.configService.get('BOMBOO_WEBHOOK_SECRET'); | |
const signature = request.headers['x-bomboo-signature'] as string; | |
const { rawBody } = request | |
const isValidSignature = isValidHmacSignature(signature, rawBody, endpointSecret); | |
if (isValidSignature) { | |
let webhookData = JSON.parse(rawBody.toString()); | |
console.log("WEBHOOK_DATA", webhookData) | |
switch (webhookData.event) { | |
case BombooWebhookEvent.SMS_PENDING: | |
this.eventEmitter.emit(BombooWebhookEvent.SMS_PENDING, webhookData); | |
break; | |
case BombooWebhookEvent.SMS_SENT: | |
this.eventEmitter.emit(BombooWebhookEvent.SMS_SENT, webhookData); | |
break; | |
case BombooWebhookEvent.SMS_DELIVERED: | |
this.eventEmitter.emit(BombooWebhookEvent.SMS_DELIVERED, webhookData); | |
break; | |
case BombooWebhookEvent.SMS_FAILED: | |
this.eventEmitter.emit(BombooWebhookEvent.SMS_FAILED, webhookData); | |
break; | |
case BombooWebhookEvent.MAIL_PENDING: | |
this.eventEmitter.emit(BombooWebhookEvent.MAIL_PENDING, webhookData); | |
break; | |
case BombooWebhookEvent.MAIL_SENT: | |
this.eventEmitter.emit(BombooWebhookEvent.MAIL_SENT, webhookData); | |
break; | |
case BombooWebhookEvent.MAIL_DELIVERED: | |
this.eventEmitter.emit(BombooWebhookEvent.MAIL_DELIVERED, webhookData); | |
break; | |
case BombooWebhookEvent.MAIL_FAILED: | |
this.eventEmitter.emit(BombooWebhookEvent.MAIL_FAILED, webhookData); | |
break; | |
default: | |
break; | |
} | |
response.status(200).send('ok'); | |
} else { | |
response.status(400).send('Invalid signature'); | |
} | |
return; | |
} | |
export const isValidHmacSignature = (hmac: string, data: string | Buffer, secret: string) => { | |
const generatedSignature = crypto | |
.createHmac('sha256', secret) | |
.update(data) | |
.digest('hex'); | |
const isValidSignature = generatedSignature === hmac; | |
return isValidSignature; | |
} | |
export enum BombooWebhookEvent { | |
SMS_PENDING = 'SMS.PENDING', | |
SMS_SENT = 'SMS.SENT', | |
SMS_FAILED = 'SMS.FAILED', | |
SMS_DELIVERED = 'SMS.DELIVERED', | |
MAIL_PENDING = 'MAIL.PENDING', | |
MAIL_SENT = 'MAIL.SENT', | |
MAIL_FAILED = 'MAIL.FAILED', | |
MAIL_DELIVERED = 'MAIL.DELIVERED' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment