An example of using Microsoft Flow or How to give a wife flowers

Original author: Ivan Iakimov
  • Transfer
  • Tutorial
Here I will demonstrate a not quite simple example of using Microsoft Flow to solve one practical problem.

Formulation of the problem


Sometimes tasks for a programmer arise from everyday life. It was one of those cases. I wanted to give my wife flowers from time to time, say once a month. But at the same time I did not want to do this on any particular date, for example, on the first day of the month. Then it would not be a surprise. I wanted randomization. For example, the next time I give flowers should be a month after the previous one, plus / minus a couple of days.

There is nothing special about these requirements. It's easy enough to write a program that reminds me of these events. But the desktop application has a significant drawback. It will work only on one machine, while I have several (at home, at work, ...). And there's a smartphone ... Wouldn't it be great to receive notifications on any of my devices?

Well, you can save information about events in any online storage (for example, Mlab ). In this case, all instances of my application will work with the same information, which will allow them to be synchronized with each other. However, I will need to install these applications on all my computers. In addition, I use Windows on computers and Android on a smartphone, so I have to write several different applications if I really want to receive my notifications everywhere. How to overcome this problem? Using the Web, of course.

If I create a Web application, I can use it on almost any device. Wonderful! But still not great enough. Let me explain my point. Although it is not so difficult to write a Web application that solves my problem, nevertheless, in this case, I will have to take care of a lot of things. We need to think about hosting, data storage, code storage, ... And why? Almost all of the required functionality is already implemented in modern Web calendars, such as Google Calendar . He can create events, make them repetitive, send me notifications about their occurrence, etc. The only thing that is not there is the randomization required by me. Wouldn't it be great if I could just add this feature and use all the other features already available?

When I thought about this issue, I came across the site IFTTT.com. The idea of ​​this site is simple, but productive. If something happens, he does something. I know this sounds strange, so let me give you a number of examples. If I received an email from a specific person, send me an SMS. If my favorite author has posted a new article on his blog, let me know in Slack. Or if it's time for a specific event in the Google Calendar, send me an email. I hope, now I understand what I am leading to. This service can monitor certain events (they are called triggers) and perform some actions when these events occurred. IFTTT supports a huge number of possible triggers and actions. I could track my events in the Google Calendar, and when a certain event occurs, I would send myself an email, delete the old event and add a new one for a later time. Great! That's it, what is needed! But not really.

First of all, IFTTT allows only one action per trigger. This is not such a big problem, because You can create multiple applets with identical triggers (an applet is a combination of a trigger and an action in IFTTT). One will send me a letter, the other will delete the old event, the third will create a new event. But there is a more serious obstacle. For a new event, I need to create a trigger time with randomization. And I have not found how to do this in IFTTT. This means that this service cannot solve my problem. But maybe the Internet has other similar services? Yes there is.

The next site I reviewed was Zapier. Here we can use several actions for one trigger, which is good. But it is available only for money, which is not so good. I have not played with Zapier for a long time, but I got the impression that it also does not give me the opportunity for randomization. Although here I may be wrong. In any case, I moved on to the next candidate.

It was Microsoft Flow . This service allows me to track 750 trigger triggerings per month for free. It is more than enough for my needs. Moreover, it has support for expressions, among which is the rand () function ! This is exactly what you need. Now let me show you how to solve the described problem using Microsoft Flow.

Decision


First you need to create a new flow. A thread is a combination of a trigger and an action. To do this, register on the site and in the main menu click on My flows , and then on Create from blank :



Click the Create from blank button:



You will be asked to select a trigger. Enter "calendar" in the search field and select Google Calendar :



From the list of available triggers for Google Calendar, select When an event starts :



Here you may be asked to allow access from Microsoft Flow to Google Calendar on your behalf.

Now the trigger is ready. Its only parameter is the calendar:



Yes, you can create multiple calendars in Google Calendar. Each event belongs to one of the calendars, and you need to choose a calendar whose events will trigger your actions.

There is nothing wrong with creating a new calendar and putting into it all the events for which your actions should work. But if not all events in the calendar should be processed, you can use filtering. Click New step and then Add a condition :



This will create a filter for you. Now we need to decide exactly how we want to filter the events. For example, I want to process only those events that contain the text "[RANDOM]" in the "location" field. To do this, click on the input field. Choose value. Microsoft Flow will show you a list of possible values ​​that you can work with:



Click on Event List Event Event Location . And fill in the remaining fields with the appropriate values:



Pay attention to the Edit in advanced mode link . She is very helpful. If you click on it, you will get a representation of the same condition in the form of a text expression:



This is very helpful when it comes time to write your own expressions.

Now you can add actions to our events. In the If yes branch of your filter, click the Add an action link . Here I’ll create an action that will send me an event notification to Gmail:



As you can see, you can use data from your event to fill in the Subject, Body, and other properties of this action.

Now we have notifications. It's time to delete the old event in the calendar and create a new one for a later time. Using the Add an action link, create the action Delete an event for Google Calendar:



It should be said that it is quite possible to simply change the event in the calendar instead of deleting the old one and creating a new one. But here I will still stick to the last option to show you another possibility of Microsoft Flow. Suppose I want to create a new event in the calendar did not happen after removing the old, but in parallel with this. Hover over the arrow between actions. Send emailand Delete an event . A plus sign will appear on the screen. Click on it and select Add a parallel branch → Add an action in the context menu:



Now we can add a parallel action Create an event from Google Calendar . For this action, we will take the title, description, and location from our original event. The only thing left to do is to set the start and end time of the new event:



Now we come to the really interesting part. We need to somehow add a random number of days to the start time of the current event. We will use this new date as the beginning of the event being created. For example, I want to add 30 days plus / minus 2 days. Documentation on the available features can be found here . I admit, this is not a very simple reading. I had a lot of questions, especially about how to extract the start time from the original event so that it can be used in functions. Some help can be obtained from our filter. Remember the Edit in advanced mode link :



Clicking on it shows the corresponding expression:



This gave me some clue in writing my expressions. Now click on the Start time field.Create an event events and select the Expression tab :



In the input field, record the following expression:

addDays(triggerBody()?['start'], add(30, rand(mul(2, -1), add(2, 1))))

and click the Ok button . This expression exactly solves our problem: it adds 30 days plus / minus 2 days to the start time of the original event. But what if I need some flexibility? What if I want to have several types of events? For events of the first type, I will increase the start time by 30 days plus / minus 2 days, for events of the second type - by 14 days plus / minus 3 days, etc. How is it possible to achieve this?

Here is one way. Remember that we store the string "[RANDOM]" in the "location" field of our events? Now we will write additional information in this field. Its content will be text in the format of "[RANDOM], NN, MM", where NN is two digits and MM is also two digits. We will increase the time of occurrence of events for NN days plus / minus MM days. When using this format, I can be sure that the characters in positions 9 and 10 (starting from 0) the lines will represent NN, and the characters in positions 12 and 13 are MM. And here is an expression that uses this new information storage format to increase the start time of an event:

addDays(triggerBody()?['start'], add(int(substring(triggerBody()?['location'], 9, 2)), rand(mul(int(substring(triggerBody()?['location'], 12, 2)), -1), add(int(substring(triggerBody()?['location'], 12, 2)), 1))))

It uses the substring function to select the desired parts from the string, and the int function to convert them to an integer form.

Great! We almost reached our goal. It remains only to set the end time of the new event. And here we meet our last obstacle. It is necessary that the end time of an event be equal to the time of its beginning plus 15 minutes. Microsoft Flow supports the addMinutes function , and we could write an expression of the type:

addMinutes(<предыдущее выражение>, 15)

However, due to the nature of the rand function , we end up with a value that is not related to the value for the start time of the event. Instead, it would be nice to have the 'nextStart' variable that stores the value of our expression. Then you could use its value for the start time of the event, and use

addMinutes(<значение переменной 'nextStart'>, 15)

for the time of its end. And you know what? Microsoft Flow supports variables. First we need to initialize it. Mouse over the arrow between the trigger and the filter. Click the plus sign and select Add action :



Type “Variables” in the search field and select Initialize variable :



Set the name of the variable 'nextStart' and the type “String”. Microsoft Flow does not have a separate type for dates and times, it uses strings everywhere.



Now you need to set the value for this variable. This cannot be done right here, since it is not yet known whether this event is correct. Only after the filter can we be sure of it. Therefore, we add another action of the Set variable type after sending the email notification:



Here we set the value of the created variable to our long expression. It remains only to reuse this variable in the expressions for the start and end time of the event. You can refer to a variable in an expression like this:

variables('nextStart')

Therefore, the expression for the end time of the event will be:

addMinutes(variables('nextStart'), 15)



Conclusion


That's the end of the story. You just need to save the flow, and Microsoft Flow will launch it for us.

I hope this article will be useful for you. For me, Microsoft Flow has proven to be a great task automation tool.

Also popular now: