Azure Alarms
Tags: beginners, webdev, azure, javascript
Published: 2020-07-22 02:44:52


I'm a very forgetful person, especially when I'm out having fun. I love hiking and biking, but my allergies make life miserable so I need to take my meds every 24 hours when I head out. To fix this problem, I can just set a notification, but when I'm doing something else, it's easy to miss the slight buzz and tone from my phone.

With Azure and Twilio, this problem can be simply remedied. We'll use Azure to set up a timer to trigger other code to run and integrate Twilio, so we can send a text message or call to ourselves to make sure we notice the reminder.

It will take a little bit of Javascript to make everything work, but it's pretty straightforward and we'll go over everything together. Firstly, we need to set up 2 accounts, [Microsoft Azure](https://azure.microsoft.com/en-us/free/search/?&ef_id=Cj0KCQjwn7j2BRDrARIsAHJkxmwz0tcFx8fTgoL7jogctsTPYrzEntPYcIpEu0QV_YRDbPFyv7oNrOoaAvspEALw_wcB:G:s&OCID=AID2000128_SEM_Cj0KCQjwn7j2BRDrARIsAHJkxmwz0tcFx8fTgoL7jogctsTPYrzEntPYcIpEu0QV_YRDbPFyv7oNrOoaAvspEALw_wcB:G:s&gclid=Cj0KCQjwn7j2BRDrARIsAHJkxmwz0tcFx8fTgoL7jogctsTPYrzEntPYcIpEu0QV_YRDbPFyv7oNrOoaAvspEALw_wcB) and [Twilio](https://www.twilio.com/try-twilio). Azure has a 12 month free trial, and Twilio has a free trial as well with some credits to experiment with the features.

Step 1: Create Timer Function
Let's pop open Azure and click to create a new resource, get started, then select "Function App". After that, fill in all the info about your function. Make sure you select "JavaScript" for the runtime stack option.

Now that we've done all that, we can go ahead and click "Create" and deploy the function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9ias1hk2pu1rsubvivel.PNG)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kbag1l9kk8c14pcuzmi2.PNG)

Let's select the "In-Portal" development environment for simplicity, but you can use VS Code instead if you want. Under the "More Templates" option, we'll be choosing a Timer Trigger because we want the timer to trigger our function in Twilio.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ws27wnw2cr0a1r7ipps8.PNG)

When filling out the time, remember that it's in UTC, and the numbers are '{sec}{min}{hr}{day}{month}{day of week}'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3838wxge16ommedx6u1x.PNG)

That's it for now. The Azure timer function is all set up. Let's integrate Twilio, so we can get a notification.

Step 2: Setting up Twilio Credentials
Firstly, let's make sure we have Twilio installed. We'll go to the left panel of our app menu in Azure and select console. Then type:

​npm install Twilio

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/srlbntx0bog2lr5nmzxg.PNG)

Now let's go over to our Twilio account and purchase a phone number. The free Twilio trial comes with some balance so we can purchase a phone number and send calls and texts using that balance.

Back over in Azure, let's go to the same left panel as we did earlier and go into the Configuration tab. There let's create 4 new application settings for your TWILIO_SID, TWILIO_TOKEN, SENDER_NUMBER, RECIPIENT_NUMBER. Those are all values that we need in our code later, but for security, we save those values under their corresponding name in the application settings instead of directly writing it into our code so other people can't see those credentials.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cksijqfgay9xurc8do8m.PNG)

Step 3: Sending a SMS
And now, the code. Let's go back to that left menu, select "Functions" and click on the timer trigger we made earlier. There will be a "Code + Test", and we'll be editing index.js.

​Here's a look at the code:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o368d56z5vxi7kiif2om.PNG)

Let's do a quick rundown of the code. If the timer is past due, write that in the log, then create and send a message from SENDER_NUMBER to RECIPIENT_NUMBER. If everything works perfectly, log a success, else log the error.

Step 4: Making a Call
The code to make a call isn't that different from sending a text, but we will have to make some changes.

One of the things that we'll have to do is make a TwiML Bin in Twilio.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g8hmxkbam8xbq1jy1mtd.PNG)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4h63tc5z61v2962gr34i.PNG)

In that TwiML Bin, let's write some code so the program knows what to say during the call, and copy the URL for the bin and save it as an Application Setting with all our other Twilio credentials.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z1hfi3dxh50jj4j6c7yt.png)

​Now we can go back and change the code like so:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wsea87gmuh7qsts3hzpc.PNG)

Step 5: Testing and We're Done!
With this, we're all done with the code and should test it. Just click the "test" button up top, and when the time you set on the timer rolls around, it will go off and either send a text or call depending on which one you chose to implement.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gm7c7i3qeo46v7pq0x4n.PNG)

If everything works, we're done! We've made an automated text or call that is sent at the time we set to remind us to do something we need to do by using Azure, Twilio, and a bit of JavaScript. You can customize the code to do whatever you want. You can use it as a wakeup call if you want something more than a simple alarm, or you can spam a friend with texts (probably shouldn't do that one...). Have fun with it! The sky's the limit. Now if you'll excuse me, I'm going to hit the trails.