Take a Break

Overview

As someone who works full-time remote, I find myself easily glued to my monitors for hours at a time. I tend to start my day around 7AM and before I know it, it’s 2PM and I haven’t eaten a morsel of food.

Telling ourselves to get up and take a break can sometimes be easier said than done. Just actively remembering to do it can be difficult if you’re hammering away at a problem while constantly telling yourself “Just 5 more mins” for the next 45 mins.

So, I figured that I could use technology to help remind me to get up and take a break.

For this tutorial, I’m going to show you how to use Twilio SMS and Azure Functions to send a text message to your phone instructing you to “Take a break!”.

Language: JavaScript

Product(s): Twilio SMS and Azure Functions

Prerequisites

Sign Up for Twilio and Get a Phone Number

In order to work with Twilio’s products, you’ll need to sign up and purchase a voice enabled phone number. If you’re new to Twilio, you can start with a free trial. For further assistance on signing up and purchasing a phone number, visit Twilio.

Sign Up for an Azure Subscription

You can create a FREE Azure Subscription to setup your Azure Function. The free trial will provide you with 12 months of free services.

Access the GitHub Repository

Visit my GitHub repository to access the code that will be used throughout the tutorial.

Tutorial

1. Create a Function App

We’ll start off by creating an app for our timer. In the Azure Portal, click + Create a Resource.

create_a_resource.PNG

In the Azure Marketplace list, click Compute. In the Featured list, click Function App (note: if Function App does not appear, then click See all).

featured_list.PNG

You’ll now need to fill in the function app settings. Use the table below to guide you with how to setup your function:

Setting Instruction Suggested Value
App Name Choose a globally unique app name. This will serve as the base URL of your app. Valid characters are a-z, 0-9 and -. take-a-break
Subscription Select the Azure subscription where you would like the function app hosted. Free Trial
Resource Group Create a new resource group and use the pre-populated entry. If you’re already an Azure user, feel free to select a different entry. take-a-break
OS Choose your operating system. Windows
Hosting Plan Since we want to create a serverless function, select Consumption Plan. Resources are added dynamically as required by your functions – therefore, you only pay for the time your functions run. West US 2
Runtime Stack Select the language that you'll use to implement the function. We'll be using JavaScript in this tutorial. JavaScript
Storage Create a new storage account. Azure will give it a name based on the app name. If you decide to change this value, make sure that is also unique. Provided by Azure
Application Insights Click the Application Insights arrow to ensure that the Application Insights Site Extensions are enabled. Enable




3. Add Function

The function app that we just created needs a function that is configured with a trigger. The trigger will start the function which sends the Twilio SMS message. We’ll be using a Timer trigger for this tutorial.

In the left menu, click Resource groups and select the resource group you created in the last step.

resource_groups.PNG

Click on the App Service (indicated by a lightning bolt).

app_service.PNG

Once you page loads, click the + button next to Functions to create a new function.

add_function.PNG


On the next screen, you’ll need to choose a development environment. Since we’ll be creating the function in the Azure Portal, select In-Portal and Continue.

in-portal.PNG

Since we want to create a Timer trigger, you’ll need to select Timer and click Create.

timer_trigger.PNG

You should now see TimerTrigger1 listed under Functions in the left menu.

timer_in_list.PNG

4. Integrate with Twilio SMS

We’ll first need to integrate Twilio SMS with our function. Under TimerTrigger1, click Integrate.

integrate.PNG


Under Outputs, click + New Output and select Twilio SMS.

twilio_sms.PNG

You’ll need to install the Mirosoft.Azure.WebJobs.Extensions.Twilio extension. You can do so by clicking Install. This process can take up to 2 minutes so just give it a moment to complete installation.

install_twilio_extension.PNG

While the installation happens in the background, you’ll need to update the following values:

Field Value
Account SID Setting TWILIO_SID
Auth Token Setting TWILIO_TOKEN
From Number SENDER_NUMBER

These values represent environment variables that we will set after we’ve installed the extension.

twilio_sms_outputs.PNG

Once you are done, click Save.

I prefer to stay on this page until the installation is complete – just to ensure no funny business! Once the installation is complete, you’re ready to add environment variables.

extension_installed.PNG

5. Set Environment Variables

When you’re just getting started and testing out all the awesome things that Twilio can do, it’s ok to hardcode your Twilio credentials. However, once you’re in production, you should use environment variables to hide your credentials.  

You should first gather the values needed before creating the environment variables. In the Twilio Console, copy and save the following values:

Twilio Variables Where to Find It
Account SID Project Dashboard under the Project Name.
Auth Token Setting In the Project Dashboard, click View to see the auth token.
Auth Token This can be found by going to your Twilio phone numbers. You’ll need to use E.164 formatting: [+][country code][phone number including area code].

Now that you have your Twilio credentials and number, you’ll want to create environment variables for these values. You can create environment variables in the Azure Portal by going to the Overview tab for your function and click Configuration.

configuration.PNG

On the Application Settings tab, we’ll need to add the following environment variables:

Name Value
TWILIO_SID ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TWILIO_TOKEN Your auth token.
SENDER_NUMBER This will be your Twilio number (ex: +12131112222).
RECIPIENT_NUMBER This will be the phone number that receives the text message (ex: +12135555555).

I’ll walk you through the first one! To add an environment variable, click + New Application Setting.

new_application_setting.PNG

In the Name field, enter the name of the variable (ex: TWILIO_SID). In the Value field, enter the value (ex: ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX).

sid_env_var.PNG

Now click OK – that’s it!

You will need to add the remaining variables before you proceed.

Note: Phone numbers can be tricky! Be sure to use the E.164 format referenced above for both the RECIPIENT_NUMBER and SENDER_NUMBER.

After all environment variables have been added click Save to save the updates that were made to the Application Settings.

6. Modify Timer Settings

By default, Azure sets your function to trigger the text message every 5 minutes. You can change how frequent the timer triggers by going to the Integrate and updating the values in Timer trigger.

timer_time.PNG

The Schedule field contains a sequence that using CRON expressions. For the purpose of testing the function, change the number 5 to the number 2 and click Save. You can later change the frequency after you confirm that the function works properly.

cron_expression.PNG

7. Modify function.json File

Now that we’ve added the environment variables, we’ll need to update the function.json file within our TimerTrigger1 function. Head back over to the function app and click TimerTrigger1. On the far-right side of the screen, click View Files.

view_files.PNG

You will see two files:

  • function.json

  • index.js

Click function.json to open the file. Since the file is currently missing the “to”: RECIPIENT_NUMBER”, we’ll need to add this to our file.

Head over to the GitHub repo and grab the code for this file. You’ll want to replace the existing code in the function.json file with the new code that you just copied from GitHub.

function_json.PNG

8. Add Text Message to the index.js File

When Azure creates a function, it adds default code to help setup your function. We will the code for the Twilio SMS message to this code.

In the View Files menu, click the index.js file. Head over to the GitHub repo and grab the code for this file. You’ll want to replace the existing code in the index.js file with the new code that you just copied from GitHub.

index_js.PNG

You can replace the text in body with the message of your choice. Once you’ve updated the body, click Save.

9. Install the Twilio Node.js Helper Library

Since we’ll be using the Twilio Helper Library , we’ll need to install the twilio-node from npm so that it’s available to our function. To do so, we’ll need to first add a new file to our function.

Add package.json file

In the View Files window, click Add. Type the file name package.json and click enter. A new blank file should appear in the middle of the screen.

package_json_new.PNG

Head over to the GitHub repo and grab the code for this file. You’ll want to paste the code from GitHub into the file. I would suggest customizing the values to your liking.

package_json.PNG

Install the Packages

Now that we’ve added our new file, we’ll need to run npm to install the packages. We’ll use Kudu to do so! Before we head over to Kudu, you’ll want to stop your app. Head over to the Overview page for your function app and click Stop. A pop-up will display to confirm that you would like to stop the app – click Ok.

stop.PNG

Click on the Platform Features tab. Under Development Tools, click Advanced tools (Kudu). Kudu will open on it’s own in a new window.

platform_features.PNG

In the top menu on Kudu, click Debug console and select CMD.

debug_console.PNG

In the command prompt, we’ll want to navigate to D:\home\site\wwwroot. You can do so by using the command cd site\wwwroot and press enter on your keyboard.

cd.PNG

If you’re uncomfortable using the command prompt, you could use the GUI above the command prompt window to navigate the file structure.

Once you’re in wwwroot, run the command npm install twilio to install the package.

Give it a moment to install. Once it’s done, the console will let you know that the packages have been added.

npm_install_complete.PNG

You will also notice a new node_modules folder added to the file structure.

node_modules.PNG

Back in the Overview tab for the function app, click Start.

start.PNG

10. Test Your Function

Head back over to the function app and click TimerTrigger1. Make sure that you’re in the index.js file. Click Test next to View Files (far right-side of the screen). At the top of the index.js file, click Run.

run.PNG

If all goes well, you should receive a text message after 2 minutes with your message!

You can adjust the frequency for your timer by heading back to Integrate and changing the Schedule field. Be sure to read up on CRON expressions before entering a new frequency.

If you’re curious to learn more about Azure Functions, I would suggest taking this Microsoft Learn module!

And if you’re wondering what else you can do with Twilio, start up a game of TwilioQuest!

Happy Coding!

TutorialsApril Speight