Using Twilio's node package with an Azure Mobile Service

A while back, I built a little service that pings my shared Azure websites to prevent delays in loading. There are fantastic services to do this like Uptime Robot, but I wanted to do it myself to learn a bit about Azure Mobile Services.

That was all well and good; my site is responsive to new users! However, I thought it would be fun to add onto it to get text messages from my service if anything happened. So I found a cheap way (less than a penny per message) to message myself if anything demands attention.

This service is built with a node backend. Azure was not offering a .Net Backend at the time (that has since changed). And of course, there is no time like the present to tinker with multiple new technologies in a project. I am only going to address the Twilio implementation of that service in this post.

Let's get started

  • Create a new Azure Mobile Service.

new service in azure

  • Navigate to the scheduler and create a new scheduled job. Sadly, free services only allow one scheduled job, but this is simply a demonstration. There are several configuration options for the interval. Every 15 minutes is good for my use to keep my website alive. On demand will suffice for this lesson.

new job creation

  • Clone the source code to a location you have access to. You can find the url for the service's git repository on the 'Configure' tab.
    • Your git user name is viewable when selecting 'Reset your source control credentials' on the 'dashboard.' I don't know if this is official, but mine always seems to be everything before the '@' on my live.com account's email address, password is my live password.

git url cloning the repository

  • Navigate to the repository and open the package.json in the service folder. Add a dependency to Twilio's node package.

    "dependencies": {
      "twilio": "1.6.0"
      },
    
  • Register for a free number and account with Twilio if you havent already done so.

  • Navigate to the scheduled task we created and paste the following. This will instruct Twilio to send a message on your behalf. Replace 'ACCOUNTSID' and 'AUTHTOKEN' with your twilio information. This is available on their website under 'Account Options' do not share this information, it is yours. Recipient phone is the number you wish to send to, plus area code. From is your twilio phone number.

    function mytwiliotask(){
        var twilio = require('twilio');
        var client = new twilio.RestClient('ACCOUNT_SID', 'AUTH_TOKEN');
        client.sendSms({
            to: 'recipient phone',
            from:'your twilio phone',
               body: 'Hello There, this is from azure!'
            }, function(error, message) {
        if (!error) {
            console.log('Success! The SID for this SMS message is: ' + message.sid);
            console.log('Message sent on: ' + message.dateCreated);
        }
        else {
            console.log('Oops! There was an error.');
            }
        });
    }
    
  • Commit your changes and push the repository to azure.

  • Navigate to your task in Azure, enable it if it is not already and run the task.

azure saying hello

That's it! In this post we reviewed how to use Twilio's node package to send a text message on Azure Mobile Services. You are not limited to SMS messages alone, the possiblities are endless, check here for more documentation.

comments powered by Disqus