textbelt/app.js

158 lines
3.4 KiB
JavaScript
Raw Normal View History

2012-04-05 05:21:56 +00:00
var express = require('express')
, app = express.createServer()
, nodemailer = require('nodemailer')
2012-04-07 02:01:24 +00:00
/*
, amazonses = require('amazon-ses')
*/
, exec = require('child_process').exec
2012-04-06 22:33:12 +00:00
, config = require('./config.js')
2012-04-06 21:52:10 +00:00
2012-04-07 02:01:24 +00:00
/*
var SendGrid = require('sendgrid').SendGrid;
var sendgrid = new SendGrid(
process.env.SENDGRID_USERNAME || 'app3740036@heroku.com',
process.env.SENDGRID_PASSWORD || 'd0y4yjqn'
)
*/
2012-04-06 21:52:10 +00:00
var redis;
if (process.env.NODE_ENV == 'production')
redis = require('redis-url').connect(process.env.REDISTOGO_URL);
else
redis = require('redis-url').connect();
2012-04-05 05:21:56 +00:00
// Express config
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
// App
/* Homepage */
app.get('/', function(req, res) {
2012-04-06 21:52:45 +00:00
res.send("it's running");
2012-04-05 05:21:56 +00:00
});
2012-04-06 19:44:51 +00:00
app.post('/text', function(req, res) {
2012-04-06 21:22:23 +00:00
var keystr = req.connection.remoteAddress + '_' + dateStr();
2012-04-06 21:52:10 +00:00
redis.incr(keystr, function(err, num) {
2012-04-06 21:22:23 +00:00
if (err) {
2012-04-06 22:42:58 +00:00
res.send({success:false,message:'Could not validate IP quota.'});
2012-04-06 21:22:23 +00:00
return;
}
2012-04-06 21:52:10 +00:00
2012-04-06 21:22:23 +00:00
if (num < 51) {
2012-04-06 22:42:58 +00:00
sendText(req.body.number, req.body.message, function(err) {
2012-04-06 21:22:23 +00:00
if (err)
2012-04-06 22:42:58 +00:00
res.send({success:false,message:'Communication with SMS gateway failed.'});
2012-04-06 21:22:23 +00:00
else
res.send({success:true});
});
}
else {
2012-04-06 22:42:58 +00:00
res.send({success:false,message:'Exceeded quota.'});
2012-04-06 21:22:23 +00:00
}
});
2012-04-06 19:44:51 +00:00
});
2012-04-06 21:22:23 +00:00
function dateStr() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
return mm + '/' + dd + '/' + yyyy;
}
2012-04-07 02:01:24 +00:00
function validatePhone() {
}
2012-04-06 22:49:50 +00:00
2012-04-06 22:42:58 +00:00
function sendText(phone, message, cb) {
2012-04-07 02:01:24 +00:00
var actual_phone = phone.replace(/\D/g, '');
//var child = process.createChildProcess('sendmail', ['-f"txt@textbelt.com"', '"9147727429@vtext.com"']);
/*
sendgrid.send({
to: '9147727429@vtext.com',
from: 'txt@textbelt.com',
subject: 'a',
text: 'Sending email with NodeJS through SendGrid!'
}, function(success, data) {
if (!success) {
console.log(data);
}
else {
console.log('message sent');
}
cb(!success);
});
*/
/*
var ses = new amazonses(config.aws.access, config.aws.secret);
ses.send({
from: 'txt@textbelt.com'
, to: ['typppo@gmail.com']
, replyTo: ['txt@textbelt.com']
, subject: ''
, body: {
text: ' Test mesg'
}
}, function(err, data) {
if (err) {
console.log(data);
}
else {
console.log('message sent');
}
cb(err);
});
*/
/*
2012-04-05 07:30:47 +00:00
var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: config.aws.access,
2012-04-06 19:44:51 +00:00
AWSSecretKey: config.aws.secret,
2012-04-07 02:01:24 +00:00
ReturnPath: 'txt@textbelt.com',
2012-04-05 07:30:47 +00:00
});
2012-04-07 02:01:24 +00:00
*/
/*
var transport = nodemailer.createTransport("Sendmail");
2012-04-05 05:21:56 +00:00
2012-04-05 07:30:47 +00:00
var mailOptions = {
transport: transport, // transport method to use
2012-04-06 19:44:51 +00:00
from: "txt@textbelt.com", // sender address
2012-04-06 22:49:50 +00:00
to: 'typppo@gmail.com',
2012-04-05 07:30:47 +00:00
subject: '', // Subject line
2012-04-06 22:42:58 +00:00
text: message,
2012-04-07 02:01:24 +00:00
ReturnPath: 'txt@textbelt.com',
2012-04-05 05:21:56 +00:00
}
2012-04-07 02:01:24 +00:00
*/
2012-04-05 07:30:47 +00:00
nodemailer.sendMail(mailOptions, function(error){
if (error) {
console.log(error);
2012-04-06 19:44:51 +00:00
cb(true);
2012-04-05 07:30:47 +00:00
}
else {
console.log("Message sent!");
2012-04-06 19:44:51 +00:00
cb(false);
2012-04-05 07:30:47 +00:00
}
transport.close(function(){}); // shut down the connection pool
});
2012-04-06 19:44:51 +00:00
}
2012-04-05 05:21:56 +00:00
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log('Listening on', port);
});