|
addEventListener('fetch', event => { |
|
event.respondWith(handleRequest(event.request)) |
|
}) |
|
|
|
const SLACK_WEBHOOK_URL = "<Your slack custom integration webhook URL>" |
|
|
|
/** |
|
* Respond to the request |
|
* @param {Request} request |
|
*/ |
|
async function handleRequest(request) { |
|
// THIS SCRIPT DOES NOT CHECK AGAINST REQUESTS OUTSIDE FROM TRANSFERWISE |
|
// NOR IT PERFORMS SIGNATURE CHECK. Feel free to comment with a fix :) |
|
if(request.method === "POST") { |
|
const requestJSON = await request.json() |
|
const { subscriptionId, profileId, amount, currency, eventType } = requestJSON |
|
if(eventType) { |
|
// BALANCE NOTIFICATION EVENTS |
|
const slackPayload = { |
|
text : `_*BALANCE NOTIFICATION*_: |
|
> *SUBSCRIPTION ID*: ${subscriptionId} |
|
> *PROFILE ID*: ${profileId} |
|
> *AMOUNT*: ${currency} ${amount} |
|
> *EVENT TYPE*: ${eventType}` |
|
} |
|
await fetch(SLACK_WEBHOOK_URL, { |
|
method: "POST", |
|
body: JSON.stringify(slackPayload) |
|
}) |
|
} else { |
|
// TRANSFER UPDATE NOTIFICATION EVENTS |
|
const { subscriptionId, profileId, resourceId, newState, eventTime } = requestJSON |
|
if (eventTime) { |
|
const slackPayload = { |
|
text : `_*TRANSFER UPDATE NOTIFICATION*_: |
|
> *SUBSCRIPTION ID*: ${subscriptionId} |
|
> *PROFILE ID*: ${profileId} |
|
> *RESOURCE ID*: ${resourceId} |
|
> *NEW STATE*: ${newState} |
|
> *EVENT TIME*: ${new Date(eventTime).getUTCDate().toLocaleString()}` |
|
} |
|
} |
|
} |
|
} |
|
// It is important to return 200 (and not for example 204) |
|
// to test the webhook on Transferwise side. |
|
return new Response("", {status: 200}) |
|
} |
Nice script, the Transferwise test won't show up in Slack, so you can run a complete test by invoking the worker endpoint with a payload from a terminal: