Skip to content

Commit

Permalink
Merge pull request #4 from dtolb/cat-facts
Browse files Browse the repository at this point in the history
Cat facts
  • Loading branch information
Daniel Tolbert authored Mar 27, 2017
2 parents 9c31fa1 + f00754e commit 1517e83
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ typings/

# dotenv environment variables file
.env

play.js

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Anything else is ignored and your chat continues as normal
* `BANDWIDTH_API_SECRET`
* `DARKSKY_KEY`
* `GOOGLE_KEY`
* `DATABASE_URL`

### Run
`npm install`
Expand Down
Binary file added catFact.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions catfact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const debug = require('debug')('giphy_sms');
const catFacts = require('cat-facts');

module.exports.handleCatFactCommand = function (message) {
const catFact = catFacts.random();
return Promise.resolve({
text: catFact,
to: message.numbers.to,
from: message.numbers.from
});
};
4 changes: 3 additions & 1 deletion commands.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const gif = require('./gif.js');
const weather = require('./weather.js');
const bandi = require('./bandi.js');
const catfact = require('./catfact.js');
const Promise = require('bluebird');

const noCommand = function (message) {
Expand All @@ -24,5 +25,6 @@ module.exports = {
weather: weather.handleWeatherCommand,
error: commandError,
default: noCommand,
bandi: bandi.handleBandiCommand
bandi: bandi.handleBandiCommand,
catfact: catfact.handleCatFactCommand
}
23 changes: 14 additions & 9 deletions controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports.checkIfBodyIsArray = function (req, res, next) {

module.exports.handleMessages = function (req, res, next) {
req.outMessages = [];
message = req.body[0];
let message = req.body[0];
debug('Handling message');
if (messageReadyForProcessing(message)) {
message.numbers = buildToArray(message);
Expand Down Expand Up @@ -95,14 +95,19 @@ module.exports.handleMessages = function (req, res, next) {
};

module.exports.sendMessages = function (req, res, next) {
bandwidth.Message.sendGroup(req.outMessages[0])
.then(function (body) {
debug(body);
})
.catch(function (err) {
debug('Error sending message');
next(err);
});
if(req.outMessages[0].to.length >= 1) {
bandwidth.Message.sendGroup(req.outMessages[0])
.then(function (body) {
debug(body);
})
.catch(function (err) {
debug('Error sending message');
next(err);
});
}
else {
debug('No valid phone numbers to send to');
}
};

module.exports.sendAccepted = function (req, res, next) {
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ debug('booting %s', name);
const express = require('express');
let app = express();


function startServer() {
debug('Starting Server');
app.use(bodyParser.json());
Expand All @@ -19,7 +18,7 @@ function startServer() {
debug(req.url)
var err = new Error('not found');
err.status = 404;
res.send(404, 'Not Found')
res.sendStatus(404, 'Not Found')
});

// production error handler, no stacktraces leaked to user
Expand Down
97 changes: 97 additions & 0 deletions optOut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const debug = require('debug')('giphy_sms');
const mongoose = require('mongoose');
const findOrCreate = require('findorcreate-promise');
const Promise = require('bluebird');
const _ = require('underscore');

mongoose.Promise = global.Promise;
mongoose.connect(process.env.DATABASE_URL || process.env.MONGODB_URI || 'mongodb://localhost/giphy-sms');

const NumberSchema = new mongoose.Schema({
number: { type: String, required: true},
disable: {type: Boolean, default: true}
});

NumberSchema.plugin(findOrCreate);

const Number = mongoose.model('Number', NumberSchema);

const phrases = [
'stop',
'unsubscribe'
];

const addNumberToOptOut = function (number) {
debug('Adding number to optout list: ' + number);
return Number.findOrCreate({
number: number
});
}

const searchForNumber = function (number) {
return Number.find({ number: number})
.then(function (res) {

})
}

module.exports.removeOptOutsFromMessage = function (req, res, next) {
const outNumbers = req.outMessages[0].to;
debug('Searching for optouts: ' + outNumbers);
return Promise.map(outNumbers, function (number) {
return Number.find({ number: number })
.then(function (doc) {
if (doc.length > 0) {
debug('Found number in optout: ' + doc[0].number);
return doc[0].number;
}
else {
debug('Number not in opt out list: ' + number);
}
})
})
.then(function (results) {
debug('Numbers that opted out: ' + results);
req.outMessages[0].to = _.difference(outNumbers,results);
next();
})
.catch(function (err) {
debug('Error removing components from to array');
next(err);
})
};

module.exports.checkForOptOut = function (req, res, next) {
let message = req.body[0];
let number = message.message.from;
let text = ''
try {
text = message.message.text.toLowerCase();
}
catch (e) {
debug('No text in message')
next();
return;
}
debug('Incoming text: ' + text);
if (phrases.indexOf(text) >= 0) {
debug('Stop Command Found');
addNumberToOptOut(number)
.then(function (doc) {
if (doc.created) {
debug('Added number to block list: ' + number);
}
else {
debug('Number already in block list: ' + number);
}
next();
})
.catch(function (err) {
debug('Error finding or creating number to block list: ' + number);
})
}
else {
debug('Not a stop command');
next();
}
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "'echo no test'",
"start":"node index.js"
"start": "node index.js"
},
"keywords": [
"bandwidth",
Expand All @@ -24,10 +24,13 @@
"@google/maps": "^0.3.1",
"bluebird": "^3.5.0",
"body-parser": "^1.17.1",
"cat-facts": "^1.0.2",
"dark-sky": "^1.0.10",
"debug": "^2.6.1",
"express": "^4.15.2",
"giphy-api": "^1.2.5",
"node-bandwidth": "dtolb/node-bandwidth#v2_messaging"
"mongoose": "^4.9.1",
"node-bandwidth": "dtolb/node-bandwidth#v2_messaging",
"underscore": "^1.8.3"
}
}
3 changes: 3 additions & 0 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const express = require('express');
let router = module.exports = express.Router();
const controllers = require('./controllers.js');
const optOut = require('./optOut.js');

router.route('/message')
.post(
controllers.sendAccepted,
controllers.checkIfBodyIsArray,
optOut.checkForOptOut,
controllers.handleMessages,
optOut.removeOptOutsFromMessage,
controllers.sendMessages
);

Expand Down

0 comments on commit 1517e83

Please sign in to comment.