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 ;
Azure Function
First we’ll start by creating the Azure Function. Use a generic webhook for this ;
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 ;
Next we’ll choose “RSS – When a feed item is published” ;
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!
Next up, we’ll integrate our Azure Function ;
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.
Once done, we can link it to our twitter ;
Now we can post a tweet by using the result of our function (the parsed feed title) and the link of the RSS item.
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 ;
And yes… this flow does what it is expected to do! 😉
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.