-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
144 lines (118 loc) · 4.4 KB
/
index.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
/**
* Module dependencies
*/
var Resource = require('deployd/lib/resource');
var util = require('util');
/**
* Module setup.
*/
function Twilio() {
Resource.apply( this, arguments );
//require the Twilio module and create a REST client
if (this.config.twilioAccountSid && this.config.twilioAuthToken) {
this.twilio = require('twilio')(this.config.twilioAccountSid, this.config.twilioAuthToken);
}
}
util.inherits( Twilio, Resource );
Twilio.prototype.clientGeneration = true;
Twilio.basicDashboard = {
settings: [
{
name : 'twilioAccountSid',
type : 'text',
description : 'Twilio Account Sid'
}, {
name : 'twilioAuthToken',
type : 'text',
description : 'Twilio Auth Token'
}, {
name : 'defaultFromTel',
type : 'text',
description : 'Default From Tel'
}, {
name : 'rootOrInternalOnly',
type : 'checkbox',
description : 'Only allow root or internal scripts to send email'
}, {
name : 'productionOnly',
type : 'checkbox',
description : 'If on development mode, print emails to console instead of sending them'
}]
};
Twilio.events = ["post"];
Twilio.prototype.handle = function ( ctx, next ) {
if ( ctx.req && ctx.req.method !== 'POST' ) {
return next();
}
if ( this.config.rootOrInternalOnly && (!ctx.req.internal && !ctx.req.isRoot) ) {
return ctx.done({ statusCode: 403, message: 'Forbidden' });
}
var data = ctx.body || {};
// to // Any number Twilio can deliver to
// from, // A number you bought from Twilio and can use for outbound communication
// body // body of the SMS message
var errors = {};
if (!data.to) {
errors.to = '\'to\' is required';
}
if ( !data.body ) {
errors.body = '\'body\' is required';
}
if ( Object.keys(errors).length ) {
return ctx.done({ statusCode: 400, errors: errors });
}
// defaults
if (!data.from) { data.from = this.config.defaultFromTel }
var that = this;
if (that.events.post) {
that.events.post.run(ctx, {data:data}, function(err) {
if (err) return ctx.done(err);
that.sendMessage(ctx, data);
});
} else {
that.sendMessage(ctx, data);
}
/*
//Place a phone call, and respond with TwiML instructions from the given URL
client.makeCall({
to:'+16515556677', // Any number Twilio can call
from: '+14506667788', // A number you bought from Twilio and can use for outbound communication
url: 'http://www.example.com/twiml.php' // A URL that produces an XML document (TwiML) which contains instructions for the call
}, function(err, responseData) {
//executed when the call has been initiated.
console.log(responseData.from); // outputs "+14506667788"
});
*/
};
Twilio.prototype.sendMessage = function(ctx, data) {
var env = this.options.server.options.env;
if (this.config.productionOnly && env != 'production') {
console.log('_______________________________________________');
console.log('Twilio Simulate');
console.log('From:', data.from);
console.log('To:', data.to);
console.log(data.body);
console.log('```````````````````````````````````````````````');
return ctx.done( null, { message : 'Simulated' } );
}
//Send an SMS text message
this.twilio.sendMessage( data, 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
console.error(err);
}
return ctx.done(err, responseData);
// if (!err) { // "err" is an error received during the request, if any
//
// // "responseData" is a JavaScript object containing data received from Twilio.
// // A sample response from sending an SMS message is here (click "JSON" to see how the data appears in JavaScript):
// // http://www.twilio.com/docs/api/rest/sending-sms#example-1
//
// console.log(responseData.from); // outputs "+14506667788"
// console.log(responseData.body); // outputs "word to your mother."
//
// } else {
// console.error(err);
// }
});
}
module.exports = Twilio;