Separate text sending into it's own module.
This commit is contained in:
		
							
								
								
									
										77
									
								
								lib/text.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								lib/text.js
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
};
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user