Daily Commmuter App
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:
-
In the request URL we are looking for the duration between Grand Central Station (longitude of
-73.979412
, latitude of40.752834
) and the World Trade Center (longitude of-74.012215
, latitude of40.711483
). Visit https://docs.mapbox.com/playground/directions to get the coordinates for your location and just replace them in the URL. -
We are using
driving
in the request URL as the routing profile. You can change it towalking
orcycling
if that’s how you commute. -
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):
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.
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}}