Guest post by David Adeboye Microsoft Student Partner at University of Cambridge
Hello! My name is David Adeboye and I am currently studying Computer Science at University of Cambridge. My main interest is in site reliability and concurrent and distributed systems, and in particular, I like designing system architectures in interesting ways. LinkedIn profile: https://www.linkedin.com/in/david-adeboye/
One of the ideas in distributed systems is the producer-consumer model, in this blog we'll look into using Azure functions to process items on an Azure Service Bus queue. The advantages of this is that it means you are not having to bear the costs of a running a virtual machine all the time, especially if your workload can be quite infrequent. Another use is when your workload is highly parallelisable, meaning you can run several instances of your code on different data.
We will partially follow the step by step tutorial provided and show how to an example of functions by creating a twitter bot using Markov models. All the source for this blog can be found on my github. (Link found at the bottom)
Theory about Hidden Markov Models would be beneficial but is not required, for brevity I use a library to generate the words, however better results may be achieved by using a custom solution.
You will require an Azure account, sign up here for free: http://portal.azure.com/
You will also require Twitter API credentials, obtainable from here: https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens Please generate an access token + secret for yourself.
You should build a bot that will wait for people to tweet on a particular hashtag with a word, and then tweet back at them with song lyrics generated using a hidden markov model and a starting word.
Since we are using the Service Bus queue, we will require 2 things, producer(s) and consumer(s).
This was implemented using Java, for now I ran it on my local computer, but it could easily run on an Azure Virtual Machine.
The first step is to set it up as a maven project, this is so that we can include the required dependencies easily, essentially we need the Azure service bus library and I'm using twitter4j to communicate with the Twitter API.
Next we'll need to create a service bus, head to portal.azure.com, login then click New and finally type Service Bus.
Click Create and fill in the information, taking note of your namespace. for the rest of this tutorial we shall take the namespace to be twitter-msp-bus
Once it has been deployed, head to the settings page and click on Shared access policies > RootManageSharedAccessKey you'll need to take note of the primary key shown.
Finally, we need to create a queue, click Queue then + Queue at the top. The settings don't need to be changed, but just create a queue and take note of the name.
Next create a new class, I've named mine Producer.java Create a main function.
The first thing we'll need is to communicate with the Azure Service Bus. Copy and paste the code with your namespace and primary key in the relevant places:
Next we'll need to get the Twitter functionality, make sure to enter your own twitter credentials.
Next we shall initialise the service bus connection and create a query to search on Twitter, in this example we are searching for the hashtag #TwitterMSPBot
The next part will need to be srrounded in a try and catch because it can throw errors.
The part below will indefinetly look on twitter for the latest tweets with the hashtag and add them to the service bus message queue in the JSON format. It will also remove any unnecessary details such as other user's usernames and any hashtags, including the one we are searching for.
And then we are done!
Returning to the Azure portal as we will be creating the serverless functions, click New and type Function App
Create an app with Windows as OS, in this case we shall name twitter-bot-msp and I will use it this for the rest of the walkthrough.
Once it has been deployed, we will need to create the function, you want a 'Queue Trigger' function.
Creating the function
1. Navigate to https://twitter-bot-msp.scm.azurewebsites.net/ and click Debug Console > CMD
2. Type in cd site\wwwroot to get to the site's home directory then type npm init to start an npm package. Enter data for the relevant fields. Here is mine below:
3. Type npm install markovchain to install the hmm module, then type npm install twitter to install the twitter module
4. Return back to the Azure portal and click on the file named 'function.json', you want to make sure that the type, the queueName, and the connection are changed to the correct parameters as shown below
5. Return back to index.js and copy in the following code, you'll need to enter your Twitter credientials again below in the correct position. In the corpus you'll need to paste in text for your markov model to train from. In my case I entered lyrics from one of most-listened artists this year, according to Spotify. (Omittted for copyright reasons). I've explained what each part does below:
This piece of code imports both the Twitter and Markov chain library, for brevity I am using a library instead of implementing my own. The corpus variable should be where you paste in some text for your Markov model, I used about 1000 lines of lyrics. Basically the more the better.
To make things more interesting we want to vary the length of the tweets, tweetLength should be a random number between 5 and 10, denoting the number of words we are going to generate. The starting_word should be taken from the tweet as the start of the markov model.
This just creates and authenicates the Twitter object we are going to use for tweeting
Firstly we generate our markov model and store it in an object named lyricGen, then we use this to generate a new lyric using the starting word we got from the original tweet. Then finally we draft a tweet to send, and we log the sentence we generate. In a future iteration we should train once and just store the probabilities in a file to be loaded up everytime this function runs.
This piece of code will tweet the sentence from your account. In testing you should uncomment those lines in case there are any errors, especially the raw response object. Context.done tells the function that we have completeted execution and we can be terminated, hopefully that tweet posted!
And then we are done, we have succesfully created a twitter bot using Azure Service bus and serverless.
There are several improvements that can be made to this system, however the general idea was demonstrated. This could be used in applications such as batch processing where you may not particularly want a dedicated virtual machine running all the time, but you want it there when you need them.
Another use case could be the ability to test your application as part of your deployment, instead of having a dedicated machine for testing, or running it locally, you could have an Azure function running to test your application when you need it!
Extra points for guessing the artist!
Resources for reference and further reading
1. Source code: https://github.com/adeboyed/azure_serverless_blog
2. Azure Service bus: https://azure.microsoft.com/en-gb/services/service-bus/
3. Introducing Azure functions: https://azure.microsoft.com/en-gb/blog/introducing-azure-functions/
4. npm reference for the markov models: https://www.npmjs.com/package/markov-chains-text
5. npm reference for the twitter api: https://www.npmjs.com/package/twitter
6. java reference for the twitter api: http://twitter4j.org/en/