How I'm Feeling

Overview

Back in undergrad, I was diagnosed with Major Depressive Disorder. Life was horrible. I went from being socially active on campus, attending events, excelling in class, working all of my jobs and spending time with my sorority sisters to being shacked up in my bedroom off-campus hoping that absolutely no one would surface and ask if I was OK.

It was rough - and it was hard on my mom as well. She’d check in often to see how I was feeling and honestly it was all too overwhelming for me. I had developed an odd phobia of being physically around people - therefore talking was definitely off the table. I resorted to texting her how I felt but even that too became too much effort on my behalf.

We survived those times and I’m doing significantly better nowadays. However looking back on that time in my life, I could only imagine how much of a struggle it was for her to keep up with my emotions when I didn’t want to have anything to do with communicating with anyone.

Communication can be difficult when you’re not in the right head space to function. It can be difficult for someone to understand the communicative struggle we go through when we’re hit with a depressive episode.

Point blank: It’s hard.

However, I recognize that those who care about us genuinely want to know how we’re feeling. So, I figured that there must be some sort of happy medium that would enable someone to share how they’re feeling with minimal effort..

For this tutorial, I’m going to show you how to use Twilio SMS, Azure Functions and a Flic button to send a text message to a designated phone number.

Language: JavaScript

Product(s): Twilio SMS, Azure Functions, Flic

Disclaimer: Mental health is a super serious topic that should not be ignored. This app should not be used in lieu of seeking help if you are in need. Rather, this app could be used to help keep communication channels. Please reach out to someone you confide in if you are struggling with your emotional and/or mental state.

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.

Purchase Flic Button

A Flic is a shortcut button for common tasks to control smart devices wirelessly. You can purchase one here!

So how does the Flic button work? When a person clicks the Flic button, an action will take place. In our case, a text message will be sent to a designated phone number.

When you tie in everything together, we’ll be following the workflow below:

workflow_flic.png

A person will click the Flic button which will trigger the Azure Function to send the Twilio SMS to a designated phone number.

Access the GitHub Repository

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

Tutorial

1. Decide on Your Messages

What we’re going to do is assign a message to each Flic button action. Each message will reflect a generic response that could be provided to describe how a person is feeling. For the sake of the tutorial, I will narrow down our messages to the following:

Action Message Text Message
One Click 1 I'm doing ok today.
Two Clicks 2 I need time to myself today.
Long Press 3 I'm having a really bad day.

2. Create a Function App

We’ll start off by creating the action for Message 1 (aka 1 click of the Flic button). Once we’ve created the function and implemented the logic, we can essentially duplicate our setup to create two additional functions for the double press and long press of the Flic button.

In the Azure Portal, click + Create a Resource.

create_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 -. flic-mental-health-message-1
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. flic-mental-health-message-1
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 HTTP trigger for this tutorial.

In the left menu, 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 HTTP trigger, we’ll need to use a Webhook + API. Select Webhook + API and click Create.

webhook_api.PNG

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

left_menu_new.PNG

4. Integrate with Twilio SMS

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

click_integrate.png


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

new_outputs.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 Application Settings. The Application Settings can be found on the Overview page for your app.

application_settings.PNG

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 Update – 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 function.json File

Now that we’ve added the environment variables, we’ll need to update the function.json file within our HTTPTrigger1 function. Head back over to the function app and click HTTPTrigger1. 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.

functionjsonfile_new.PNG

7. Add Message 1 to the index.js File

When Azure creates a function, it adds default code for a hello world app. We will replace this code with the code for our message.

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.

indexfilenew.PNG

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

8. 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.

blank_file.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_file.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.

cmd_prompt.PNG

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

gui.PNG

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

npm_install_twilio.PNG

Give it a moment to install. Once it’s done, you will notice a new node_modules folder added to the file structure.

node_modules_added.PNG

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

start.PNG

9. Test Your Function

Head back over to the function app and click HTTPTrigger1. 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 with your message!

10. Connect Flic Button

We’re finally at the last step of our tutorial! We now need to add the function URL to our Flic app so that when we click the button, our message is sent via Twilio SMS to the designated number.

Get the Function URL

You’ll first need to grab the function URL. Next to Run in the index.js file, click </> Get function URL.

get_function_url.PNG

Copy the function URL and send it over to yourself in whatever way gets it over to your phone. I send mine via email so that I can easily copy it.

Download Flic App and Pair Flic

Before we download the app, you’ll want to go to your phone settings and ensure that Bluetooth is enabled. Once confirmed that Bluetooth is enabled, head to the App Store or Google Play store to download the Flic app. Download the Flic app and create an account. Once you’ve completed registration, you’ll need to login to the app. An interactive guide will appear and will walk you through how to get Flic paired with your phone.

Add Function URL to Flic

Once you’ve connected your Flic, you’ll need to add the function URL to the first click. Since I have an iPhone, the instructions here will be for iPhone users. In the Flic app, click the Phone tab and select your Flic. Next to Click (this represents 1 click), click the + icon.

flic_add.png

Use the search (magnifying glass) to search for Internet Request and click Internet Request.

Take the function URL for your function pp and paste it in the URL field. Once you’ve added the URL, click Add.

flic_url.png

Test Flic

Here’s the moment of truth – click the Flic button! If all works well, the designated phone number will receive a text message!

flic_message.png


To create a function for the remaining two Flic actions, repeat steps 2 - 10! 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 and consider attending SIGNAL 2019! Oh - and before I forget..the scholarship application process is open for those in need :)

Happy Coding!

TutorialsApril Speight