Search code examples
google-apps-scripttwilio-api

How do I set the riskCheck parameter in my Twilio API call using Google Apps Script?


We have been using this code to send SMS messages from a Google Sheet, using Google Apps Script and Twilio API:

function sendSms(message) {
  const number = "+44########";
  var twilioAccountSID = "###############################";
  var twilioAuthToken = "#################################";
  var messages_url = "https://api.twilio.com/2010-04-01/Accounts/" + twilioAccountSID + "/Messages.json";

  var payload = {
    "To": number,
    "Body" : message,
    "From" : "+44#########"  //Twilio number
  };

  var options = {
    "method" : "post",
    "payload" : payload
  };

  options.headers = { 
    "Authorization" : "Basic " + Utilities.base64Encode(twilioAccountSID + ':' + twilioAuthToken)
  };

  UrlFetchApp.fetch(messages_url, options);
}

We have started getting errors with code 30450, a possible solution Twilio offer is:

...you can use the optional RiskCheck parameter when creating a Message with the Programmable Messaging API. To prevent a known/legitimate message from getting blocked in future by SMS Pumping Protection, include the RiskCheck parameter with value disable when creating the new Message resource.

Does anyone know how I can add this RiskCheck parameter to my code?


Solution

  • I do not have a Twilio account to test it, but according to their documentation you need to add riskCheck to your payload:

    var payload = {
      "To": number,
      "Body" : message,
      "From" : "+44#########",
      "riskCheck": "disable"
    };