This app will notify you each morning how long your commute will be. You can customize it to notify you only if there’s traffic.

Time Frequence

The default template will execute daily in the morning. You can adjust the timing and time zone under Condition in the top action block.

HTTP Request

The HTTP action sends a request to a an API of https://www.mapbox.com/. The default template sends a request to:

https://api.mapbox.com/directions/v5/mapbox/driving/-73.979412,40.752834;-74.012215,40.711483?geometries=geojson&access_token=pk.eyJ1Ijoid2Vlam9obnkyIiwiYSI6ImNsa2EzM2hoeDAydHczZnF0NGFrNTV0aDYifQ.yFoNbo-dYwI8L-A3R_aQbA
  1. In the request URL we are looking for the duration between Grand Central Station (longitude of -73.979412, latitude of 40.752834) and the World Trade Center (longitude of -74.012215, latitude of 40.711483). Visit https://docs.mapbox.com/playground/directions to get the coordinates for your location and just replace them in the URL.

  2. We are using drivingin the request URL as the routing profile. You can change it to walking or cycling if that’s how you commute.

  3. We are using a demonstration token in the end of the request URL. Since it might hit the request limit, you may want to create an own free account on Mapbox and replace with your own token.

If we make a request to that API, here’s an example of output data we receive (shortened):

{
  "steps.get-commute.outputs": {
    "Body": {
      "code": "Ok",
      "routes": [
        {
          "distance": 8835.942,
          "duration": 1125.284,
          "geometry": {
            "coordinates": [
              [-73.979395, 40.752857],
              ...
              [-74.010788, 40.710999]
            ],
            "type": "LineString"
          },
        }
      ],
    ...
    }
  }
}

The data we are interested in is {{steps.get-commute.outputs.Body.routes.[0].duration}}. In this example the value for this is 1125.284 which is 1125 seconds.

Variable

In the next action block we are assigning a variable to {{steps.get-commute.outputs.Body.routes.[0].duration}} and will call this value Duration going forward. This step is optional but makes it easier to expand the the app if we want to.

Database Query

Since Mapbox is providing us with the duration time in seconds, we will to convert to minutes and hours through SQL as seconds are not really handy for an alert message.

SELECT FLOOR({{steps.duration.value}} / 3600) AS hours, FLOOR(({{steps.duration.value}} % 3600) / 60) AS minutes;

We are using {{steps.duration.value}} in the calculation which is the variable value for duration we created in the previous step.

Check duration time

Before sending out the message through Slack we check through an If Condition if the duration is longer than an hour so we can properly format our text.

Specifically we check if the outcome of our SQL calculation euals zero with:

  • {{steps.covert-to-minutes-hour.outputs.Result.[0].hours}} = 0

Ping Message

If our condition is met we will communicate our result in minutes through:

  • {{steps.covert-to-minutes-hour.outputs.Result.[0].minutes}}

If our condition is not met, we will communitate our result in hours and minutes through:

  • {{steps.covert-to-minutes-hour.outputs.Result.[0].hours}} &
  • {{steps.covert-to-minutes-hour.outputs.Result.[0].minutes}}