Skip to content

Instantly share code, notes, and snippets.

@adamgibbons
Created July 29, 2016 22:16
Show Gist options
  • Save adamgibbons/bc44ad8a99472d7324e6882e49d540c8 to your computer and use it in GitHub Desktop.
Save adamgibbons/bc44ad8a99472d7324e6882e49d540c8 to your computer and use it in GitHub Desktop.
get form submissions from slack _leads channel
var _ = require('lodash');
var fs = require('fs');
var readline = require('readline');
fs.readdir('./data', function(err, files) {
if (err) throw err;
files.map(function(file) {
fs.readFile('./data/' + file, {encoding: 'utf-8'}, function (err, messages) {
if (err) throw err;
return JSON.parse(messages).filter(function (message) {
return message.file && message.file.name && message.file.name.indexOf('[Form Submitted]') !== -1;
})
.map(function (message) {
return message.file.plain_text.split('\r\n');
})
.map(function (arr) {
var obj = {};
arr.reduce(function (prev, curr, idx, arr) {
if (curr.toLowerCase().indexOf('origin') !== -1) {
obj.origin = arr[idx + 1];
}
if (curr.toLowerCase().indexOf('destination') !== -1) {
obj.destination = arr[idx + 1];
}
if (curr.toLowerCase().indexOf('name') !== -1) {
obj.name = arr[idx + 1];
}
if (curr.toLowerCase().indexOf('shipperemail') !== -1 || curr.toLowerCase().indexOf('shipper_email') !== -1) {
obj.email = arr[idx + 1];
}
if (curr.toLowerCase().indexOf('phone') !== -1) {
obj.phone = arr[idx + 1];
}
if (curr.toLowerCase().indexOf('weight') !== -1) {
obj.weight = arr[idx + 1];
}
if (curr.toLowerCase().indexOf('moveid') !== -1 || curr.toLowerCase().indexOf('move_id') !== -1) {
obj.moveId = arr[idx + 1];
}
}, obj);
console.log(obj);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment