Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saniales/ddeabd442ddd9fa0889ef9294a1f8b98 to your computer and use it in GitHub Desktop.
Save saniales/ddeabd442ddd9fa0889ef9294a1f8b98 to your computer and use it in GitHub Desktop.
A TransferWise integration with Slack Webhooks using Cloudflare Workers

TransferWise webhook integration with Slack notifications using Cloudflare Workers

Greetings from the Tryvium Team,

Here is how to integrate a Transferwise webhook with Slack notifications. We have chosen to use cloudflare workers as a perfect case study for serverless applications using cloudflare.

Setting things up

  1. First of all set up things on Transferwise side

  2. Secondly create an incoming webhook

  3. Finally take the script below and create a new cloudflare worker, filling the SLACK_WEBHOOK_URL variable with your actual URL.

MIT License
Copyright (c) 2019 The Tryvium Company LTD
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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})
}
@lucaspar
Copy link

lucaspar commented Feb 3, 2021

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:

curl -X POST \
     -H 'Content-type: application/json' \
     --data '{"subscriptionId": "sid_test", "profileId":"profile_test", "amount":"1000", "currency":"USD", "eventType":"transfer"}' \
     https://<WORKER_SUBDOMAIN>.workers.dev/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment