Skip to content

Instantly share code, notes, and snippets.

@jhgaylor
Last active November 19, 2015 06:09
Show Gist options
  • Save jhgaylor/693513c80ac4b6c46beb to your computer and use it in GitHub Desktop.
Save jhgaylor/693513c80ac4b6c46beb to your computer and use it in GitHub Desktop.
An accompaniment for a blog post on blog.JakeGaylor.com where we help Frank's gym.
client.save = function (callback) {
if (client._id) {
collection.updateOne({_id: client._id}, client, callback);
} else {
collection.insertOne(client, callback);
}
}
client.recordResponse = function (response, callback) {
if (client.wasSentSMS.monthThree) {
response.milestone = "monthThree";
} else if (client.wasSentSMS.monthOne) {
response.milestone = "monthOne";
} else {
response.milestone = "weekOne";
}
collection.updateOne({_id: client._id}, {
$addToSet: {
responses: response
}
}, callback);
}
client.sendSMS = function (milestone, callback) {
var personalizedMilestoneMessage = client.name + ", " + milestone.message;
SMS.send(client.phoneNumber, personalizedMilestoneMessage, function (error, responseData) {
if (error) {
console.log("failed to send sms", error, error.stack);
throw error;
}
var fieldString = 'wasSentSMS.' + milestone.name
var operation = {
$set: {}
};
operation.$set[fieldString] = new Date;
collection.updateOne({_id: client._id}, operation);
});
}
{
name: String,
phoneNumber: String,
signupDate: Date,
responses: [Subdocument],
wasSentSMS: {
week1: nullable Date,
month1: nullable Date,
month3: nullable Date
}
}
var Twilio = require('twilio')(twilio_sid, twilio_auth_token);
function send (number, message, cb) {
console.log("SMS: ", number, message);
Twilio.sendMessage({
to: number,
from: twilio_number,
body: message
}, cb);
}
module.exports = {
client: Twilio,
send: send
}
var MongoClient = require('mongodb').MongoClient;
var defaultMongoDBUrl = 'mongodb://localhost:27017/franks_gym';
var DataBaseURL = process.env.MONGO_URL || defaultMongoDBUrl;
function getDBConnection (callback) {
MongoClient.connect(DataBaseURL, function (err, db) {
if (err) {
throw err;
}
console.log("Connected correctly to server");
callback(db);
});
}
module.exports = {
getDB: getDBConnection
};
var Database = require('./db');
var Client = require('./models').Client;
var clientsData = [{
name: "joe",
phoneNumber: "+15555555555",
signupDate: new Date("03/22/2015"),
wasSentSMS: {
weekOne: false,
monthOne: false,
monthThree: false
},
responses: []
}, {
name: "sue",
phoneNumber: "+15555555555",
signupDate: new Date("10/09/2015"),
wasSentSMS: {
weekOne: false,
monthOne: false,
monthThree: false
},
responses: []
}
];
Database.getDB(function (db) {
var completeCount = 0;
var clients = clientsData.map(function (clientData) {
return Client(db, clientData);
}).forEach(function (client) {
client.save(function () {
completeCount += 1;
if (completeCount === clientsData.length) {
console.log("All fixtures have finished. Goodbye.");
db.close();
}
});
});
});
function getPendingResponses (db) {
Models.getAllClients(db, function (clients) {
var clientPhoneNumbers = clients.map(function (client) {
return client.phoneNumber;
});
SMS.client.messages.list(function (err, data) {
data.messages.forEach(function (message) {
var messageIsFromClient = (clientPhoneNumbers.indexOf(message.from) > -1);
if (messageIsFromClient) {
handleIncomingClientMessage(db, message);
}
});
});
});
}
var dayInMilliseconds = 60*60*24*1000;
var intervalHandle;
start();
function start () {
Database.getDB(function (db) {
function doWork () {
getAndSMSClients(db);
getPendingResponses(db);
}
doWork();
intervalHandle = setInterval(doWork, dayInMilliseconds);
});
}
function restart (error) {
console.log("Error: ", error.stack);
clearInterval(intervalHandle);
start();
}
{
"name": "frank",
"version": "0.0.1",
"description": "A tool for Frank to communicate with his gym's clients.",
...
"homepage": "https://github.com/jhgaylor/frank",
"dependencies": {
"mongodb": "^2.0.48",
"twilio": "^2.5.2"
}
}
{
"name": "frank",
"version": "0.0.1",
"description": "A tool for Frank to communicate with his gym's clients.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/jhgaylor/frank.git"
},
"keywords": [
"twilio"
],
"author": "Jake Gaylor <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/jhgaylor/frank/issues"
},
"homepage": "https://github.com/jhgaylor/frank"
}
var twilio_sid = process.env.TWILIO_SID;
var twilio_auth_token = process.env.TWILIO_AUTH_TOKEN;
var twilio_number = process.env.TWILIO_NUMBER;
function handleIncomingClientMessage (db, message) {
Models.getClientByNumber(db, message.from, function (client) {
if (! client) {
console.log("No client found for this number. Weird.");
return;
}
var responseIds = client.responses.map(function (response) {
return response.sid;
});
if (responseIds.indexOf(message.sid) === -1) {
client.recordResponse(message);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment