-
Notifications
You must be signed in to change notification settings - Fork 87
/
sms_server.js
92 lines (80 loc) · 2.9 KB
/
sms_server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var Future = Npm.require('fibers/future');
var Twilio = Npm.require('twilio');
SMS = {};
SMSTest = {};
var next_devmode_sms_id = 0;
var output_stream = process.stdout;
// Testing hooks
SMSTest.overrideOutputStream = function (stream) {
next_devmode_sms_id = 0;
output_stream = stream;
};
SMSTest.restoreOutputStream = function () {
output_stream = process.stdout;
};
var devModeSend = function (options) {
var devmode_sms_id = next_devmode_sms_id++;
var stream = output_stream;
// This approach does not prevent other writers to stdout from interleaving.
stream.write("====== BEGIN SMS #" + devmode_sms_id + " ======\n");
stream.write("(SMS not sent; to enable sending, set the TWILIO_CREDENTIALS " +
"environment variable.)\n");
var future = new Future;
stream.write("From:" + options.from + "\n");
stream.write("To:" + options.to + "\n");
stream.write("Text:" + options.body + "\n");
stream.write("====== END SMS #" + devmode_sms_id + " ======\n");
future['return']();
};
/**
* Mock out sms sending (eg, during a test.) This is private for now.
*
* f receives the arguments to SMS.send and should return true to go
* ahead and send the email (or at least, try subsequent hooks), or
* false to skip sending.
*/
var sendHooks = [];
SMSTest.hookSend = function (f) {
sendHooks.push(f);
};
/**
* Send an sms.
*
* Connects to twilio via the CONFIG_VARS environment
* variable. If unset, prints formatted message to stdout. The "from" option
* is required, and at least one of "to", "from", and "body" must be provided;
* all other options are optional.
*
* @param options
* @param options.from {String} - The sending SMS number
* @param options.to {String} - The receiver SMS number
* @param options.body {String} - The content of the SMS
*/
SMS.send = function (options) {
for (var i = 0; i < sendHooks.length; i++)
if (!sendHooks[i](options))
return;
if (SMS.twilio) {
var client = Twilio(SMS.twilio.ACCOUNT_SID, SMS.twilio.AUTH_TOKEN);
// Include FROM in options if it is defined.
SMS.twilio.FROM && (options.from = SMS.twilio.FROM);
// Send SMS API async func
var sendSMSSync = Meteor.wrapAsync(client.sendMessage, client);
// call the sync version of our API func with the parameters from the method call
var result = sendSMSSync(options, function (err, responseData) { //this function is executed when a response is received from Twilio
if (err) { // "err" is an error received during the request, if any
throw new Meteor.Error("Error sending SMS ", err.message);
}
return responseData;
});
return result;
} else {
devModeSend(options);
}
};
SMS.phoneTemplates = {
from: '+972545999999',
text: function (user, code) {
return 'Welcome your invitation code is: ' + code;
}
};