Twilio FAX Send
Sends the PDF file specified by the URL as a fax.
2017 (C) Questetra, Inc. (MIT License)
Configs
  • C1. ACCOUNT SID *
  • C2. AUTH TOKEN *
  • C3. From Telephone Number (You got at Twilio) ex. +8150XXXXXXXX *
  • I1. To Telephone Number ex. +8175XXXXXXXX *
  • I2. URL of PDF sending as FAX *
  • O1. Data Item to save status confirmation url
  • O2. Data Item to save FAX SID
  • O3. Data Item to save error
Script
(function(){
  var accountSid = configs.get("AccountSid");
  var authToken = configs.get("AuthToken");
  var from = configs.get("From");
  var to = configs.get("To");
  var pdfUrl = configs.get("PdfUrl");
  var statusUrlItem = configs.get("StatusUrlItem");
  var faxSidItem = configs.get("FaxSidItem");
  var errorItem = configs.get("ErrorItem");

  var response;
  try{
    response = httpClient.begin()
      .basic(accountSid, authToken)
      .formParam('To', to)
      .formParam('From', from)
      .formParam('MediaUrl', pdfUrl)
      .formParam('quality', 'standard')
      .post('https://fax.twilio.com/v1/Faxes');
  } catch(e) {
    if(errorItem != null) {
      engine.setDataByNumber(errorItem, e.message);
      return; // ここで終了
    }
    throw e;
  }

  if (response.getStatusCode() != 201) {
    var error = response.getStatusCode() + "\n" + response.getResponseAsString();
    if (errorItem != null) {
      engine.setDataByNumber(errorItem, error);
      return; // ここで終了
    }
    throw error;
  }

  var responseJson = JSON.parse(response.getResponseAsString());
  if (statusUrlItem != null) {
    engine.setDataByNumber(statusUrlItem, responseJson["url"]);
  }
  if (faxSidItem != null) {
    engine.setDataByNumber(faxSidItem, responseJson["sid"]);
  }
  engine.setDataByNumber(errorItem, '');
})();

Download

Capture

Notes

  • You need to create a project on the Twilio console, and acquire an ACCOUNT SID / AUTHTOKEN / Telephone number beforehand
%d bloggers like this: