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.
Prerequisites
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
Setting up the 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:
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.
Reminder, you can find your Account SID and Auth Token on your Console:
Next, install CLI autocomplete to add support for bash completion:
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.
Now purchase an available phone number by replacing the --phone-number
attribute with a number from the previous command:
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:
Test if the CLI is working properly by sending a simple message to your mobile device:
NOTE: The --from
attribute is the Twilio number you purchased and the --to
attribute is your test mobile number.
Create your employee or customer list
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:
If you prefer to use Vi or any CLI editor, it should look like this:
NOTE: Be sure to replace the From
and To
columns with the correct numbers.
Create a simple bash script to send the notifications
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 CliNotifications.sh
:
Test the script
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:
- If you get a
bash: ./CliNotifications.sh: Permission denied
- issue the following command to change the permissionschmod 755 CliNotifications.sh
- Check the Debugger for error messages
- Check the Calls Log for error messages (Active Numbers => Calls Log tab)
Look on the call Details and Insights Summary tabs:
- Check the Messages Log for error messages (Active Numbers => Message Log tab)
Enhance with your actual numbers
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:
Conclusion: Automating Notifications Using Twilio
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 coronavirus@twilio.com.
Possible improvements
Here are a few suggestions on how to extend what we have just built:
- Add email as a channel option. Configure a SendGrid account and use the SendGrid API or Twilio CLI to send emails as well.
- Send notifications using Twilio APIs instead.
- Improve the script to load 2 files, one for static fields (
date
,hoursStart
,hoursStop
,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
Additional resources
The following reference resources will provide you with in-depth information on some of the topics mentioned in this post:
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.