Building a DIY ADHD Medication Reminder with Azure Functions & Twilio
Tags: javascript, node, tutorial, serverless
Published: 2019-06-04 23:01:18

---
title: "Building a DIY ADHD Medication Reminder with Azure Functions & Twilio"
published: true
description: "A step by step guide on using Azure Timer Triggers and the Twilio API to remind you of important things."
tags: javascript, node, tutorial, serverless
cover_image: https://thepracticaldev.s3.amazonaws.com/i/z497pbzx5c0680j21hdh.jpeg
---

Lately, I've been playing around with Azure Functions to automate different parts of my life. Just today, as I was building a demo using Functions for an upcoming work project, I looked up at the clock and discovered it was 4pm.

### 🙋‍♀️ The Good News: Time flies when you're coding!
### 🤦‍♀️ The Bad News: I do not function as well without my ADHD medication and well… I forgot to take my ADHD medication.


![](https://media.giphy.com/media/xT1XGXjrQSB337IszC/giphy.gif)

Here’s the thing about us folks with ADHD- we have trouble with our [working memory](https://en.wikipedia.org/wiki/Working_memory). If you’re unfamiliar working memory it is the part of our memory that is able to keep information long enough to remember what’s next, stay focused on a task, or use info in the short term (you can think of it like a cache). For example, when I’m telling a story I’ll often get excited, go on a tangent, my brain runs it's garbage collector, and completely forget what I was originally talking about. Also, I often have to look at the weather/ask our Google home for it 2–3 times before I remember the temperature for the day. Let’s just say I’m not Dori from Finding Nemo-level forgetful but… I’ve been known to completely forget important things in the moment, especially tasks like taking my medication twice a day. 😣 Perhaps **that’s** why I began to feel like my brain was moving slower this afternoon, eh?

![](https://media.giphy.com/media/l0D2PvoPVcXGegM1y/giphy.gif)

So, I stopped what I was doing, took a half pill (I didn’t want to be up until 1am 😳), and wrote up this handy guide on building your own ADHD medication (or, whatever you’d like to be reminded about) reminder with [Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/?WT.mc_id=azuredevtoreminderapp-blog-chcondon).

# Let’s Build It (before we forget to 😅)!

### Step 1: Create an Azure Function

For the sake of easy to understand visuals/screenshots I used the Azure portal to create this. You can also use [VS Code](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code/?WT.mc_id=azuredevtoreminderapp-blog-chcondon), the [Azure CLI](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function-azure-cli/?WT.mc_id=azuredevtoreminderapp-blog-chcondon), etc.​ With Azure Functions you are given the the ability to code and test Functions locally on your machine without having to deploy to the cloud every single time you want to test (a huge time saver!).

To create an Azure Function, you can just start from the Get Started menu and select Function App.

![](https://thepracticaldev.s3.amazonaws.com/i/gyevy34y36l2py02e2y7.png)

Then you’ll need to fill in some basic info about your function here. Including the app name, the Azure subscription you’d like to use, a [resource group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-overview/?WT.mc_id=azuredevtoreminderapp-blog-chcondon) (I’m creating a new one in this case), the Operating System you’d like to use, the [hosting plan](https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale/?WT.mc_id=azuredevtoreminderapp-blog-chcondon) (I’m using consumption), the location I’d like to use (I’m in California, so West US 2 is usually my default), the runtime stack I’d like to use (I’m using JavaScript in this case), and I have the option to create new [storage](https://docs.microsoft.com/en-us/azure/storage/common/storage-introduction/?WT.mc_id=azuredevtoreminderapp-blog-chcondon) or use existing. I created a new one in this case.​​

![](https://thepracticaldev.s3.amazonaws.com/i/v8qv7hgadhjqh6cqqzin.png)

Once I have all these filled out, I can go ahead and deploy! Wait about a minute or two, then watch for the **Deployment succeeded** message.​

If you followed those steps, we have our resource! We’ll just select **“Go to resource”** to view your new Function App.​ Now we’ll add a new function.

![](https://thepracticaldev.s3.amazonaws.com/i/64ut6v7chdl2oqb3goy5.png)

![](https://thepracticaldev.s3.amazonaws.com/i/ucn9jyhkmdhlcpxczogq.png)

When you select Trigger Timer, a prompt will appear asking for the **Name** of your function, as well as the **Schedule** for your trigger. I’ve put “0 0 15 * * *” so my reminder will be sent at 3pm (*NOTE: check out the timestamp in your logs- Azure uses Coordinated Universal Time- UTC*).

![](https://thepracticaldev.s3.amazonaws.com/i/zvlthn28efuc4wl357pv.png)

But how will this reminder be sent? Wouldn’t it be great if you had a personal assistant who could text you daily to check-in to see if you’ve taken your pills? Well, while we don’t have time to build a robot assistant to go do a coffee run for us, we **can** use the Twilio API to text us about it! You’ll need to create a [Twilio](https://www.twilio.com/referral/xBBj30) account to do this, and grab the SID and Auth Token from there once created (☎️ *you can sign up for Twilio and get $20 with my personal code: CHLOE20*). **I highly suggest adding these to your app settings immediately so they are secure** (vs. adding them directly to your code).

### Step 2: Integrate with Twilio

![](https://media.giphy.com/media/jPgEkqIWTmYLK/giphy.gif)

You’ll need to add your **RECIPIENT_NUMBER**, **SENDER_NUMBER** (I am using a phone number I purchased from Twilio) as well as **TWILIO_TOKEN** and **TWILIO_SID** to your Application settings under Configuration. As I mentioned before, I **highly** suggest adding these to your [app settings](https://docs.microsoft.com/en-us/azure/azure-app-configuration/overview/?WT.mc_id=azuredevtoreminderapp-blog-chcondon) vs. adding them directly to your code, so they are secure. If you’re looking for a good video walkthrough on the how/why of Azure app settings, check out [this video](https://azure.microsoft.com/en-us/resources/videos/configuration-and-app-settings-of-azure-web-sites/) with [Scott Hanselman](https://dev.to/shanselman) and Stefan Schackow.

You’ll also need to make sure you have Twilio installed. You can simply enter the following in your terminal.

```
npm install twilio
```

![](https://thepracticaldev.s3.amazonaws.com/i/wn8fr7x8qe1c9dapvjoe.png)

### Step 3: Write some code (quickly, before we get too distracted! 😅)

Here’s a look at the code 👉 [index.js](https://gist.github.com/ChloeCodesThings/344b84004cf0c1584cbff6797f6ea5a1#file-index-js)

As you can see, I have created a function that checks if my timer is past due (if so, I write that to my logs). Then, I’m creating a message (.create) that will be sent from SENDER_NUMBER, includes the body of my message, and will be sent to RECIPIENT_NUMBER. If all goes according to plan, ‘Text sucessfully sent’ will be written to my logs. Otherwise, I am logging the error.

Alternatively, you can set up [bindings](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-twilio/?WT.mc_id=azuredevtoreminderapp-blog-chcondon) in a function.json file. You can take a look at a Gist I made of those [here](https://gist.github.com/ChloeCodesThings/9a147ac0c4edfdc808469191d216cf15). [Bindings](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-twilio/?WT.mc_id=azuredevtoreminderapp-blog-chcondon) can also be edited and updated through the **Integrate** section of the Azure portal like so:

![](https://thepracticaldev.s3.amazonaws.com/i/94p7dgrcrqw896g4wbc2.png)

However, with the code we’ve written, we do not need bindings set up- we can simply call the function, and reference the variables (SENDER_NUMBER, TWILIO_SID, etc.)from our app settings.

![](https://media.giphy.com/media/2XskdWMBUY1yxD6Hcic/giphy.gif)

As you can see, it’s pretty simple. Create a text message, send it, and ta-da — reminder sent! But there’s a problem… sometimes I get so distracted or focused on a task that I won’t see a text (also, I get a lot of notifications on my phone- what if I don’t notice it? 😬). So, I decided to add a back-up plan… a phone call!

The code for making a phone call is similar to sending a text message, with a few minor changes.


Here’s a look at the code 👉 [index.js](https://gist.github.com/ChloeCodesThings/8137aa645fd1a5eebaf20e3ad7c69b62#file-index-js)

You’ll notice that the code for our phone call refers to ‘TWIML_URL’. This is referring to a URL in my application settings that links to a [TwiML Bin](https://www.twilio.com/docs/runtime/tutorials/twiml-bins) which I have set up in Twilio. A [TwiML Bin](https://www.twilio.com/docs/runtime/tutorials/twiml-bins) allows us to create static TwiML documents. They provide us with a private URL that we can then use to configure our Twilio SMS messages or Voice webhooks. In this case, I’m using a TwiML Bin](https://www.twilio.com/docs/runtime/tutorials/twiml-bins) to hold the logic for what should occur during the call.

![](https://thepracticaldev.s3.amazonaws.com/i/6z3zc3qz9gaaa6l7bnny.png)

You can create interactive voice and messaging applications using [TwiML](https://www.twilio.com/docs/voice/twiml). Here is an example of how I am using it for my call:

![](https://thepracticaldev.s3.amazonaws.com/i/kn68z4tj5l8hllrdh8cz.png)

The URL provided at the top of the TwiML Bin page is the URL I have secured in my app settings as **TWIML_URL** (referred to in index.js for the phone call reminder function). You’ll notice I’m using syntax similar to Markdown in the TwiML Bin. It will say a message with a pill reminder (using <Say></Say>), and then play an MP3 (using <Play></Play>). The Mp3 is optional, but I decided I wanted a message to play for a minute or two, allowing me to find my pill box, take the pill, and hang up when I’m done. What’s the Mp3? Well, here’s a demo of it in action:

{% youtube mliMzdK1Nq8 %}

The MP3 is being hosted by Twilio through [Assets](https://www.twilio.com/docs/runtime/assets). To create a new Asset, simply press the + button to upload a MP3 (you could also use [Twilio Assets](https://www.twilio.com/docs/runtime/assets) to add an image to your reminder text if you wish!). This asset is an MP3 of [Shia LaBeouf yelling a motivational speech](https://www.youtube.com/watch?v=ZXsQAXx_ao0), but you can make the MP3 a song, message from yourself, or any other sound you wish!

![](https://thepracticaldev.s3.amazonaws.com/i/4cvxuub9l36a79f4rogx.png)

### Step 4: Test it out (before we get distracted by another project)!

Finally, we need to test out our Trigger Timer! If you’d like, you can add your Twilio number to the contacts in your phone. I named mine “💊👩🏻‍⚕️💊”, but you can make yours discrete if you wish (Grandma/Dad/Cousin/Jennifer work just as well!). That way, when a call comes through at your set time, you know what it is.

![](https://thepracticaldev.s3.amazonaws.com/i/zvhont5ac20426ygmofh.jpeg)

To test your call/text, you can simply press the **Run** button in the Azure portal in each function (located at the top of your **index.js** file next to **Save**). However, you’ll also want to test out the timer to make sure the text/call is firing off at the correctly specified time. It is likely that whatever time you chose to have your call/text send is set to a time that has either passed, or has not happened yet. You can edit the timer for your function in your **function.json** file, or click over to **Integrate** to edit it in the portal.

![](https://thepracticaldev.s3.amazonaws.com/i/tp42i7m26k1pk3j83zib.png)

A quick important note to avoid confusion: **check the timestamp in your logs to help determine the correct cron expression to include** (otherwise, your alert may send at an incorrect time!). You’ll want to make sure that your time corresponds to the Coordinated Universal Time (UTC) Azure uses.

### Step 5: Set it and forget it! ⏰

![](https://media.giphy.com/media/fUNGb8eMRelIi9Sit8/giphy.gif)

If your function works, and is sending at the correct time- congrats, you built an [Azure Trigger Timer](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer/?WT.mc_id=azuredevtoreminderapp-blog-chcondon)! 🎉 Feel free to customize the code however you’d like to help automate reminders for yourself, or if you’re looking for something more on-demand, you can take a peek at my article on creating a fake boyfriend call/text using Twilio + Azure [here](https://medium.com/microsoftazure/an-ambiverts-guide-to-azure-functions-95931976c565). Happy coding!

![](https://media.giphy.com/media/qK6ccDxjSmmre/giphy.gif)