In Part 1, we outlined our goals for the @BotJurassic Twitter Bot, along with setting up the underlying data structure required for the Logic App. In this part, we'll actually go about creating the Logic App, using those data sources to craft randomly generated Tweets.
The Logic App
There it is! The whole Logic App in its glory
- Complicated? Yes.
- Convoluted? Yes.
- Over Engineered? Yes.
- A whole lot of fun? Heck yes!
Let's break it down into its parts and walk through how it all comes together.
- Firstly, the trigger. Nice and simple, the Logic App runs daily at 1800 UTC
2. Here we begin to initialise some variables that we will use later on in the Logic App, 3 arrays, one for each of the Azure Tables
...and 3 Integer variables
3. At this point we'll start our random selection sequence by generating a random number between 1 and 15 using the following expression
4. Depending on the result of the previous random number, we choose which path we follow to complete the Tweet. If the number = 15, then we head down the True path (for the disaster Tweet), otherwise we head down the standard False path
5. The False path of the Logic App is used for the standard daily Tweet. This is the simplest path which consists of three steps.
6. Firstly, we read the file contents of the day.txt file stored in the Azure Blob Storage we set up in Part 1 to get the Current Day Counter (this is the number of days without incident)
7. Using the value from the previous step, we Tweet out the following message
8. After the Tweet is sent, we increment the counter by one so the next daily Tweet will reflect another day without incident!
9. On to the mammoth True path!
10. The first thing we do here is reset the incident counter to 0, bad things have happened so no more consecutive Dino free days
11. After resetting the counter to 0, we run through several steps for each Azure Table we have set up to generate the Tweet content. Each Table uses identical steps so I've outlined the names flow below.
We pull all the various options from the table into the matching array which we initialised in step 2, and from here we then again use the random number function to select an entry. Because we don't automatically know how many entries are in each table at this point, we'll use the length expression to specify the top end of the random numbers based on the number of items in the array
12. We repeat this for the Prefix and Suffix Tables to end up with 3 numeric variables which we can use in the next step when we actually send the Tweet
13. The Final Step! All that hard work we've put in so far is about to pay off.
At this point, we take all those variables we've been putting together to create the Tweet text, using the random numbers to select the entry from the array as follows
We do this for each of the variables, names, prefix, and suffix, to complete the sentence required, just like the example below!
Would I do it this way again? Probably not. There are several excellent Twitter API modules out there for many programming languages such as twit for node.js which I have used before which would probably have been a lot simpler to work with the arrays on, however, for a "design-first" bot, Logic Apps were way more powerful than I had initially thought.
As I mentioned in Part 1, this design crept well outside the initial scope of my design but thanks to the flexibility of Logic Apps, I was able to quickly iterate through my 30+ versions of the App to deliver something that I'm pretty impressed with.
Again, huge thanks to @ChloeCondon, @MetzinAround, and @madebygps for the Got Bots? series and the inspiration to build something out of my comfort zone. Definitely go give them a Follow and check them out on Twitter for more fantastic puns and Twitter bots!