-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexchange.js
317 lines (286 loc) · 7 KB
/
exchange.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Generated by CoffeeScript 1.8.0
(function() {
var app, encounters, express, records, server;
express = require("express");
var bodyParser = require('body-parser');
var exchangeAPI =require ("./lib/api");
var dao =require ("./lib/dao");
var smsKeyWord=exchangeAPI.getSMSKeyWord();
function errorHandler(err, req, res, next) {
if (res.headersSent) {
return next(err);
}
res.status(500);
res.render('error', { error: err });
}
app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//app.use(express.static(entityAPI.resolvePathDirectory(__dirname,'public')));
app.use(errorHandler);server = app.listen(process.env.PORT || 8084, function() {
return console.log("SMS2Sparrow exchange is running on port:" + (server.address().port));
});
function sendSMS(from,text,to,id,res)
{
var escapedText="";
var cleanedText="";
//escapedText=toUTF8(text);
if(exchangeAPI.getSupportSpecialCharset()==true)
{
escapedText=encodeURIComponent(text);
cleanedText=escapedText.replace(/\%2B/g, '+');
}
else
{
cleanedText=text;
}
console.log("########: "+cleanedText+" ######");
exchangeAPI.SendSMS(from,cleanedText,to,id,function(resAPI)
{
//console.log("Send report: "+resAPI);
//res.json(resAPI);
//console.log(resAPI[0].status);
//return res.end();
if(resAPI[0].response_code==200)
{
console.log("Messages sent");
res.json('{"response":"Messages sent"}');
//Log the sms in the db
dao.saveSMS(id,from,to,function(resBD)
{
if(resBD==true)
{
console.log("Success: SMS logged in the DB");
}
else
{
console.log("Fail: SMS not logged in the DB");
}
return res.end();
});
}
});
}
function sendAndNotify(from,text,to,id,res)
{
exchangeAPI.SendSMSAndNotifySuccess(from,text,to,id,function(resAPI)
{
console.log(resAPI[0]);
res.json(resAPI[0]);
res.end();
});
}
function reportSentToRapidPro(res)
{
dao.getListSMS3(function(listSMS)
{
if(listSMS!=null)
{
var oSMS=listSMS;
exchangeAPI.notifySendSucceded(oSMS.id,function(resRP)
{
//console.log(resRP[0]);
if(resRP[0].response=="SMS Status Updated")
{
dao.editSMS(oSMS.id,1,function (resSB)
{
if(resSB==true)
{
console.log("Success "+oSMS.id+": SMS updated in the DB")
//res.js('{"Success":"SMS updated in the DB"}');
}
else
{
console.log("Fail: SMS not updated in the DB");
//res.send('{"Fail":"SMS not updated in the DB"}');
}
res.json('{"response":"message updated in rapidPro"}');
return res.end();
});
}
else
{
}
});
}
else
{
//console.log("No messages sent to notify in pending");
res.json('{"response":"No messages in pending"}');
return res.end();
}
});
}
//the keyword is supposed to be at the beginning of the sms
function removeKeyWordFormSMS(_text)
{
var rebuiltSMS="";
var lowerCaseString=_text.toLowerCase();
var resSplit=lowerCaseString.split(smsKeyWord);
for(var i=0;i<resSplit.length;i++)
{
rebuiltSMS+=resSplit[i];
}
return rebuiltSMS;
}
function reportReceivedSMSToRapidpro(res)
{
dao.getListReceivedSMS(function(listSMS)
//exchangeAPI.getListReceivedSMS(function(listSMS)
{
if(listSMS!=null)
{
var oSMS=listSMS;
console.log(oSMS.dateSent);
var rebuiltSMS=removeKeyWordFormSMS(oSMS.text);
//console.log("####rebuilt sms :"+rebuiltSMS);
var tempDateSent=new Date(""+oSMS.dateSent);
var dateSent=tempDateSent.toISOString();
exchangeAPI.notifyReceivedSMS(oSMS.from,rebuiltSMS,dateSent,function(resRP)
{
//console.log(resRP[0].response.trim());
if(resRP[0].response.includes("SMS Accepted")==true)
{
dao.editReceivedSMS(oSMS.id_sparrow,function (resSB)
{
if(resSB==true)
{
console.log("Success "+oSMS.id_sparrow+": SMS updated in the DB")
//res.js('{"Success":"SMS updated in the DB"}');
}
else
{
console.log("Fail: SMS not updated in the DB");
//res.send('{"Fail":"SMS not updated in the DB"}');
}
res.json('{"response":"message updated in rapidPro"}');
return res.end();
});
}
else
{
console.log("Error :"+resRP[0].response);
}
});
}
else
{
//console.log("No incoming messages in pending");
res.json('{"response":"No messages in pending"}');
return res.end();
}
});
}
function reportSentToRapidPro2(res)
{
dao.getListSMS2();
}
app.param ("from", function (req,res,next,from)
{
req.from=from;
next();
});
app.param ("text", function (req,res,next,text)
{
req.text=text;
next();
});
app.param ("to", function (req,res,next,to)
{
req.to=to;
next();
});
app.param ("id", function (req,res,next,id)
{
req.id=id;
next();
});
app.get ("/send/from=:from&text=:text&to=:to&id=:id", function (req,res,next)
{
/*
console.log("From:"+req.from);
console.log("Text:"+req.text);
console.log("To:"+req.to);
console.log("To:"+req.id);
* */
//var encodedText=encodeURIComponent();
sendSMS(req.from,req.text,req.to,req.id,res);
});
app.get ("/reportsent", function (req,res,next)
{
//reportSentToRapidPro(res);
reportSentToRapidPro(res);
});
app.get ("/getsms", function (req,res,next)
{
var currentDate=new Date().toJSON();
//reportSentToRapidPro(res);
//reportSentToRapidPro(res);
//console.log(""+currentDate+"| "+"| Campain logged!!!!");
//console.log("----------------- >>>");
//console.log(req);
//return res.end();
var from="";
var to="";
var keyword="";
var text="";
from=req.query.from;
to=req.query.to;
keyword=req.query.keyword;
text=req.query.text;
var stringParams=""+from+"| "+to+"| "+keyword+"| "+text;
console.log(""+currentDate+"| "+stringParams+"| Campain logged!!!!");
console.log("----------------- >>>");
if(from!="undefined" && text!="undefined")
{
dao.saveReceivedSMS(from,text,to,function(resBD)
{
if(resBD==true)
{
console.log("Success: SMS logged in the DB");
}
else
{
console.log("Fail: SMS not logged in the DB");
}
return res.end();
});
}
//console.log(req);
//res.json('{"response":"callback url called"}');
//return res.end();
});
/*
app.get ("/testincrement", function (req,res,next)
{
dao.saveReceivedSMS('+243810890810','sms text','3660',function(resdB)
{
console.log(resdB)
});
return res.end();
});*/
/*
app.get ("/getReceivedSMS", function (req,res,next)
{
dao.getListReceivedSMS(function(listSMS)
{
if(listSMS!=null)
{
var oSMS=listSMS;
res.json(oSMS)
}
return res.end();
});
});
* */
app.get ("/pushsms2rp", function (req,res,next)
{
//reportSentToRapidPro(res);
reportReceivedSMSToRapidpro(res);
});
app.get ("/delcollection", function (req,res,next)
{
//reportSentToRapidPro(res);
dao.deleteSMSCollection();
});
}).call(this);