Webhooks
Description
The handler can be triggered not only when a new packet is received from a device, but also by an external call - a webhook. The handler can be triggered not only when a new packet is received from the device, but also with the help of an external call - a webhook. To work with webhook, click Edit on the card of an existing handler.
In the handler edit box, click Create Webhook.
A webhook URL will be generated. Copy it and add it to the desired service. Field :id field in the webhook URL replace it with the identifier of the object on which you want to run this handler. this handler.
This identifier is in the _id
field in the API link of the object.
Also, this identifier is visible in the search box when clicking on the desired object.
Additional parameters for the handler can be passed in the body of the request and then get them in the code as follows:
- Body
{ "param1": 123, "param2": "text" }
- handler code fragment
const body = ric.webhook?.request?.body;
const param1 = body.param1;
const param2 = body.param2;
When working with the ric.webhook
object, the code editor prompts for possible options.
Example Usage
Let's look at an example of adding a button in a service IFTTT (opens in a new tab), when you press on button that sends coordinates from a cell phone to an object in the platform.
Handler
- Create a handler that extracts the coordinates from the request body and returns them in the output parameters. If necessary, you can additional processing of the retrieved coordinates. Also, if required, define the response format that will be sent in response to the webhook.
function process() {
const body = ric.webhook?.request?.body ??? {};
const lat = +(body.lat || 0);
const lon = +(body.lon || 0);
return { lat, lon }
}
Handler Code (opens in a new tab)
-
Create a webhook for the handler.
-
Add the handler to the object.
-
Perform a mapping between the output parameters of the handler and the arguments in the model of the object.
Button
- Create a button in the IFTT service. To do this, make a new Applet and add the Button press action to the If block the Button press action and to the Then block the Make a web request.
You can create and edit an Applet in both the web interface and the mobile app. mobile app IFTT (opens in a new tab). But you can only use the Applet directly (in this case, by clicking on the button). can only be used in the applet.
For the Make a web request action, fill in the required fields. In the URL of the webhook specify the identifier of the object where the coordinates should appear.
- Click Continue, specify the name of your Applet and click Finish.
- Applet is ready. To use it, create a widget with it in the IFTT mobile app. IFTT application.
- Press the button. The coordinates from your cell phone will be sent to your site.
- Check that the coordinates sent do match the coordinates that are displayed on the object. coordinates that are displayed on the object.