Skip to content

Instantly share code, notes, and snippets.

@hcgatewood
Last active February 25, 2025 08:25
Show Gist options
  • Save hcgatewood/1827bfb26a3d0738742aef9953afeb4c to your computer and use it in GitHub Desktop.
Save hcgatewood/1827bfb26a3d0738742aef9953afeb4c to your computer and use it in GitHub Desktop.

Revisions

  1. hcgatewood revised this gist Feb 25, 2025. No changes.
  2. hcgatewood renamed this gist Jul 25, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. hcgatewood revised this gist Dec 23, 2019. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions twilio-dropbox-function.js
    Original file line number Diff line number Diff line change
    @@ -93,6 +93,8 @@ function dateFilename() {
    dt.getDate().toString().padStart(2, '0')}_${
    dt.getHours().toString().padStart(2, '0')}-${
    dt.getMinutes().toString().padStart(2, '0')}-${
    dt.getSeconds().toString().padStart(2, '0')}`;
    dt.getSeconds().toString().padStart(2, '0')}_${
    dt.getTime().toString()
    }`;
    return filename;
    }
    }
  4. hcgatewood created this gist Dec 23, 2019.
    98 changes: 98 additions & 0 deletions twilio-dropbox-function.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    const isoFetch = require('isomorphic-fetch');
    const Dropbox = require('dropbox').Dropbox;
    const util = require('util');

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

    const whitelisted_numbers = [
    context.HCG_NUM_1,
    context.HCG_NUM_2,
    ];

    if (!whitelisted_numbers.includes(event.From)) {
    callback(`Received text from non-whitelisted ${event.From}`);
    }

    const twilioClient = context.getTwilioClient();
    const dbx = new Dropbox({
    accessToken: context.DROPBOX_ACCESS_TOKEN,
    fetch: isoFetch,
    });
    const todoFilename = `todo_${dateFilename()}.txt`;
    const todoFilepath = `/${todoFilename}`;

    dbx.filesUpload({
    path: todoFilepath,
    contents: event.Body,
    mute: true,
    // mode: 'overwrite',
    // Upload success
    }).then(function(response) {
    console.log('dropbox upload response...');
    console.log(response);
    dbx.sharingCreateSharedLink({
    path: todoFilepath,
    // Share success
    }).then(function(response) {
    console.log('dropbox link response...');
    log(response);
    twilioClient.messages.create({
    from: event.To,
    to: event.From,
    body: `${response.url}`,
    }, twilio_err(null, callback));
    // Share fail
    }).catch(function(err) {
    console.log('dropbox link err...');
    log(err);
    twilioClient.messages.create({
    from: event.To,
    to: event.From,
    body: `❌ Dropbox link fail.\n${fmtObject(err.error)}`,
    }, twilio_err(err, callback));
    })
    // Upload fail
    }).catch(function(err) {
    console.log('dropbox upload err...');
    log(err);
    twilioClient.messages.create({
    from: event.To,
    to: event.From,
    body: `❌ Dropbox upload fail.\n${fmtObject(err.error)}`,
    }, twilio_err(err, callback));
    });
    };

    function twilio_err(err, callback) {
    return function(err_twilio, result) {
    if (err_twilio === null) {
    callback(err);
    } else {
    console.log('twilio err...');
    log(err_twilio);
    callback(err_twilio);
    }
    }
    }

    function fmtObject(obj) {
    return util.inspect(obj, {showHidden: false, depth: null});
    }

    function log(obj) {
    console.log(fmtObject(obj));
    }

    function dateFilename() {
    var dt = new Date();
    filename = `${
    dt.getFullYear().toString().padStart(4, '0')}-${
    (dt.getMonth()+1).toString().padStart(2, '0')}-${
    dt.getDate().toString().padStart(2, '0')}_${
    dt.getHours().toString().padStart(2, '0')}-${
    dt.getMinutes().toString().padStart(2, '0')}-${
    dt.getSeconds().toString().padStart(2, '0')}`;
    return filename;
    }