Tweeting new blog posts with Logic Apps
Tags: azureapril, azure, serverless, logicapps
Published: 2019-04-21 20:02:15

When I'm blogging I like to ensure that the content, as it is published, is sent out to my audience beyond just my normal RSS subscribers and I find that Twitter is an effective way to do this.

In the middle of 2017 I decided to rebuild my blog [as a static site](https://www.aaron-powell.com/posts/2017-07-27-site-rebuild/) because I like writing in Markdown and I wanted a way that I felt more in control of the content. And last year I decided that I would make it a proper static website using [Azure Storage Static Sites](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website?WT.mc_id=azureapril_devto-blog-cxa) and use [Azure Pipelines](https://azure.microsoft.com/en-us/services/devops/pipelines/?WT.mc_id=azureapril_devto-blog-cxa) to [automate the deployment](https://www.aaron-powell.com/posts/2018-07-05-automating-deployments-for-dddsydney/).

Because this process can take a minute or two to run and then a few more minutes for the CDN to be purged and the new content deployed, I often would forget to tweet my new post, or I'd be furiously refreshing my home page, cursing the CDN for not purging fast enough!

So I decided it was time to take another approach, it was time to automate this little task out of my life.

Now, I could write a bit of code and deploy it as an [Azure Function](https://docs.microsoft.com/en-us/azure/azure-functions/?WT.mc_id=azureapril_devto-blog-cxa), run it on a timer to check my RSS and then write some more code to do the tweet, but I really wanted something as simple as possible.

This lead me to look at [Logic Apps](https://docs.microsoft.com/en-us/azure/logic-apps/?WT.mc_id=azureapril_devto-blog-cxa), which are similar to functions but is a _codeless_ solution.

## Quick overview of Logic Apps

Logic Apps are an integration tool, allowing you to create a workflow that is triggered by an event in an external system. 

There are dozens of triggers available:

![Logic Apps Triggers](https://thepracticaldev.s3.amazonaws.com/i/o7nzws1kp1ncpaz4rbpl.png)

You can connect to a system like SalesForce, listen for a new record to be created and then start a new workflow. This workflow can perform a bunch of tasks, from manipulating the data in the trigger to calling another external system:

![Logic Apps Actions](https://thepracticaldev.s3.amazonaws.com/i/iiw32uww9z100y32p4un.png)

And all of this can be done without writing a single line of code!

## Listening to blog publishes

While it might be interesting to explore SalesForce to SAP integration without writing any code we're actually looking at our blog. For this I'm going to select the **When a feed item is published** RSS trigger:

![RSS trigger](https://thepracticaldev.s3.amazonaws.com/i/vq3dmlziv9gs4j086ubg.png)

And provide it with the URL for my RSS feed:

![RSS trigger info](https://thepracticaldev.s3.amazonaws.com/i/k3njny5353q9ktrb0ui7.png)

I'm leaving the default polling interval, while I'm not publishing every 3 minutes, it does mean that when I do publish it will be picked up pretty quickly.

## Tweeting our post

Logic Apps is listening now to when new articles are published, it's time to do something with them.

Since an item in the feed could have multiple URLs associated to it we want to iterate over them. Thankfully there's an Action in Logic Apps for that called `For each`. We have to give it something to loop over, so we'll provide the `Feed links` content from the trigger, selected from the **Dynamic content** panel that appears:

![For each action on links](https://thepracticaldev.s3.amazonaws.com/i/l4y7l25rhsfqyn12u8ic.png)

Finally, we can provide the Action to run for each link, we'll use the Post a tweet Action (you'll need to authorise Logic Apps to access the Twitter account you plan to use) and put in some information:

![Tweet action](https://thepracticaldev.s3.amazonaws.com/i/11soejmg2woynqr6as2b.png)

The `Feed title` is the title of the article and `Current item` is the link from the loop.

And you're done, publish an article and it'll automatically tweet a tweet per link that was associated with the new article (which, in reality, will likely be one tweet)!

## Improving our tweet

While we're creating a tweet to ensure that we get the broadest reach we probably want to put a hashtag or two in place, and a logical way to do this is to grab the tags that you've put on the post, after all, they are the keywords you'd expect people to look for. When the new item came from the Trigger the tags are an array of strings (eg: `[azureapril, azure, serverless, logicapps]`), but we want to convert that into a set of hashtags that we can append to the tweet.

To do this we'll use the `Join` Data Operation Action, which takes a collection, and performs a string join operation using the join tokens you provide. We'll add that before our loop so we can then use the dynamic content in our tweet:

![Hashtagging tweets](https://thepracticaldev.s3.amazonaws.com/i/01q815cssnph5uteehnm.png)

The joiner that I'm using is ` #`, so a leading ` ` followed by the `#`, so that we would end up with a string like `azureapril #azure #serverless #logicapps`. Then in the tweet we'll put a `#` before the dynamic content so the first tag also becomes a hashtag. 😉

## Scheduling a follow-up tweet

When a tweet goes out it's a point in time, a message that will be seen by the people who are looking at their feed _then_. But I want to try and hit both the audience who is online right now and a future audience.

To do this we can add another Action to our Logic App, Delay. Delay allows us to, well, delay the following steps of the Logic App by a pre-determined time period. Let's delay by 12 hours to hit the people who were most likely asleep when the initial one went out, and then run a new loop:

![Introducing delay](https://thepracticaldev.s3.amazonaws.com/i/dl3yhgz6gvhzp4vhmk6k.png)

Because it's a new tweet we can adjust the message a little (I add 'ICYMI', short for 'In case you missed it').

## Conclusion

And there we have it, with a few clicks in the Logic App designer we can create a Logic App that will automatically tweet, and schedule a future tweet, each time we publish an article to our blog.

Also, with a Logic App you can export it as JSON, I've done that if you want to get started quickly (just remember to update the RSS link and Twitter connection):

{% gist https://gist.github.com/aaronpowell/5f37e2ae873b779abd75db4718eb58a8 %}