SMS Follow-ups for Outbound Calls with Vonage and Zapier
Published on December 5, 2024

SMS Follow Up ExampleSMS Follow Up ExampleIn a previous article,“Make Phone Calls from Google Sheets”, I showed how easy it is to make outbound calls without needing a server or standalone application. All you needed was a Google Sheet of contacts and an AI Studio agent. In a separate article, “Building a Resilient Voice Agent: A Guide to Failover Systems,” I showed how you could trigger a follow-up SMS if your outbound call isn’t answered.

However, I didn’t think that someone would necessarily combine them! Thanks to Alice on the Vonage Community Slack, I was proven wrong. The problem was that the Resilient Voice Agent article relied on a Node server. But what if you want to build a fully no-code / low-code solution like Alice? You’re in luck because we’ll do just that in this article!

This tutorial will demonstrate how you can use Zapier to connect an outbound voice agent to an outbound SMS follow-up agent using no code!

Prerequisites

Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

How to Create An Outbound Voicebot

To create your agent, follow the instructions found in the AI Studio documentation. There are three important options to select:

  • Type: Telephony

  • Template: Start From Scratch

  • Event: Outbound

The functionality of your agent will be quite simple: a single robotic call with your birthday well wishes. You will use a single Speak Node and an End Conversation Node.

AI Studio Bot OverviewAI Studio Bot Overview

How to Create Custom Parameters in AI Studio

Inside your SpeakNode, you’ll need 2 parameters. So open the Properties panel on the left-hand side and choose parameters. Under Custom Parameters create the following entries:

  • name - @sys.any 

  • message - @sys.any

Ensure the parameters have been saved and open your Speak Node. Inside the node, add the following message. Use your parameters by typing the $ followed by the parameter name. For instance, to use your name value, you will type $name.

<speak><break time='1s' /> <p> Dear $name</p> <p>Your loving friend YOUR_NAME has the following birthday message for you: $message </p> <p>Have a wondeful birthday!</p></speak>

You’ll notice that you are using Speech Synthesis Markup Language to add a one-second pause and create different paragraphs for the agent to speak more naturally.

Click Save & Exit.

How to Connect Your Virtual Agent to a Virtual Number

The final step is to connect your agent to a virtual number. This guide explains how to publish your agent.

How to Create a Google Sheets Database

Open Google Sheets and click Blank Spreadsheet. Give your spreadsheet a nice title like Birthday Messages. Also, name your sheet birthdays.

Your sheet will have 4 column headers:

  1. name

  2. phone_number

  3. birthday

  4. message

Add at least 1 friend's worth of data, but to see the full logic add about 10.

Google Sheet Sample DataGoogle Sheet Sample Data

  • For phone_number, add your number or another phone number you can access to receive SMS for testing. Phone numbers should be in international format without any + or 00. For example, a US number would be 15552345678

    • Ensure your phone_number column is formatted to “Plain text” or you will later have problems with the data.

    • Ensure that birthday is formatted to be of Date type. E.g., 9/26/2008.

Select Plain TextSelect Plain Text

How to Convert Your Google Sheet to JSON

Now, you will turn your Google Spreadsheet into a programmable app! Open the Extensions tab and select Apps Scripts. Apps Scripts allows you to write code on top of Google Workspace applications like Google Sheets or Google Docs to automate and extend their functionality. 

First, give your new project a nice title like Birthday Messages API. Clear the code in code.gs and replace it with the following:

function sendBirthdayCall() {
 // Open the spreadsheet by name
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("birthdays");
  // Get today's date
 var today = new Date();
 var todayDay = today.getDate();
 var todayMonth = today.getMonth() + 1;
  // Get all the data from the sheet
 var data = sheet.getDataRange().getValues();
  // Iterate through each row to check for birthdays
 for (var i = 1; i < data.length; i++) {
   var name = data[i][0];
   var phoneNumber = data[i][1];
   var birthday = new Date(data[i][2]);
   var birthdayDay = birthday.getDate();
   var birthdayMonth = birthday.getMonth() + 1;
   var message = data[i][3];
  
   // Check if today is the person's birthday
   if (birthdayDay === todayDay && birthdayMonth === todayMonth) {
     // Prepare the JSON payload
     var payload = {
       "name": name,
       "phone_number": phoneNumber,
       "birthday": birthday.toISOString(),
       "message": message
     };
     const payload_string = JSON.stringify(payload)
 
     // Send POST request to AI Studio
     sendRequest(payload);
   }
 }
}

This code creates a sendBirthdayCall function, which searches our spreadsheet for the birthdays sheet. It then iterates through each row, checking if today is the person’s birthday by comparing the value for day and month. If so, it creates the payload object that contains the birthday boy's or girl's information. It then passes this information to the sendRequest function, which will send the birthday information to AI Studio.

How to Send an Apps Script POST Request

You will now create your sendRequest function to trigger your Outbound Virtual Agent. You can paste the following code below the sendBirthdayCall() section.

To make this code work, you’ll need to update 4 values:

  1. aiStudioUrl 

  • The endpoint depends on the region you selected for your agent:

For EU agents →https://studio-api-eu.ai.vonage.com/telephony/make-call

For US agents --> https://studio-api-us.ai.vonage.com/telephony/make-call

2. X-Vgai-Key

  • Add your AI Studio API Key.  You can find the X-Vgai-Key at the top right of your AI Studio canvas. Click on the "user" icon, and then "Generate API Key".

3. agent_id

  • Add the id of your Vonage AI Studio agent. This can be found under agent details.

4. Status_url

  • Leave the status_url blank for now. This will be updated with the URL from Zapier.

function sendRequest(payload) {
// Replace with either the US or EU URL
 var url = '';


  // Define headers for the POST request
 var headers = {
   'Content-Type': 'application/json',
   'X-Vgai-Key': '' // Replace with your key
 };
  // Define the body for the POST request
 var body = {
   "to": payload.phone_number,
   "agent_id": "", // Replace with your agent ID
"status_url": "", // Replace with webhook URL from Zapier

   "session_parameters": [
     {
       "name": "name",
       "value": payload.name
     },
     {
       "name": "message",
       "value": payload.message
     }
   ]
 };
  // Make sure to set proper options for your API request (e.g., headers, authentication)
 var options = {
   'method': 'post',
   'headers': headers,
   'payload': JSON.stringify(body)
 };


  // Send the POST request
 var response = UrlFetchApp.fetch(url, options);


 // Add a two-second delay before sending the next request
 Utilities.sleep(2000);
  // Log the response (you can do more error handling here)
 Logger.log(response.getContentText());
}

You can now test that your code works by clicking Run.

For testing, make sure that at least 1 of the contacts has a birthday that is on the current day and a phone number you can access (like your personal number).

Cool, so we have an automated outbound agent! Next, we need to create the follow-up SMS agent.

How to Create a Follow-up SMS Chatbot

Now you’ll need to create a second agent. In this example, we’ll use SMS as a failover method, but you can also use WhatsApp or HTTP similarly. There are three important options for our agent, select:

  • Type: SMS

  • Template: Start From Scratch

  • Event: Outbound

Our SMS agent will be quite simple. Add 2 nodes:

  1. A Send SMS node with the text “Hey, I tried to call to wish you a happy birthday. I hope you have a fantastic day!”

    • The “To” field should be set to $SENDER_PHONE_NUMBER system parameter, which will get its value from the request we will send to Studio in the next section.

    • The “From” field should be replaced with the $AGENT_PHONE_NUMBER.

  2. (Optional) A Send Email node that you can configure to your personal email with a subject and message of your choosing

Simple SMS Agent in AI StudioSimple SMS Agent in AI Studio

Lastly, publish your second agent like you did before with your second Vonage number.

How to Use Zapier to Connect Two Outbound Agents

We’ll use Zapier to connect our two agents with just four steps:

  1. Catch the Raw Webhook from AI Studio

  2. Clean the Incoming JSON Data

  3. Check if the Call Status was “answered”, if not:

  4. Send a POST Request to Trigger the SMS Follow-up 

How to Catch a Raw Webhook in Zapier

First, create a new blank Zap. For your trigger app, select Webhooks by Zapier. For the trigger event, select Catch Raw Hook.

Select Catch Raw Hook for your Zap TriggerSelect Catch Raw Hook for your Zap Trigger

When you click continue, Webhook will create a webhook URL. This endpoint is the “status_url” endpoint that we left blank in our Apps Scripts request. You can now replace the empty string with your Zapier webhook URL.

Zapier Webhook Endpoint used for status_urlZapier Webhook Endpoint used for status_url

Your update script in Google Sheets should look something like this:

Updated Apps Scripts code to include Zapier Webhook URLUpdated Apps Scripts code to include Zapier Webhook URL

You can now hit Run in Google Sheets. But this time, ignore or reject the call. This will trigger AI Studio to send a request to the Zapier Webhook. Then, you can return to Zapier and test the trigger. You should see a record returned. When you examine the record, you’ll see a bunch of data. It might look scary, but don’t worry; we’ll clean that up next.

Test results for trigger testTest results for trigger test

How to Clean Up JSON Payload in Zapier

In the last step, we returned unformatted data from our POST request. You might have noticed all the good stuff inside the raw body

If you look inside the raw body, you’ll see an attribute called status. This information from AI Studio lets us know whether the call was or wasn’t answered. To get it, we’ll add the Code by Zapier action.

Add the Code by Zapier as the second step in your ZapAdd the Code by Zapier as the second step in your ZapFor the “Action event,” select Run Javascript. We’ll use this code block to clean up the raw data and transform it into usable JSON.

For the Input Data, create a new key called Payload. For the value, click the + sign and select Raw Body.

Then, for the custom code, add the following:

let Payload = JSON.parse(inputData.Payload.replace(/%g/,""));
output = [{Payload}];

This code cleans up the inputData and parses it into a JSON object. This way, we can use the keys and values for logic in the following steps.

Transform the Raw Body into a Javascript Object called PayloadTransform the Raw Body into a Javascript Object called Payload

When you test this block, you’ll see that you now have access to data fields called “Payload From” (your Voice Agent number), “Payload To” (your test phone number), and most importantly, “Payload Status.” 

Now that we have access to the value of Payload Status, we’ll want to create a condition.

How to Create Conditions with Zapier Filter Action

Add another step in your Zap, and add the Filter by Zapier Action. Create the following condition:Only continue if:  Payload Status does not exactly match answered

Create the condition to filter Payload StatusCreate the condition to filter Payload Status

The Zap will continue for all cases unless the Payload Status equals “answered.” So we can now add our final action: connecting to our follow-up SMS agent!

How to Create a POST Request with Zapier

In our trigger, we used Zapier to listen for POST requests from AI Studio. But now, we will use Zapier to send a POST request to AI Studio and trigger our SMS follow-up. 

Again, select Webhooks by Zapier for the event App. This time, for the Action event, choose POST.

Select POST webhook actionSelect POST webhook action

Click continue to configure the POST request. Here you must configure six fields: the URL and Payload Type, 3 fields in the Data section, and one field in the Headers.

URL

For the URL, you must check if your agent is US or EU-based. Add the corresponding URL:

Payload Type

Change the payload type to json.

Data

Add the following 3 fields with the corresponding values:

  1. Key: to
    Value: Payload To

  2. Key: agent_id
    Value: the ID of your outbound SMS agent; the second agent you created

  3. Key: channel
    Value: sms

Headers

Add the following field and value:

  1. Key: X-Vgai-Key
    Value: your AI Studio API Key

POST request properly formattedPOST request properly formatted

You can then test this step and receive the SMS on your phone. If everything worked, publish your Zap, you’re finished!

Conclusion

And that’s it- a totally no-code / low-code outbound phone and SMS solution! Were you able to make it work? Did you have any troubles? Do you have other preferred low-code platforms besides Zapier and Google Sheets? I would love to hear about it!As I said before, the inspiration for this tutorial was a community question on our Community Slack. Join me in the #ai-studio channel and let me know how you found this tutorial. You can also follow VonageDev on X for the latest Vonage developer news.

Additional Resources

Benjamin AronovDeveloper Advocate

Benjamin Aronov is a developer advocate at Vonage. He is a proven community builder with a background in Ruby on Rails. Benjamin enjoys the beaches of Tel Aviv which he calls home. His Tel Aviv base allows him to meet and learn from some of the world's best startup founders. Outside of tech, Benjamin loves traveling the world in search of the perfect pain au chocolat.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.