As you might already know, IFTTT is an easy online tool that allows people with no specific tech skills to create rules (called “recipes”), which connect an event stemming from a service (e.g email) to an action executed by another service (e.g. twitter). Service providers are called channels and you can only create rules from available channels.

The beauty and the success of IFTTT lie in its simplicity. However, this is also one of its weaknesses: recipes are often too simple and cannot do much more than trigger a single action, which does not cater to many real-life situations. For example: consider the recipe that specifies “if a water leak is detected, send an email notification”. What if you also wanted to turn off the water system, send an email to a plumber and shut down electronic appliances if the leak was above a given threshold?

In other terms, IFTTT if not designed to implement orchestrations and define business rules, which is actually the realm of scriptr.io. In this tutorial, we propose to explain how to get the best of both worlds: combine the simplicity of IFTTT to the power of scriptr.io, by allowing IFTTT end users to invoke the complex logic implemented in your scriptr.io scripts.

Purpose of this tutorial

In the below we will learn how to create an IFTTT recipe that allows you to invoke any script in your scriptr.io account, from an IFTTT endpoint. On that purpose, we will use IFTTT’s maker channel that allows makers to connect IFTTT to their own APIs.

As a reminder, an IFTTT (if “this” then “that”) recipe contains two main components: this, which defines an event to react to, and that, which defines the action to execute upon the occurrence of the event. In our case, we will match this to an incoming request on a specific endpoint, whereas that will forward the request to one of our scripts on scriptr.io.

Prerequisites

  • You need to have signed-up to scriptr.io
  • You need to have signed-up to IFTTT
  • You will also need your IFTTT api key to test your recipe. You can find it here,
  • Ideally you should also have a Google developer account and/or an Google API key as it is needed for the scriptr.io script used in the tutorial. If you don’t, that’s OK, just use the demo script we’ve deployed for you (more on this below).

Create a recipe

Sign-in to your IFTTT account and click on My Recipes then on “Create a Recipe”.

iftttt-create-recipe

This

Click on the highlighted “this” to define a trigger condition. In the list of all available channels that appears, select the “maker” channel.

ifttt-maker

Since we are using the maker channel, the only available trigger is “Receive a web request”, which is exactly what we need. Clicking on the corresponding link takes you to a form field asking for an event name. Type “scriptrio” (or any other word that suits you), then click on “Create trigger”. The “event name” is actually added to the IFTTT endpoint that you can expose to your end users (https://maker.ifttt.com/trigger/event_name/with/key/your_ifttt_api_key)

ifttt-event

That

Click on the highlighted “that”. Once again, we are presented with a list of possible channels. Since our purpose is to invoke a script on scriptr.io, we’ll select the maker channel.

ifttt-action-channel

Click on “Make a web request”, the only available option.

ifttt-make-request

The form that appears allows us to configure the way we will invoke our scripts in scriptr.io. If you are not familiar with IFTTT, note that the latter provides you with 5 parameters that you can use to build the action:

  • “Value1”, “Value2” and “Value3”, are the predefined names of the parameters that are initially (and optionally) received by the trigger request, i.e. they are used by your end users to pass data (as strings),
  • “EventName” and “OccurredAt” are two other predefined parameters that respectively contain the event name of the trigger and the time stamp of the request (automatically filled by IFTTT).

You can use any of these parameters to build the action in any of the form fields. In this tutorial, we will use Value1 to specify the name of the script we wish to trigger on scriptr.io and Value2 will be a stringified JSON structure holding a map of parameters to send to the specified script.

  • In the URL field, type “https://api.scriptrapps.io”, which is the default prefix to your scripts on scriptr.io, then add {{Value1}} to specify the path to the script to execute, and append “?auth_token=YOUR_SCRIPTR_TOKEN” to pass your user’s authentication token. If you do not have a scriptr.io account or a Google API key, use RzM1RkYwQzc4Mg== as your scriptr.io token (the latter points to a demo script that is already deployed on a test account),
  • In the method field select “POST”, since we need to send data to our scripts,
  • In the Content-Type field, select “application/x-www-form-urlencoded”,
  • In the body field, you specify what parameters to send along with the action request: type params={{Value2}}, i.e. we pass the stringified JSON object we received as is to the script, through a request parameter called “params”.

ifttt-create-action

Once done, click on “Create”. In the last step, specify what title you would like your recipe to have and check or un-check the “Receive notifications” option. Finally click on “Create Recipe”.

Create a test script

Let us now move to scriptr.io and implement a test script that will be invoked by our recipe. We suggest creating a simple script that expects to receive a stringified JSON object with location, type, radius (in meters) and email properties. The script passes the location, type and radius values to a Google Places API to find the nearest places of the given type (e.g “restaurant”). It then sends the result by email using the received email value.

Note: if you do not have a Google developer account and/or a Google API Key that is fine. Don’t copy/paste the below into your scriptr.io account as a new script. Instead, just use the demo script

var http = require("http");

try {
  
  // we retrieve the received json from request.parameters and we parse it since we know
  // it is a stringified json
  var parameters = JSON.parse(request.parameters.params);
  var location = parameters.location;
  var type = parameters.type;
  var radius = parameters.radius;
  var email = parameters.email;

  // Your Google API key
  var googleApikey = "YOUR_GOOGLE_API_KEY";

  // Prepare the request to send to Google Places API
  var requestParameters = {

    url: "https://maps.googleapis.com/maps/api/place/nearbysearch/json",
    params: {
      location: location,
      type: type,
      radius: radius,
      key: googleApikey
    }
  };

  var response = http.request(requestParameters);
  var body = JSON.parse(response.body);
  var results = body.results;

  // In the below, we simplify the structure returned by Google to the following {location:"lat,long", name""xxx", vicinity:yyy"}
  var places = [];
  for (var i = 0; i < results.length; i+a+) {

    var place = {};
    place.location = results[i].geometry.location.lat + "," + results[i].geometry.location.lng;
    place.name = results[i].name;
    place.vicinity = results[i].vicinity;
    places.push(place);
  }

  // Send the obtained places by email then return them to the caller
  // We prettify the JSON array and render it as an HTML table
  var prettyHtml = "

“; for (var j = 0; j < places.length; j++) { prettyHtml += ”
“; } prettyHtml += ”

Name Location Vicinity
” + places[j].name + “ ” + places[j].location + “ ” + places[j].vicinity + “

” sendMail(email, “scriptr.io place locator”, “Your nearest ” + type, prettyHtml); return places; }catch(exception) { return exception; }

Name the above script “findPlace” and save it in your scriptr.io account

Try it!

In order to test your recipe, you just need to issue an HTTP request to the following endpoint: https://maker.ifttt.com/trigger/your_event_name/with/key/your_ifttt_api_key

Notes: Make sure that your maker channel is connected on IFTTT

curl -X POST -H "Content-Type: application/json" -H -d '{
    "value1" : "findPlace", 
    "value2" : "{\"location\":\"40.7441855,-73.995394\",\"type\":\"restaurant\",\"radius\":\"250\",\"email\":\"YOUR_EMAIL\"}"
}' "https://maker.ifttt.com/trigger/scriptrio/with/key/YOUR_IFTTT_API_KEY"

Note: observe that although IFTTT’s recipe creation wizzard uses “Value1”, “Value2” and “Value3” when defining the action, you actually have to send “value1”, “value2” and “value3”.