This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtwilio.js
154 lines (128 loc) · 3.72 KB
/
twilio.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var twilio = Npm.require('twilio');
var lookups = new Mongo.Collection('twilio.lookups');
Twilio = function (options) {
var self = this;
options = _.extend({
// default
}, options);
check(options, {
from: String,
sid: String,
token: String
});
self.client = new twilio(options.sid, options.token);
self.from = options.from;
self.auth = options.sid + ':' + options.token;
};
/*
* Send an sms to a phone number with a message.
* @param options
* {String} options.to
* {String} options.body
* {String} [options.from] Specify a different number to send the sms from.
* @param {Function} [callback]
*/
Twilio.prototype.sendSMS = function (options, callback) {
var self = this;
options = _.extend({
from: self.from
}, options);
check(options, {
to: String,
from: String,
body: String,
statusCallback: Match.Optional(String)
});
return Meteor.wrapAsync(self.client.sendMessage).call(self.client, options, callback);
};
/*
* Send an mms to a phone number with a message and media.
* @param options
* {String} options.to
* {String} options.body
* {String} options.mediaUrl
* {String} [options.from] Specify a different number to send the sms from.
* {String} [options.statusCallback] Specify a callback URL for Twilio to hit for status updates
* @param {Function} [callback]
*/
Twilio.prototype.sendMMS = function (options, callback) {
var self = this;
options = _.extend({
from: self.from
}, options);
check(options, {
to: String,
from: String,
body: String,
mediaUrl: Match.Optional(String),
statusCallback: Match.Optional(String)
});
return Meteor.wrapAsync(self.client.messages.post).call(self.client, options, callback);
};
/*
* Call a phone number.
* @param options
* {String} options.to
* {String} [options.from] Specify a different number to call from.
* {String} [options.statusCallback] Specify a callback URL for Twilio to hit for status updates
* @param {Function} [callback]
*/
Twilio.prototype.makeCall = function (options, callback) {
var self = this;
options = _.extend({
from: self.from
}, options);
check(options, {
to: String,
from: String,
statusCallback: Match.Optional(String)
});
return Meteor.wrapAsync(self.client.makeCall).call(self.client, options, callback);
};
/*
* Asynchronously look up information about a phone number.
* @param {String} phoneNumber
* @param options
* {String} [options.Type] Get more information about the number. Ex. Type: 'carrier'
* {String} [options.country_code]
* {String} [options.statusCallback] Specify a callback URL for Twilio to hit for status updates
* @param {Function} [callback]
*/
Twilio.prototype.lookupAsync = function (phoneNumber, options, callback) {
var self = this;
check(phoneNumber, String);
if (_.isFunction(options)) {
callback = options;
options = {};
}
var info = lookups.findOne({
phoneNumber: phoneNumber
});
if (info) {
callback(null, info);
} else {
HTTP.get('https://lookups.twilio.com/v1/PhoneNumbers/' + phoneNumber, {
auth: self.auth,
params: options
}, function (error, response) {
if (error) {
callback(error);
} else {
lookups.insert(response.data);
callback(null, response.data);
}
});
}
};
/*
* Synchronously look up information about a phone number.
* @param {String} phoneNumber
* @param options
* {String} [options.Type] Get more information about the number. Ex. Type: 'carrier'
* {String} [options.country_code]
* @param {Function} [callback]
*/
Twilio.prototype.lookup = function (phoneNumber, options, callback) {
var self = this;
return Meteor.wrapAsync(self.lookupAsync).call(self, phoneNumber, options, callback);
};