Managing functions
Functions allow you to define your own logic on what should happen when request is received.
Last updated
Functions allow you to define your own logic on what should happen when request is received.
Last updated
Webhook Relay Functions provide an easy way to extract values from webhooks and transform HTTP requests before passing them to their final destination. Function can be reused by any number of Inputs and/or Outputs. Since user doesn’t have to run a server, this type of function service is also known as FaaS (Function as a Service) or Serverless.
Functions on Inputs transform webhooks when they enter the system and also allows you to customise both response that Webhook Relay will return and webhook request that will be passed along to all outputs.
Functions on Outputs allow you to customise webhooks for each destination. This way you can have a custom webhook arriving at each destination (Slack, Mattermost or some CRM system like HubSpot or Zoho).
Currently, you can write functions in Lua language which is perfect for most of the tasks.
Functions can be managed using relay CLI or through the web dashboard. You can create, edit and delete functions:
To create a new function, simply use relay function create
command:
You can also specify name, source and driver using optional flags:
--driver lua or wasm (defaults to the extension.)
--name name of the function (defaults to the name of the file)
--source points to the location of the function file (usually .lua (version 5.1) or .wasm)
In this example we will use a Lua 5.1 function that takes webhook JSON as an input and converts it to a Slack message JSON to display an alert for us in chat.
Mailgun JSON (shortened version, usually it’s a bit bigger):
Below is a Lua function that converts Mailgun webhook into a Slack webhook:
This function transforms it into a JSON that can be accepted by Slack incoming webhook API:
To add this function to your account so we can use it later, type:
The easiest way to start using Functions is with the forward
command:
This command will create a new bucket, input and output. Function will be attached to the output that’s forwarding to https://hooks.slack.com/services/xxx
endpoint.
Alternatively, you can attach Functions to existing Inputs:
or Outputs
To stop using some particular function in your Input or Output, just update Function to “”:
You can view your uploaded functions via relay function ls
command:
To update a function, you have to specify the name and source location:
To delete a function, first ensure that any Input or Output doesn’t use the function anymore. Once it’s done: