0

I am trying to get the status of my outgoing twilio calls and update them onto the spreadsheet. "Completed, Busy, No-Answer, Cancelled, Failed".

However, I do not know where to include the StatusCallBackEvent and how to access the retrieved status from Google Apps Script.

Here is the code which I have to initiate an outbound call.

function makeCall(to) {
  var call_url = "https://api.twilio.com/2010-04-01/Accounts/" + TWILIO_ACCOUNT_SID + "/Calls.json";
  
  var payload = {
    "To": "+" + String(to),
    "From" : TWILIO_NUMBER,
    "Url": "http://a1fb888ec032.ngrok.io/" +"voice",
    "Method": "GET"
  };
  
  var options = {
    "method" : "post",
    "payload" : payload
  };
  
  options.headers = {    
    "Authorization" : "Basic " + Utilities.base64Encode(TWILIO_ACCOUNT_SID + ":" + TWILIO_AUTH_TOKEN)
  };

  var response = UrlFetchApp.fetch(call_url, options);
  UrlFetchApp.fetch(call_url, options);

  return JSON.parse(response);
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
guddaae2u
  • 23
  • 5
  • 1
    Can you provide the official document of API you want to use? – Tanaike Apr 23 '21 at 01:26
  • @Tanaike Thanks for the comment! I'm using twilio so I saw some references online but they were for other programming languages and I wasn't sure how to incorporate it into google apps script. [link] (https://www.twilio.com/docs/voice/twiml#callstatus-values) I just want to record the final call statuses in this article. I know how to update my spreadsheet using apps script. It's just connecting with twilio is confusing me. [link] (https://support.twilio.com/hc/en-us/articles/223132547-What-are-the-Possible-Call-Statuses-and-What-do-They-Mean-) – guddaae2u Apr 23 '21 at 01:42
  • Thank you for replying. When I saw your provided official document, the request of your script is correct. So in your situation, how about checking each values you send, again? By the way, why are you requesting 2 times with `var response = UrlFetchApp.fetch(call_url, options);` and `UrlFetchApp.fetch(call_url, options);`? – Tanaike Apr 23 '21 at 01:57
  • @Tanaike yea, I am still trying to see how to get the call status values. Ah, I must have forgotten to remove the second `UrlFetchApp.fetch(call_url, options);` It is commented out on my program. But thanks for spotting that out! – guddaae2u Apr 23 '21 at 02:26
  • Thank you for replying. I think that the request of your script is correct. So when each value is confirmed again, the script might work. – Tanaike Apr 23 '21 at 03:03
  • @Tanaike Yea, my call request script is correct. Now I am having issues retrieving the call status after the call has ended :( – guddaae2u Apr 23 '21 at 15:21
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike Apr 24 '21 at 00:37

1 Answers1

1

Twilio developer evangelist here.

To receive the status of the call, you need to set a StatusCallback URL when you create the call. The URL should point towards a URL which you control that can update your spreadsheet when it receives an HTTP request from Twilio.

  var payload = {
    "To": "+" + String(to),
    "From" : TWILIO_NUMBER,
    "Url": "http://a1fb888ec032.ngrok.io/" +"voice",
    "Method": "GET",
    "StatusCallback": "https://SOME_URL"
  };

You can use Google Sheets and App Script to set up a web application that can receive webhooks like this. Briefly, you need to create script that has a doPost method (Twilio webhooks are POST requests by defaults) and returns a successful response. Twilio statusCallback webhooks don't expect any content in the response, so you can use an empty text output.

Something like this might work:

function doPost(event) {
  var callSid = event.parameter("CallSid");
  var status = event.parameter("CallStatus");
  // update spreadsheet with call status
  return ContentService.createTextOutput('');
}

Check out the documentation on turning scripts into web apps and Twilio call status callbacks for more details.

philnash
  • 70,667
  • 10
  • 60
  • 88