From 210b0aef275e183f5d793626157c6c315942f05e Mon Sep 17 00:00:00 2001 From: Taylor Russ Date: Sun, 5 Oct 2014 20:24:12 -0700 Subject: [PATCH 1/5] Refactor server and lib into separate folders. --- Procfile | 2 +- providers.js => lib/providers.js | 0 scripts/start.sh | 2 +- app.js => server/app.js | 2 +- banned_numbers_example.js => server/banned_numbers_example.js | 0 keys_example.json => server/keys_example.json | 0 mixpanel_config_example.js => server/mixpanel_config_example.js | 0 {views => server/views}/index.html | 0 8 files changed, 3 insertions(+), 3 deletions(-) rename providers.js => lib/providers.js (100%) rename app.js => server/app.js (99%) rename banned_numbers_example.js => server/banned_numbers_example.js (100%) rename keys_example.json => server/keys_example.json (100%) rename mixpanel_config_example.js => server/mixpanel_config_example.js (100%) rename {views => server/views}/index.html (100%) diff --git a/Procfile b/Procfile index e1d4131..528737e 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web: node app.js +web: node server/app.js diff --git a/providers.js b/lib/providers.js similarity index 100% rename from providers.js rename to lib/providers.js diff --git a/scripts/start.sh b/scripts/start.sh index c332e85..46fe802 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,3 +1,3 @@ #!/bin/sh -nohup hotnode app.js & +nohup hotnode server/app.js & diff --git a/app.js b/server/app.js similarity index 99% rename from app.js rename to server/app.js index 1867843..f31e660 100644 --- a/app.js +++ b/server/app.js @@ -6,7 +6,7 @@ var express = require('express') , exec = require('child_process').exec , spawn = require('child_process').spawn , Stream = require('stream') - , providers = require('./providers.js') + , providers = require('../lib/providers.js') , redis = require('redis-url').connect() // Optional modules diff --git a/banned_numbers_example.js b/server/banned_numbers_example.js similarity index 100% rename from banned_numbers_example.js rename to server/banned_numbers_example.js diff --git a/keys_example.json b/server/keys_example.json similarity index 100% rename from keys_example.json rename to server/keys_example.json diff --git a/mixpanel_config_example.js b/server/mixpanel_config_example.js similarity index 100% rename from mixpanel_config_example.js rename to server/mixpanel_config_example.js diff --git a/views/index.html b/server/views/index.html similarity index 100% rename from views/index.html rename to server/views/index.html From cfc629399a10528932bac8ded22d6ae7e78f587c Mon Sep 17 00:00:00 2001 From: Taylor Russ Date: Sun, 5 Oct 2014 20:30:56 -0700 Subject: [PATCH 2/5] Fix a missing variable declaration. --- server/app.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/app.js b/server/app.js index f31e660..37cb0a5 100644 --- a/server/app.js +++ b/server/app.js @@ -12,14 +12,15 @@ var express = require('express') // Optional modules var banned_numbers; try { - banned_numbers = require('./banned_numbers.js') + banned_numbers = require('./banned_numbers.js'); } catch(e) { banned_numbers = {BLACKLIST: {}}; } -var mpq; +var mpq + , mixpanel_config; try { - mixpanel_config = require('./mixpanel_config.js') + mixpanel_config = require('./mixpanel_config.js'); mpq = new mixpanel.Client(mixpanel_config.api_key); } catch(e) { mpq = {track: function() {}}; From ce1ded590cdf6442ea0c579e757e33f977f5fd05 Mon Sep 17 00:00:00 2001 From: Taylor Russ Date: Sun, 5 Oct 2014 22:37:34 -0700 Subject: [PATCH 3/5] Separate text sending into it's own module. --- lib/text.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ server/app.js | 35 ++++------------------- 2 files changed, 82 insertions(+), 30 deletions(-) create mode 100644 lib/text.js diff --git a/lib/text.js b/lib/text.js new file mode 100644 index 0000000..869b8be --- /dev/null +++ b/lib/text.js @@ -0,0 +1,77 @@ +var providers = require('./providers.js') + , _ = require('underscore') + , exec = require('child_process').exec + , spawn = require('child_process').spawn; + +var debugEnabled = false; + +//---------------------------------------------------------------- +/* + General purpose logging function, gated by a configurable + value. +*/ +function output() { + if (debugEnabled === true) { + return console.log.apply(this, arguments); + } +} + +//---------------------------------------------------------------- +/* Enable verbosity for the text module. + + If enabled, logging functions will + print to stdout. + + Params: + enable - bool +*/ +function debug(enable) { + debugEnabled = enable; + return debugEnabled; +} + +//---------------------------------------------------------------- +/* Sends a text message + + Will perform a region lookup (for providers), then + send a message to each. + + Params: + phone - phone number to text + message - message to send + region - region to use (defaults to US) + cb - function(err), provides err messages +*/ +function sendText(phone, message, region, cb) { + output('txting phone', phone, ':', message); + + region = region || 'us'; + + var providers_list = providers[region]; + + var done = _.after(providers_list.length, function() { + cb(false); + }); + + _.each(providers_list, function(provider) { + var email = provider.replace('%s', phone); + email = 'Subject: Text\r\n\r\n' + email; + var child = spawn('sendmail', ['-f', 'txt2@textbelt.com', email]); + child.stdout.on('data', output); + child.stderr.on('data', output); + child.on('error', function(data) { + output('sendmail failed', {email: email, data: data}); + done(); + }); + child.on('exit', function(code, signal) { + done(); + }); + child.stdin.write(message + '\n.'); + child.stdin.end(); + }); +} + +module.exports = { + send: sendText, // Send a text message + debug: debug // Enable or disable debug output +}; diff --git a/server/app.js b/server/app.js index 37cb0a5..16a2fff 100644 --- a/server/app.js +++ b/server/app.js @@ -6,8 +6,11 @@ var express = require('express') , exec = require('child_process').exec , spawn = require('child_process').spawn , Stream = require('stream') - , providers = require('../lib/providers.js') , redis = require('redis-url').connect() + , text = require('../lib/text'); + +// Enable log messages when sending texts. +text.debug(true); // Optional modules var banned_numbers; @@ -105,7 +108,7 @@ function textRequestHandler(req, res, number, region, key) { response_obj = response_obj || {}; // Time to actually send the message - sendText(number, message, region, function(err) { + text.send(number, message, region, function(err) { if (err) { mpq.track('sendText failed', tracking_details); res.send(_.extend(response_obj, @@ -196,34 +199,6 @@ function stripPhone(phone) { return (phone+'').replace(/\D/g, ''); } -function sendText(phone, message, region, cb) { - console.log('txting phone', phone, ':', message); - - region = region || 'us'; - - var providers_list = providers[region]; - - var done = _.after(providers_list.length, function() { - cb(false); - }); - - _.each(providers_list, function(provider) { - var email = provider.replace('%s', phone); - email = 'Subject: Text\r\n\r\n' + email; - var child = spawn('sendmail', ['-f', 'txt2@textbelt.com', email]); - child.stdout.on('data', console.log); - child.stderr.on('data', console.log); - child.on('error', function(data) { - mpq.track('sendmail failed', {email: email, data: data}); - done(); - }); - child.on('exit', function(code, signal) { - done(); - }); - child.stdin.write(message + '\n.'); - child.stdin.end(); - }); -} // Start server var port = process.env.PORT || 9090; From a1940867085d6d240f52a5718a48a7294f70b34e Mon Sep 17 00:00:00 2001 From: Taylor Russ Date: Sun, 5 Oct 2014 22:48:40 -0700 Subject: [PATCH 4/5] Add index.js to enable usage as module. --- index.js | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..9ae6fe6 --- /dev/null +++ b/index.js @@ -0,0 +1,4 @@ + +var textbelt = require('./lib/text'); + +module.exports = textbelt; From b33d31a1e2023dd46c1ef4ed5839a8c4c970b84a Mon Sep 17 00:00:00 2001 From: Taylor Russ Date: Sun, 5 Oct 2014 22:55:58 -0700 Subject: [PATCH 5/5] Update README with module use info. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index d3ed7a7..0443f9e 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,38 @@ Sample failure: ``` {"success":false,"message":"Exceeded quota for this phone number."} +``` +### Usage as a module + +Though this repository contains an express server so you may run your own +instance of the web app, you may also use it to send text messages in your +project. The only requirement is unix/posix `sendmail`, used to forward the message. + +For example, to send a text using the default settings: +``` +var text = require('textbelt'); + +text.send('9491234567', 'A sample text message!', undefined, function(err) { + if (err) { + console.log(err); + } +}); +``` + +You can also supply a region (valid choices are `us`, `intl`, or `canada`) +``` +var text = require('textbelt'); + +// Canada +text.send('9491234567', 'A sample text message!', 'canada', function(err) { +... +}); + +// International +text.send('1119491234567', 'Bonjour!', 'intl', function(err) { +... +}); + ``` ### Canadian and International endpoints