Advanced | Flow of the Week: Get an email with a list of places in your current location based on a Point of Interest
The following is a FOTW blog written by Flow Community member Ahmad Najjar (Twitter: @ahmadn82). This tutorial highlights how to use Microsoft Flow to get an email with a list of places in your current location based on a point of interest. Let's begin.
Have you ever ended up stranded in a city, not knowing which tourist attraction to hit, which restaurant to go to or where is the nearest grocery store? Well, today you are saved! Because the solution is simply punching one Flow button!
For this scenario, we’ll create a Button flow that collects a point of interest (POI) from you. It will then find places based on the POI you passed (Museums, Restaurants, Things to do, etc…), and list those places. You’ll then get an email with a table of the places found, along with some details about those places (rating, address, etc…).
This flow will demonstrate some advanced concepts, including calling a public API using the HTTP action, parsing returned body from the API as JSON, and building an HTML email.
Trigger the Flow
The flow will start out simple with a Button trigger.
On the Button trigger, we’ll have one input to collect a POI. Next, we’ll call Google’s places API and we’ll pass the POI entered along with some Dynamic Content from the Button trigger.
Calling the API
Calling the API is straight forward, we’ll be using the HTTP action in Flow. Select Get as the Method type and in the Uri we’ll be passing the following: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=[LATITUDE],[LONGITUDE]&rankby=distance&keyword=[POINT_OF_INTEREST] in [CITY]&key=[YOUR_API_KEY]
Latitude, Longitude and City are Dynamic Content coming from the Button trigger, while Point of Interest is the input value entered through the Button trigger.
Next, we need to parse the returned JSON from the HTTP action and loop through the returned places in that JSON.
We’ll pass the body returned from the HTTP action which contains our JSON. Note that we need to pass in the schema for the JSON returned. The schema must match the JSON payload, so the properties from the JSON is mapped correctly, however we don’t have to define all the properties in our schema for the returned JSON, we only need to define the properties that we are going to use later in our Flow.
For the reference, our schema should look something like the figure below:
Create HTML Table
At this point, we could use the Create HTML table action, pass the results property from Parse JSON action, define custom columns and then send the output in an email.
However, we want the email look neat by doing some extra formatting!
Building an HTML email using string variables
We’ll build the HTML email by creating a string variable – a variable is an easy way to collect data each time an action happens inside of a loop. Whenever you use a variable, first you need to use the Initialize variable action. You can name your variable whatever you like, but you need to be sure to set the Type as String. First, we’ll initialize the Table of Places variable, and we’ll pass the table opening tag and header in the value field.
Then, we’ll initialize another String variable to format the “open_now” value returned from the API, so instead of showing true/false, we want to show Yes/No.
Apply to each
Inside the Apply to each we are going to construct the email. For each result (Place) we’ll set a condition to check the “open_now” property, if it’s true we set the “Open” variable to Yes otherwise we set it to No.
Next, we add a row to our table with the details of that Place by using the Append to string variable action, we’ll append these details to the “Table of Places” variable we initialized earlier.
One more formatting we’ll do, is making our Place name linkable, whereas when clicked it will navigate to the exact location of that Place in Google maps! For this, we’ll wrap the Place name with an HTML anchor and we’ll construct the href as follows: http://maps.google.com/maps?q=[LATITUDE],[LONGITUDE] (Note: Latitude and Longitude are from the API results)
We need to add two last actions after the Apply to each. We need to close out that HTML table with another Append to string action.
And now we’re ready to send the email. In the Send an email action we can fill out the To and Subject fields with whatever we’d like. In the Body, all we need to do is use the “Table of Places” variable. Finally, we need to set Is HTML to yes, otherwise we won’t get the table in the correct format!
The final Flow would look like this:
Running the Flow
Finally, if you want to run this flow from your phone or your desktop, just enter a point of interest you want to find nearby, and you’ll immediately get a list of places – with helpful details about those places!
Thanks again to the Microsoft Flow team for collaborating with us to bring you the Flow of the Week!
If you have any questions or comments, please leave them in the comments below or post your questions in the Flow Community!
Until next time.
P.S. To never miss another blog post from the Flow blog – us this Flow.