Create a Simple Notification System using Twilio CLI, Programmable Voice, and Programmable Messaging
Time to read: 4 minutes
If you find yourself needing to broadcast urgent news to your customers or employees, but do not have a notification system in place, this tutorial is for you. In this blog post we'll show you how to create a bash script to quickly send SMS messages or Voice message notifications to several recipients in a CSV.
To get started with the project you will need the following:
- A Twilio account (Sign up for a free trial account and get a $10 credit)
- Homebrew (for Mac users) or Node.js (for Windows users) to install Twilio CLI
If you haven’t already done so, go to twilio.com and sign up for a free account or log in to your existing account. Take note of your Account SID and Auth Token as you will need them to login via the CLI in the following steps.
Twilio CLI is a command line interface tool that allows you to manage your Twilio resources directly from your terminal or command prompt. This will be helpful to prevent us from switching between the browser and our terminal since we’ll be using the command prompt to develop our application. Install Twilio CLI for your respective operating system by following this guide.
Once Twilio CLI is installed, login into your account by running this command in your terminal:
$ twilio login
You will be prompted to enter a local ID for the credentials known as the Shorthand identifier, followed by your Account SID and Auth Token. Use “TwilioAccount” as seen in the screenshot below if you don’t know what to specify for the identifier.
Next, install CLI autocomplete to add support for bash completion:
$ twilio autocomplete bash
Let’s test out the CLI by listing phone numbers available for purchase by area code. Be sure to replace the
--area-code with the value of your desired region.
$ twilio api:core:available-phone-numbers:local:list \
--area-code="222" --country-code US
Now purchase an available phone number by replacing the
--phone-number attribute with a number from the previous command:
$ twilio api:core:incoming-phone-numbers:create \
Now that you have tested listing and purchasing a phone number via the CLI, review your phone numbers with a filter for the area code:
$ twilio api:core:incoming-phone-numbers:list --phone-number="+1222"
Test if the CLI is working properly by sending a simple message to your mobile device:
$ twilio api:core:messages:create \
--from "+12223334444" \ --to "+15556667777" \
--body "Hello from Twilio!"
--from attribute is the Twilio number you purchased and the
--toattribute is your test mobile number.
As mentioned before, a list of recipients will need to be created in a simple spreadsheet (.csv file), containing the information you will use to send notifications. For each use case (SMS vs Voice) the fields will vary. We will show you a simple example where we want to send SMS and Voice greetings to our customers by name, and inform them of the store hours on a certain date. We are assuming that customers OPTed IN to receive such notifications. These examples could also be easily modified to send information to employees being notified of an office closure or any news that is important and requires immediate response.
In this example, create a small
notification.csv file with the following columns and save it to your computer:
NOTE: Be sure to replace the
To columns with the correct numbers.
We will now create a bash script (or plain text file with a series of commands) to parse our CSV and contact the recipients accordingly. If you are new to programming, please check this guide to learn more about bash scripts.
In your terminal, create a new file named
$ touch CliNotifications.sh
Make sure you saved both files in the same directory.
Execute your script by running
./CliNotifications.sh and check the results.
You should have received a SMS message on your first number and a Voice message on your second as shown in the screenshot below.
In case it does not work, follow these steps to troubleshoot:
Look on the call Details and Insights Summary tabs:
If all worked well, you can add all the real customers or employees information to your list. You can also modify the messages by removing or commenting out the following in your code:
# echo "Date : $date"
# echo "HoursStart : $hoursStart"
# echo "HoursStop : $hoursStop"
# echo "From : $from"
# echo "Name : $name"
# echo "To : $to"
# echo "Channel : $channel"
In this blog, you have learned how to use Twilio CLI and a Bash script to automate sending notifications to customers or employees.
Note: It is important to observe the proper security and HIPAA compliance measures when using Twilio Programmable Messaging and Voice to contact patients about their individual health status/symptoms. Contact Twilio if you have any questions.
If your organization would like more guidance on how to build solutions or would like to partner together on your work addressing the outbreak, we’re here to help. Reach out to email@example.com.
Here are a few suggestions on how to extend what we have just built:
twilio api:core:messages:list \
-o tsv \
| twilio email:send \
--text 'Message log'
- Send notifications using Twilio APIs instead.
- Improve the script to load 2 files, one for static fields (
from) and another for the ones that vary per customer or employee (name, to).
Al Kiramoto is a Solutions Engineer at Twilio. He lives in Dallas, TX and enjoys working with customers solving business problems - besides a good barbecue and TexMex food. He can be reached at akiramoto [at] twilio.com
The following reference resources will provide you with in-depth information on some of the topics mentioned in this post:
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.