Commit 6d3c240f9e156cc81b9f1c7b45c122d055762932
1 parent
bd55bd82bf
Exists in
master
Pesan Kadaluarsa closed #3
Showing 1 changed file with 13 additions and 2 deletions Inline Diff
index.js
1 | var iniparser = require('iniparser'); | 1 | var iniparser = require('iniparser'); |
2 | var config = iniparser.parseSync('./config.ini'); | 2 | var config = iniparser.parseSync('./config.ini'); |
3 | var telegram = require('node-telegram-bot-api'); | 3 | var telegram = require('node-telegram-bot-api'); |
4 | var request = require('request'); | 4 | var request = require('request'); |
5 | var http = require('http'); | 5 | var http = require('http'); |
6 | var url = require('url'); | 6 | var url = require('url'); |
7 | |||
7 | 8 | ||
8 | var chat_ids = {}; | 9 | var chat_ids = {}; |
9 | 10 | ||
10 | var options = { | 11 | var options = { |
11 | polling: true | 12 | polling: true |
12 | }; | 13 | }; |
13 | 14 | ||
14 | var bot = new telegram(config.globals.token, options); | 15 | var bot = new telegram(config.globals.token, options); |
15 | 16 | ||
16 | function sendMessage(destination, message) { | 17 | function sendMessage(destination, message) { |
17 | //destination = destination.replace(/@TELEGRAM$/, ''); | 18 | //destination = destination.replace(/@TELEGRAM$/, ''); |
18 | console.log('Sending reply to ' + destination + ': ' + message); | 19 | console.log('Sending reply to ' + destination + ': ' + message); |
19 | var chat_id = chat_ids[destination]; | 20 | var chat_id = chat_ids[destination]; |
20 | bot.sendMessage(chat_id, message); | 21 | bot.sendMessage(chat_id, message); |
21 | } | 22 | } |
22 | 23 | ||
23 | function createHttpResponseServer(){ | 24 | function createHttpResponseServer(){ |
24 | var httpServer = http.createServer(function(request,response){ | 25 | var httpServer = http.createServer(function(request,response){ |
25 | var qs = url.parse(request.url, true).query; | 26 | var qs = url.parse(request.url, true).query; |
26 | console.log('Incoming message from SMSIN server:') | 27 | console.log('Incoming message from SMSIN server:') |
27 | console.log(qs); | 28 | console.log(qs); |
28 | response.end('OK'); | 29 | response.end('OK'); |
29 | 30 | ||
30 | sendMessage(qs.PhoneNumber, qs.text); | 31 | sendMessage(qs.PhoneNumber, qs.text); |
31 | }); | 32 | }); |
32 | httpServer.listen(config.globals.listen_port, function(){ | 33 | httpServer.listen(config.globals.listen_port, function(){ |
33 | console.log("listening on " + config.globals.listen_port); | 34 | console.log("listening on " + config.globals.listen_port); |
34 | }) | 35 | }) |
35 | } | 36 | } |
36 | createHttpResponseServer(); | 37 | createHttpResponseServer(); |
37 | 38 | ||
38 | bot.getMe().then(function (me) { | 39 | bot.getMe().then(function (me) { |
39 | console.log('Hi my name is %s!', me.username); | 40 | console.log('Hi my name is %s!', me.username); |
40 | }); | 41 | }); |
42 | |||
41 | bot.on('text', function (msg) { | 43 | bot.on('text', function (msg) { |
42 | console.log(msg); | 44 | console.log(msg); |
43 | 45 | ||
44 | var from = msg.from.username.toUpperCase() + '@TELEGRAM'; | 46 | var now = Math.floor(new Date().getTime()/1000); |
47 | |||
48 | if (now - msg.date > config.globals.message_max_age){ | ||
49 | var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; | ||
50 | console.log(message) | ||
51 | bot.sendMessage(msg.chat.id, message); | ||
52 | return; | ||
53 | } | ||
54 | |||
55 | var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; | ||
45 | 56 | ||
46 | chat_ids[from] = msg.chat.id; | 57 | chat_ids[from] = msg.chat.id; |
47 | 58 | ||
48 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); | 59 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); |
49 | 60 | ||
50 | var request_opts = { | 61 | var request_opts = { |
51 | url: config.globals.aaa, | 62 | url: config.globals.aaa, |
52 | qs: { | 63 | qs: { |
53 | PhoneNumber: from, | 64 | PhoneNumber: from, |
54 | Text: msg.text, | 65 | Text: msg.text, |
55 | Res_Port: config.globals.listen_port, | 66 | Res_Port: config.globals.listen_port, |
56 | SMSCID: config.globals.smscid | 67 | SMSCID: config.globals.smscid |
57 | } | 68 | } |
58 | }; | 69 | }; |
59 | 70 | ||
60 | request(request_opts, function(err, response, body) { | 71 | request(request_opts, function(err, response, body) { |
61 | if (err) { | 72 | if (err) { |
62 | console.log('Request error: ' + err); | 73 | console.log('Request error: ' + err); |
63 | return; | 74 | return; |
64 | } | 75 | } |
65 | 76 | ||
66 | console.log('Response: ' + response); | 77 | console.log('Response: ' + response); |
67 | console.log('Body: ' + body); | 78 | console.log('Body: ' + body); |
68 | 79 | ||
69 | }); | 80 | }); |
70 | }); | 81 | }); |
71 | 82 | ||
72 | 83 |