Festive Automatic Reply Using Power Automate

As we all go away for the Christmas and New Year holiday, the Exchange mailbox automatic reply feature goes on. You might sit there for a bit thinking of something amusing yet inoffensive to write, then just end up writing some dry, factual content about when you’re back.

Well, let me help you past your writers block with step by step instructions on how to automatically reply to emails with a random joke.

This flow uses a single column SharePoint list to store email addresses of incoming messages, and uses the Office 365 Outlook connector, but you could use another data source to store your list of email addresses and of course you could be using Outlook.com or Gmail etc, it really doesn’t matter.

We also use premium connectors here: namely HTTP, but you can sign up for/extend a free trial to cover you over your holiday period, use Logic Apps or indeed pay for a per user Power Automate plan.

Anyway, let’s start:

  1. Create a SharePoint list. Straight out of the box – you only need Title

Create a new flow. The trigger is When a new email arrives (V3). Everything you can see here is left on the defaults

You don’t want to trigger on automatic replies, so we set up a trigger condition. Go into the settings of the action:

Get items from the SP list. There’s an OData filter query here to get items created since midnight local time.

I love living New Zealand except the fact midnight UTC is smack bang in the middle of the day here, so we have to perform these time zone gymnastics to get the start of the working day from a data source that works in UTC. If you’re within a few hours of UTC your own expression can be much simpler, because who cares when your cutoff point is, as long as it’s kind of overnight.

Anyway, the reason for this is we want to get email addresses of senders that have sent email today and not reply to them. This is a safeguard to prevent auto-reply loops and stop some joker smashing your inbox for laughs.

Create a single column array with the contents of Title from the SP list:

If the array contains the from address of the sender, exit the flow, else add them to the list:

Now we know we’re going to do the thing, initialise a couple of variables. One of the joke and one for the first name of the sender. Both of these are optional really, but I have some branching and error handling that means I need these.

Now I have a scope that contains a few actions. These actions are intended to parse the first name of the sender from the message. Not every sender comes with a from address. Usually it goes like From: Will Page <will.page@somedomain.com> but it might just be From: will.page@somedomain.com

These actions will grab that name if it exists. I put a bit of effort into making sure it never fails, even if there’s no from name but it still might. I’m going to be on holiday and I don’t really want to debug this so I put all the actions in a scope, and set the next action after the scope to run even if the scope fails. That’s also why I use the variable to hold the name.

The first action, Export message, is pretty straightforward. Next, Compose and put a new line in there. Next, Filter array:

The Filter array is acting on the output of the Export message action, split on the new line.

Split() outputs an array – filter it for lines starting with From:

The next two actions are a bit trickier:

The first action is really two in one. First(body(‘Filter_array’)) effectively turns this one item array into a string. Substring() is used to pick out the bit that starts after From: by ignoring the first 6 characters. Substring has 3 inputs; The original string, the starting index (in this case, 6 chars in) and the length, which in this case is the length of the original string minus 6. We have to use the original string twice in this expression so we turn the single item array into a string twice. This could really be done in two actions.

Next, we get the senders first name by splitting the result of the previous action on a space character and taking the first item from that. However, as I said earlier there may not be a name here, and the input to this action might not have a space in it.

To get past this, there’s an if(contains(… here to evaluate whether there’s a space, and if there is, split on that space and grab the first item. The else condition is an empty string.

You might notice a sneaky concat(‘ ‘,.. in there. This is to put a preceding space in the output, so the string can go straight after the preceding word. If the string is empty, punctuation can be added straight after and we don’t end up with a space then punctuation if there’s no From name in this email.

So, now we’re past that techy bit, stick it in the string variable:

Now, generate a random number between 0 and 2:

Now, switch on the output of rand

In each case, make an http request to a free joke API. 0 = chuck norris, 1 = joke of the day and 2 = dad joke.

All of these APIs are free and unauthenticated but they each have slightly different request parameters and responses.

For each response, we set the sJoke variable with the joke.

Chuck Norris:

Joke of the day:

Dad joke:

Each of these Set variable actions look like dynamic content from the preceding action, but actually they’re not. I typed these into the expression editor based on the response schema in the documentation and Power Automate then prevents you from editing the expression after you’ve saved the flow.

Now finally the response:

Notice sFirstName is directly after Year without a space. The space is in the variable. If the variable is empty, which it will be if there was no from name, It’ll just say Happy New Year!

You can simplify this by only using one API instead of 3. You can simplify it by not bothering to parse the From name from the email, or you could expand it by having different responses depending on who the sender is by switching on the domain suffix. You could even add cognitive services to get the sentiment of the sender’s email and customise your response accordingly. The world’s your pavlova with Power Automate and Azure technologies.

That’s all. Have a great Christmas and New Year and let’s see what 2021 brings to the Power Platform and low-code/no-code community.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s