Automatically tweet new blog posts using Microsoft Flow

Published on Monday, July 4, 2016

IFTTT

If you're a developer or a technically savvy individual, chances are you've heard of or actually utilize If This Then That (IFTTT) to help automate portions of your life.

IFTTT is built upon the notion of recipes which take the form of: if trigger then action.

Examples of popular recipes include:

  • Automatically keep Facebook and Twitter profile pictures in sync
  • Automatically post your Instagram pictures to Tumblr
  • Automatically post Tweets on Facebook when you include a specific hashtag
  • Etc, etc.

As I was installing and configuring my new Ghost blog I noticed it doesn't currently have functionality to automatically tweet new posts on Twitter which is a feature many other blogging platforms offer. Naturally I turned to the wisdom of Twitter and received a fantastic recommendation from David Balderston:

And that's exactly what I did using a customized derivative of this recipe. After I little trial and error, I felt confident that the solution was working exactly as advertised.

Enter Microsoft Flow

However, being a Microsoft blue badge and knowing we recently released an IFTTT-like service called Microsoft Flow, I couldn't leave well enough alone and instead felt compelled to reproduce the IFTTT solution I had just implemented in IFTTT using Microsoft Flow.

Getting Started

To get started with Microsoft Flow, you need to sign-up using an "organizational" account (i.e. an account backed by Azure Active Directory and not merely a Microsoft Account). Since I'm setting up this solution for my personal blog, I didn't want to use my work O365 account which meant there'd be a little bit of work for me to get started.

This is where Azure comes to the rescue. It just so happens to be that I'm actually hosting my Ghost blog in Azure and therefore already have access to an Azure Active Directory in the form of the Default directory created for my Azure subscription. All I needed to do was add a new user in the form of user@yourtenant.onmicrosoft.com to my Azure Active Directory and then return to Flow and complete the sign-up steps, logging in as the newly created user.

From there the experience was much like IFTTT and I was able to quickly create a new flow that would watch my RSS feed and post a tweet when a new feed item was published.

To add the RSS trigger just search for "RSS" or "Feed" in the trigger search box and then plug in URL to your RSS feed:

RSS Trigger

Then click Add an action, search for Twitter, choose Post a new tweet, and then configure the contents of your tweet. NOTE: If this is your first time using Twitter within Flow, you'll need to establish the connection by authorizing Microsoft Flow to access your Twitter account.

RSS-Twitter Action Take 1

Once the trigger and action were linked, I saved the flow, created a new blog post and sat back and waited for a new tweet from me to appear in my timeline. Sure enough, about a minute or two later a shiny new tweet with the title and URL of my blog post appeared like magic. Well, I thought to myself, why stop here?

Expanding The Solution?

Since this solution worked so well with my personal blog, I wanted to leverage it to post tweets triggered by new blog entries on the Microsoft Azure Blog. Logging back into Microsoft Flow, I quickly duplicated the flow I setup for my personal blog but this time pointed the RSS trigger to the Azure Blog RSS feed. Feeling accomplished, I shut my Surface down for the night and eagerly awaited the Azure team to publish new posts the following day.

Trouble In Paradise

Luckily, the Azure team had quite a bit to say the next day which served extremely valuable in highlighting a fatal flaw in my Flow - something I had taken for granted because IFTTT handled it automatically behind the scenes. Turns out my Flow tweeted some of the new Azure blog posts and not others. There didn't seem to be any rhyme or reason for the missing tweets, so I logged into Flow and looked at the failures in the activity log.

Smack. There it was in plain sight:

Twitter Error

status: 403 message: "Tweet must be 140 characters or less."

Doh! Turns out the combined length of the blog title and URL exceeded Twitter's 140 character limit. The reason this wasn't an issue with IFTTT is because the recipe I had created automatically uses a URL shortener for links and all good bloggers know to keep the titles of posts well under the 140 character limit so the content is easily shareable on Twitter.

Post a new tweet - Take 2

Now that the error was known the solution was fairly straight forward. After evaluating a few URL shortening services I decided to go with Bitly. Although I was familiar with Bitly from having used it in the past, this decision really came down to two technical requirements:

  • Since I'm creating a lightweight "drag and drop" solution I didn't want a complicated authentication/authorization scheme to deal with
  • Since I didn't want to write code that meant that the shortening service had to return the shortened URL in the body as plain text because otherwise I'd likely have to parse JSON/XML.

Bitly came through on both fronts. Per their Authentication API documentation:

If you only need a token for your own account and will not be authenticating any additional end-users, you can generate a developer access token from https://bitly.com/a/oauth_apps or by using the Basic Authentication Flow

Also, as illustrated in their Links API documentation, by specifying format=txt, the response from Bitly will contain only the shortened URL in the body.

So with that, I was able to add a simple Http action to my flow which would capture the URL of the new blog post from the RSS feed, add it to an HTTP call to Bitly, and finally use the body of the response from Bitly as the link in the Post a new tweet action. The final Flow look like this:

Final Flow

I hope this post helps you out or inspires you to check out Microsoft Flow - it's a bit rough around the edges, but there is huge potential. If you have any questions or would like to share any Flows you've created feel free to drop a line in the comments below.