When I originally blogged about sending tweets based on my RSS feed I was vaguely annoyed by some of the limitations. I assumed I was forced to either send a tweet for new Posts, there is actually a Power Automate template for this scenario, or I could send a tweet based on the oldest entry in my RSS feed. Here is what I came up with for my Power Automate flow for tweeting the oldest entry:
string(last(Body('List_all_RSS_feed_items'))['title']) string(last(Body('List_all_RSS_feed_items'))['primaryLink'])
As I noted previously, this couple of lines of code displays the title and the primary link of the last array item in the list of all RSS feed items, and frankly it works well. However, I wanted to improve on this idea just a bit.
I know that my main site RSS feed contains references to 20 blog posts (this is configurable in DasBlog Core), and my goal was to send a tweet about one of these old posts, but to have the selection of the post be random. So my first task was to initialize a variable with a random number that represented the blog post I want to reference (full confession I am still not sure if this is zero based array). Here is the code for that:
rand(2, 19)
Ok, that was simple enough but how do I use that random value in the Post a tweet portion of the Power Automate flow? In the image below you should see that I name this initialized random variable INDEX for later reference, and it looks like this:
So the magic sauce is really in the Post a tweet section which references the random variable (INDEX) as follows:
A recent post: last(take(Body('List_all_RSS_feed_items'), variables('INDEX')))['title'] last(take(Body('List_all_RSS_feed_items'), variables('INDEX')))['primaryLink']
So how do you read this? Let’s assume the random number is 5, our code will then take the first 5 posts in the RSS list and then select the last one in the list. Your tweet then contains the title and primary link of the randomly selected 5th item in your RSS feed.
This seems rather convoluted but I got there in the end, suggestions are certainly welcome!
n.b. It looks like Microsoft Flow was renamed to Microsoft Power Automate (apparently naming things is really hard).
Comments are closed.