Create a Balance Reminder with Vonage Account API and Google Apps

Being a freelancer, I have helped a couple of local businesses in India implement Vonage products. Recently, one of my clients asked if they can get a reminder email when the Vonage balance is below a specified limit as they don’t want to hamper their operations because of insufficient balance. Almost all of my clients use Google Workspace, so I decided to create an integration of Vonage and Google Apps Script to create this workflow.

Google Apps Script allows us to manage all of Google apps using one platform in the cloud. The best part is that the authentication is baked into the platforms and many businesses in the market use Google Workspace-formerly known as G-Suite.

In this blog post, we will learn how to create custom notifications when our Vonage Account balance is below a specified limit. The post is aimed at those Vonage developers who want to manage their client base effectively by sending them reminders about the Vonage Account balance.

Prerequisites

  • Vonage API Account — We will need a Vonage API account. We can sign up today and start building with free credit. Once we have an account, we can find our API Key and API Secret at the top of the Vonage API Dashboard.

Vonage API Account

Create a Google Apps Script Project

Check Balance

Google Apps Script is written in JavaScript, so even if you have basic JavaScript knowledge you can get started.

Let us create a new function, fetchBalance, and try to connect to the Vonage REST API by sending a GET request to check our balance. We need to write the following code in Code.gs file:

First things first. Before running the above script, we need to get our Vonage API Key and Vonage API Secret. Both of them are available at Vonage Dashboard.

Replace your set of credentials in the above script and run the fetchBalance function.

When we run a function for the first time, we will be asked to authorize the script execution.

We will be provided with a list of all the permissions required to execute the script.

Once we have authorized our Google Apps Script project, the above function will execute and the response from the Vonage Account API will be logged in our Logger console.

To retrieve the balance from the response, let us add the following lines of code to our function:

Let us re-run the function. We will see that this time we are not prompted for permissions and our function will be executed directly.

Alright, now we have successfully got our Vonage Account balance.

Specify Balance Limit

We have added a new variable, balanceLimit, to specify the amount below which we should be alerted via an email. Then at the end of the script, we have added a simple if condition to compare our current balance and balance limit. Let us run the above script, and check our logger for any logs.

Send an email

For any email, we need three things: email recipient, email subject, and email body. Let us modify our fetchBalance function, and add email functionality:

Feel free to modify the emailSubject and emailBody as per your liking.

We will be asked for permissions again. It is because we have added a piece of code to send an email that requires user authorization, as the script will send the email on the behalf of the user. So, we need to authorize the new permissions.

Once the script execution is completed, we need to check our inbox for the email.

Note: The email will only be sent if your account balance is lower than the balance limit you have set in the script.

Check Balance Periodically

The trigger frequency depends entirely on our use case. If our Vonage API is used often, it makes sense to run the script more frequently, for instance, on an hourly basis. Otherwise, we can create a trigger that runs our fetchBalance function once a day or once a week. Your call!

Let us go ahead and create a trigger programmatically. In Code.gs, we need to add the following function:

The above script does exactly what it says. It creates a trigger that runs our fetchBalance function every single hour. Let us run the createTrigger function by selecting it from the action bar.

We will be prompted again for an authorization request. The triggers require the Allow this application to run when you are not present permission. Let us authorize the script and wait for its execution.

Once the execution is complete, we need to verify whether a new trigger has been created or not.

In the Sidebar, let us go to Triggers.

Alright. We can see that a trigger has been created. We need to wait for some time before the trigger executes.

We have just created our custom balance reminder. It is really helpful for continuous service.

What Next?

Developer content from the team at Vonage, including posts on our Java, Node.js, Python, DotNet, Ruby and Go SDKs