Overview
In this post, we will learn how to check the Twilio Balance using Apps Script and send an email to the user when the threshold level meets.
But Twilio already sends an email when the balance reaches $5 then why do we need this script?
If that was the question that runs through your mind means think of this scenario what would you do if your client asks you to send an email when Twilio balance reaches $10 instead of $5. You cannot check every day then what you can do?
Automate it by writing a script using Apps Script
Objectives
What is Apps Script
Create a Twilio account (If you are new)
Enable Apps Script
Logic to fetch the current balance from Twilio
Logic to send an email when threshold value matches
Trigger functions to check Twilio balance daily
Overview of what we are going to do — Source: Nidhinkumar Inspired from Priyanka Vergadia
Prerequisites
- Google Workspace account
1. What is Apps Script
Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with Google Workspace. You can write code in modern JavaScript and have access to built-in libraries such as Gmail, Calendar, Drive, etc.,
Source: Nidhinkumar
2. Create a Twilio account (If you are new)
Go to the Twilio page and do the signup if you are a new user else login with the credentials available.
If you have an existing project means you can skip this step else create a new project and click Programmable SMS and click Whatsapp now you could see a page like below
Twilio Sandbox
Now copy that phone number and create a contact in your phone and then open Whatsapp and select that contact and type the code join bush-period (add the code which you see on your Twilio Sandbox :)
Once the test connection is completed click the Home icon where you could see the ACCOUNT SID and AUTH TOKEN copy both the credentials and keep it handy we will use it later
Account SID and Auth Token
3. Enable Apps Script
Open Google Sheets and click on Tools -> Script Editor in Google Sheets like below
Script Editor
Once you click the Script Editor you will see a window like below.
Script Editor
Here we will write the logic to get the current balance as well as sending an email once the threshold value reaches.
4.Logic to fetch the current balance from Twilio
To check the Twilio balance using the REST API. we can use the curl command like below
`curl -G https://api.twilio.com/2010-04-01/Accounts/your_account_sid/Balance.json \
-u "your_account_sid:your_auth_token"`
It would be something like this when you add your Account SID and Auth Token
curl -G https://api.twilio.com/2010-04-01/Accounts/AC123abc45678901c3ff481ea2/Balance.json \
-u "AC123abc45678901c3ff481ea2:baa9fe5ad79a7a97dxxx00c012a3d4ce"
Once you run the command in your terminal you could see the current balance of your Twilio account without opening the Twilio console.
In the above curl command, we have authenticated using our Account SID and Auth Token
Now we will implement the same in Apps Script for that first, rename the function to checkTwilioBalance
function checkTwilioBalance() {}
Once the function is renamed we will make the API call from Apps Script to Twilio using UrlFetchApp
What we have done in the above code we have used the UrlFetchApp which will fetch the data from the URL which we define.
Since the Twilio API contains authentication so we have used headers in which we have passed our Account SID and Auth Token
Now if you save the function and click the Run button you would be asked for review permission like below
Review Permissions
Click on Review Permissions and then click Allow for the Gmail account connected with it. Once you gave the necessary permissions your function will be run.
To see the logs click on View-> Logs.
View Logs
Click Logs you could see the response which we receive from Twilio like below
Twilio Balance
Now we are able to get the response from Twilio to Apps Script.
5. Logic to send an email when the threshold value matches
Now we will write the logic to send an email when the balance is less than $7 or whatever the limit you like
What we have done in the above code is we check if the current Twilio balance is less than 7 if so then we will call the sendEmail with subject and body
Now we will write the logic to sendEmail to the clients like below
Once done you are ready to test the script. Click on the Start(Play) icon to run the script and make sure the function is checkTwilioBalance is selected like below
Running the script
Now you will be asked for permission to allow Apps Script to use Gmail for sending an email like below
Permission Review
Account Review and Allowing to access Gmail
Email Send From AppScript when Balance is Low
6. Trigger functions to check Twilio balance daily
Now we got the email triggered when the balance is low by clicking the Run script. But every time we cannot run the application manually right. so, what we do?
Don’t worry Apps Script provides an option to run the script automatically by setting the triggers
Click Edit -> Current Project’s Trigger which will open a new tab click Add Trigger which will open a pop-up modal like below
Trigger
Trigger
Click Save now it will automatically check the Twilio balance every two hours (you can change it to your wish) and if the threshold meets then it will trigger an email.
Reference Links
- Apps Script Code — Click here to view the code
Congratulations!
You have learned how to check the Twilio balance and send Email using Apps Script
Happy Learning :)