How to roll your own rss2twitter bot in 15 minutes in Azure?

Introduction

For today I’ll show you how to roll your own rss2twitter bot in about 15 minutes. What will this bot do?

  • Check an RSS feed for new entries
  • Parse the new entry and replace key words with hashtags
  • Post it to twitter

For this we’ll be using ;

  • Azure Function : as the customer code to find/replace certain words
  • Logic App : as the tool to create our process via a visual tool

And in the end, we’ll see something like this ;

2017-02-08-19_15_28-azure-inspired-azureinspired-_-twitter

 

Azure Function

First we’ll start by creating the Azure Function. Use a generic webhook for this ;

2017-02-08-19_11_03-function-app-microsoft-azure

And create your function ;

module.exports = function (context, data) {
    context.log('Webhook was triggered!');

    context.log('Data = ' + data["feed"]);

    var feed = data["feed"];;

    var mapObj = {
        azure:'#azure',
        'stream analytics':'#streamanalytics',
        streamanalytics:'#streamanalytics',
        'big data':'#bigdata',
        bigdata:'#bigdata',
        'application gateway':'#applicationgateway',
        applicationgateway:'#applicationgateway',
        'internet of things':'#iot',
        'sql server':'#mssql',
        'visual studio team services': '#vsts',
        cloud: '#cloud',
        paas: '#paas',
        iaas: '#iaas',
        saas: '#saas',
        blockchain: '#blockchain',
        iot:'#iot',
        storage:'#storage',
        microsoft:'#microsoft'
    };

    var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
    feed = feed.replace(re, function(matched){
        return mapObj[matched.toLowerCase()];
    });

    var body = feed;

    context.log('Replaced = ' + body);

    context.res = body;

    context.done();
}

 

Azure Logic App

We’ll create a LogicApp by starting from a blank template ;

2017-02-08-19_20_25-logic-apps-designer-microsoft-azure

Next we’ll choose “RSS – When a feed item is published” ;

2017-02-08-19_20_38-logic-apps-designer-microsoft-azure

Here we’ll enter the url and how often we want to check this. Bare in mind that you’ll pay a small fee per logic app run. So doing this every minute we’ll cost more than doing it once a day. The pricing is low. Though nevertheless, you should always take this into consideration!2017-02-08-19_21_10-logic-apps-designer-microsoft-azure

Next up, we’ll integrate our Azure Function ; 2017-02-08-19_21_18-logic-apps-designer-microsoft-azure 2017-02-08-19_21_36-logic-apps-designer-microsoft-azure

 

The function is a web hook and will expect a json payload as input. So be sure to format your input like this! For this example, I’m sending the content of the “Feed title” from the RSS entry to my Azure Function.

2017-02-08-19_22_12-logic-apps-designer-microsoft-azure

Once done, we can link it to our twitter ;
2017-02-08-19_22_21-logic-apps-designer-microsoft-azure 2017-02-08-19_22_35-logic-apps-designer-microsoft-azure

 

Now we can post a tweet by using the result of our function (the parsed feed title) and the link of the RSS item.

2017-02-08-19_22_43-logic-apps-designer-microsoft-azure

Result

Now we’ll take this out for a test spin… An example flow is to check the RSS feed of the Azure blog and post it to a twitter account @AzureInspired ;

2017-02-08-19_11_58-logic-apps-designer-microsoft-azure


And yes… this flow does what it is expected to do! 😉


2017-02-08-19_12_19-azure-inspired-azureinspired-_-twitter

 

Closing Thoughts

  • Yes, this bot is generating even more spam on twitter! 😀
  • The creation of this bot was done in less than 15 minutes. Try it yourself…
  • This example shows how easy it is to set things up with Logic Apps.

 

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 )

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.