-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtexter.js
64 lines (54 loc) · 1.6 KB
/
texter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Initialize a REST client for Twilio in a single line:
var client = require('twilio')();
var schedule = require('node-schedule');
// reqire pg that interacts with knex
var pg = require('pg');
var knex = require('knex')({
client: 'pg',
connection: process.env.DATABASE_URL
});
var texter = {};
// set RecurrenceRule for
var rule = new schedule.RecurrenceRule();
rule.hour = 16;
rule.minute = 01;
texter.sendText = function(num, text){
var number = '';
if (num.length === 10){
number = '+1' + num;
} else if (num.length === 11){
number = '+' + num;
} else {
number = num;
}
// Use this convenient shorthand to send an SMS:
client.sendSms({
to: number,
from:'+17816674950',
body: text
}, function(error, message) {
if (!error) {
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
} else {
console.log('Oops! There was an error.');
}
});
};
texter.scheduleTexts = function () {
schedule.scheduleJob(rule, function () {
knex.select('*').from('users').leftJoin('goodhabits', 'users.id', 'goodhabits.userid').then(function(success){
for (var i = 0; i < success.length; i++) {
if (success[i].id !== null){
texter.sendText(success[i].phone, 'Have you logged about ' + success[i].habitname + ' today?');
}
}
});
});
};
texter.confirmText = function (phone, name) {
texter.sendText(phone, 'Hi ' + name + ', welcome to Habbit Rabbit!');
};
module.exports = texter;