I’ve been reading a lot of great content on dev.to lately (seriously you should go check it out and follow some tags…great community there) and came across this great headline “How I Solved My NYC Parking Problem With Python, the Search Tweets API and Twilio” by Jessica Garson, a Developer Advocate at Twitter. Jessica was trying to solve a problem that NYC folks have when trying to determine when to move their street-side parked cars each night due to ‘alternate side regulations’ which determine when certain sides of roads can be used or the regulations won’t be enforced due to holidays, events, whatever.
She delved into using Python, the Twitter Search APIs and Twilio to tie these all together. Fun problem to solve and results in a text to Jessica whether she needs to worry about moving her car or not. Immediately after reading it I though of the game show Name That Tune.
The premise of the game show is that contestants battle it out to who can identify a song based on the fewest amount of notes in a song. So of course, it is 100% applicable to code, right? Anyhow I thought to myself Self, I can solve this problem in less effort and with NO code!
Immediately I went to work using Azure Logic Apps. Azure Logic Apps are a means to…well, let’s let the marketing people tell us what they are:
Azure Logic Apps simplifies how you build automated scalable workflows that integrate apps and data across cloud services and on-premises systems.
Lots of buzzwords there but basically it’s workflow/orchestration platform that allows you to use ‘connectors’ between various inputs and outputs. Logic Apps can be developed using code, but most are easily developed using no code and using the graphical connector tools. If you’ve ever used/heard of Microsoft Office Flow this is powered by Azure Logic Apps underneath! So immediately after reading the article I went to work. I knew that Azure already had pre-build connectors for Twitter and Twilio and that I should be able to do this easily. Connectors are pre-defined pieces of logic that help get access to events, data, and actions across other apps and platforms, in our case Twitter and Twilio!
Jessica’s problem was fairly simple: Watch when the @NYCASP account tweets and if it indicates rules are ‘suspended’ then alert her with a text message. Now this relies on @NYCASP account being consistent in their tweets and it turns out they are VERY consistent in them, so it’s easy to search for the simple terms as Jessica did. So let’s do the same.
To get started I needed an Azure account which is free to get started and there are a ton of services free that you can use forever. I also still needed a Twilio account like Jessica notes so you still need that to get your Twilio credentials…be sure you have that. With both of these in hand, let’s log in to the Azure Portal….we won’t even need any tools other than a browser to complete this app!
In the portal you’ll create a new resource in Azure…search for Logic App and it will show up:
You’ll need to provide a name, choose a resource group plan and a geographic location where you want this to live. If you’ve never created any Azure resources before, a resource group is a container for certain compute resources to leverage. For this, I recommend creating an App Service resource group and using a free plan that comes with your free trial account. Then you can use this resource group for your Logic App. Once you have that created navigate to that resource and you will see a page that welcomes you with a tutorial video and some options for pre-configured templates. Thankfully one of the starting options is “When a new tweet is posted” so let’s use that one to help us get started as it will default add the Twitter connector!
This dumps us into the Logic App designer, a graphical interface that helps us do the connections. You’ll immediately see the first ‘trigger’ which is our Twitter one and it wants you to sign in to be able to use the functionality (think of this as authorizing use of the API). Once you sign in click continue and you’ll get the options. Now basically we want to look for when a new tweet is posted by @NYCASP on a time interval. Their account is pretty consistent so I chose every four hours and used the ‘from:@NYCASP’ query language as the search text.
That’s it for Twitter. No code successfully so far! So now every 4 hours this will check for a new tweet from that account. Now let’s do something with it! In Jessica’s scenario we need to look at the tweet and act upon it only if a specific condition was met. So let’s use that little “+” symbol on the designer and add a new action. Search for ‘Condition’ and you will see ‘Control’ come up as an option…select that, then you will see the Condition connector…choose that. The condition connector gives us a simple decision tree: what is the condition, what do you want to do if true, what do you want to do if false:
In the condition area where it says ‘Choose a value’ when you click in to there, data from the previous trigger will be made available to you and you can see all the details it exposes! We will scroll and look for Tweet text and select that. Change the oeprator to ‘contains’ and type in ‘suspended’ as the value. It should look like this:
Now we know that the tweet is telling us something about suspending…but Jessica wants to know if she has to move her car for tomorrow. Let’s add another condition to now check to see if it contains ‘tomorrow’ in the text. We follow the same steps in creating a new condition trigger and connecting it.
At this point we have a nested condition. Could we have put them in the same one? Probably, but I’m just following similarly Jessica’s flow.
It should look like this:
Now we know if both of those are true we need to send a text message. Click the ‘Add an action’ button in the True condition and search for Twilio and select the ‘Send a Text Message’ action. This will add the provided Twilio connector which provides this functionality (and more). Similarly like twitter you will see it and after selecting have to authenticate to your account to get the credentials.
After doing that you now simply enter the details of your text message. For Twilio the From number must be your account SMS number unless you have premium services that enables you to do something more. If you try to get fancy without premium services from them, this will fail. Don’t get fancy…we’re just moving cars across the street remember? Enter the text you want to send and to the phone number you want to send it to…done!
Now on the false conditions we do still have to tell the Logic App what to do. In these cases, unless you want to do more, again add an action and choose System, then look for ‘Terminate’ – it’s a simple action that basically just stops the flow and can log a message. I configured mine like this on BOTH false conditions:
That’s it, I’m done. No code. This took me longer to write this post then it did to actually do the Logic App the first time. Now I waited. And waited. And waited. I completed the logic app right after reading Jessica’s article and wanted to ‘naturally’ test this with the real deal tweets. But no parking regulations were suspended. My logs looked like this:
UNTIL A FEW DAYS AGO! My phone buzzed, I looked down and BOOM:
I wonder if Jessica got a text message too! I was so happy and I don’t even live in NYC or have to worry about moving my car to a different side of the street! The logs of Logic Apps are pretty cool as well and also graphically follow your flow and show you input/output state along the way:
So with no code and only a few steps to authenticate to Twitter and Twilio, I was able to use only a browser and complete the same task. Did I win Name That Tune? Who cares…doing software isn’t a competition, it’s fun and we get to use the tools and technology we feel most productive with. For me, this was just a gut reaction to see if could be done as easily as I thought and indeed it could. So yeah I won :-D.
Check out more about the pieces I used to put this together and get your custom logic apps working:
Azure Account – get one free
Azure Logic Apps (docs/tutorials)
Azure App Service (for the resource group)
Over ~200 built-in connectors provided for Logic Apps!
Hope this helps!
(this article cross-posted from timheuer.com)