Send a fax to Dropbox

Dropbox has a wonderful API that allows you pass a publicly available URL and it will download the data and store it into dropbox on your behalf.  This is incredibly useful since Twilio on receipt of fax builds a URL which the content can be accessed from. 

We don't even have to download the attachment and stream it to Dropbox, Dropbox will do it for us!

Prerequisites to Fax with Dropbox

  1. A fax enabled number from Twilio
  2. A Dropbox account

Dropbox Setup

Login to your dropbox account (or create a new one here) and navigate to https://www.dropbox.com/developers/apps/create.

Create a Dropbox app

Next, you'll need to create a new DropBox app.

  1. Select Dropbox API
  2. Select App folder
  3. Give your folder a name like Modern Fax

Dropbox App

Configure your Dropbox App

In the following screen, in the OAuth2 section:

  1. Set Allow implicit grantDisallow
  2. Click Generate access token and save this for later

App Settings

The access token does not expire and anyone with access to it can perform CRUD operations on your App folder, so keep it safe!

Create a function to receive faxes

We need to tell Twilio what we should do when we want to receive a fax.  In our case, we want to push the file to Dropbox.  

Setup Config

  1. Add a new environment variable called DBX_ACCESS_TOKEN and paste in your access token.
  2. Add a new dependency for the npm package request version 2.88.0

Create a Function

  1. Create a new function
  2. Set the Function NameModern Fax 
  3. Set the Path = /fax-to-dropbox
  4. Copy/Paste the code below into your function
  5. Click Copy on the function url, and paste somewhere.  You'll need it in the next step!
const request = require('request');

exports.handler = function(context, event, callback) {

    var path = `/${event.FaxSid}.pdf`;

    var uploadData = {
        "path": path,
        "url": event.MediaUrl
    };

    // upload to dropbox
    request.post({
        url: 'https://api.dropboxapi.com/2/files/save_url',
        auth: {
            bearer: context.DBX_ACCESS_TOKEN
        },
        body: uploadData,
        json: true
    },
    (e, r, b) => {
        console.log(r.statusCode);
        if (e) {
            return callback(e);
        } else {
            return callback(null, b.async_job_id);
        }
    });
};

Random Details about this code.

  1. This creates your file in the root directory of your dropbox app (/Apps/ModernFax/)
  2. The name of the file is set to the FaxSid in this sample, feel free to change to whatever you want.
  3. You can do many things through the Dropbox API such as create folders, so feel free to extend!

Create a TwiML Bin

Now we need to tell Twilio if we want to receive the incoming fax or not.  We have two options, <Receive> or <Reject>.  In this example, we won't build any special logic to reject faxes, we will just take everything.

  1. Navigate to https://www.twilio.com/console/runtime/twiml-bins
  2. Create a New TwiML Bin
  3. Set Friendly Name = Fax Handler
  4. Set TwiML = the code below. 
  5. Replace {function_url} with the full URL to the function you created above.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Receive action="{function_url}" />
</Response>

Configure your Twilio Number

You're going to need a Twilio number that supports fax to build this app, so log in to your Twilio account. You can buy a new number or you may already have one, just look for this icon to show that it can receive fax:

nQIhmVPBdTpbLsB-58rra_bGRNZpKkyuOCEpt9CzpOw0AClQ5a8jLy82g3VL2OgNV9MXYVbNnJ7wKdKKlYZpTkDLsp7AqYZgHErJxglCF1WcG-Xe5ImC6TxBpLNsNddd6bj8RfY

 

Once you have your number, you'll need to configure it to point to the TwiML Bin you created.  Head back to edit your fax capable number. In the "Voice & Fax" section make sure you are set to accept incoming faxes.  

MuGw-WSEv-fM7PqaF9SDj_-XCAnmcPbHDTLbJDlqibTpKtBjmk3eFYXsABvva4dfhhr-VsXHP2k5r5_b4MePW8QsH1WOWPFaGunYIVQ8TPzNqEJRDGF85SLpEiYPTT3cbiIAiaI

For "A fax comes in" select TwiML and then select the TwiML Bin Modern Fax.

Testing out Dropbox faxes

As we already established, it's 2019 and we don't have a fax machine to test this with. Now, you can either head down to your local library or print shop and ask to borrow theirs, or pop open the Twilio API explorer and send yourself a fax via the API (you can use your existing fax number as both the To and From number here). When sending a fax you need to have a PDF hosted somewhere that Twilio can reach it. If you don't have one, feel free to use our test PDF file here.

Don't forget to play the authentic sounds of a fax machine as you send it off into the world.

Wait a couple of minutes (faxes take time!) and then check your Dropbox folder.

mmTCf-XKv4kyX_RW5FafuI_qyD-xIi3ZFym6dVVIje5scqt5AR_HOS78b7lhIsZYBBMZSFyhH9RkHCj_FXz7OgRH6pVyeHYctuho7xDpCj8Z5INK76Vnt36RUrwhCPayYv5UuVs

We got a fax!

And there you have it, a Twilio-linked DropBox fax receiving app in just a few minutes.

We hope this demonstration inspires your next fax-app – or fax-related hack, anyway. We can't wait to see what you build!

Source: Twilio

Leave a Reply

Your email address will not be published.


*