-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sms_node.js
104 lines (84 loc) · 2.6 KB
/
test_sms_node.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
/*
* This work has been done by Phillip Ochola Makanyengo
* Email: [email protected]
*
* This work uses open source code and libraries and
* can therefore be replicated unless certain portions
* are stated otherwise.
*
* Please refer to the author when using the code.
*
*/
//Nodejs implementation of a http server
// Import required Modules
var comms = require('morphbridge').comms,
logger = require('morphbridge').logger,
channels_obj = require('morphbridge').channels_obj,
buffer = channels_obj.newBuffer(),
http = require('http'),
https = require('https');
//Optionally Set a limit to the buffer to limit the size
//buffer.setLimit(5); //This code limits the buffer to 20 messages
//Handle internode messages
/*
Place your own function to handle messages recieved by the node.
*/
var handle = function(msg){
//console.log('SMS node received ZMQ message');
//logger.logStat('SMS node received ZMQ message');
buffer.load(msg);
};
//Socket Initialisation
comms.init(handle); //Pass message handling function to sub_socket
// We need this to build our post string
var querystring = require('querystring');
// Your login credentials
var username = 'its2uraps';
var apikey = 'bf5e56550abd9434eaf31fe26f22875f65b5d6afa4c6f1f01ddf896bc8a6d1a2';
function fetchMessages(lastReceivedId_) {
// Build the post string from an object
var options = {
host: 'api.africastalking.com',
port: '443',
path: '/version1/messaging?username=' + username + '&lastReceivedId=' + lastReceivedId_,
method: 'GET',
rejectUnauthorized : false,
requestCert : true,
agent : false,
headers: {
'Accept': 'application/json',
'apikey': apikey
}
};
var request = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
//console.log(JSON.parse(chunk));
var jsObject = JSON.parse(chunk);
var messages = jsObject.SMSMessageData.Messages;
if ( messages.length > 0 ) {
for (var i = 0; i < messages.length; ++i ) {
var logStr = 'from=' + messages[i].from;
logStr += ';message=' + messages[i].text;
console.log(logStr);
comms.transmit(logStr);
lastReceivedId_ = messages[i].id;
}
}
});
});
request.end();
console.log('LastReceivedId: ' + lastReceivedId_);
setTimeout(function(){
fetchMessages(lastReceivedId);
}, 3000);
}
var lastReceivedId = 0;
setTimeout(function(){
fetchMessages(lastReceivedId);
}, 3000);
process.on('SIGINT', function() {
comms.close()
console.log('\nClosed')
process.exit();
});