Commit 49a1346c139e20909ae336c34c22175f513de088
1 parent
9b9050ae75
Exists in
master
dump chat ids
Showing 1 changed file with 31 additions and 13 deletions Side-by-side Diff
index.js
... | ... | @@ -32,7 +32,7 @@ var logger = new (winston.Logger)({ |
32 | 32 | }) |
33 | 33 | ] |
34 | 34 | }); |
35 | - | |
35 | + | |
36 | 36 | |
37 | 37 | var chat_ids = {}; |
38 | 38 | |
... | ... | @@ -63,8 +63,15 @@ function deleteChatId(from) { |
63 | 63 | |
64 | 64 | function sendMessage(destination, message) { |
65 | 65 | //destination = destination.replace(/@TELEGRAM$/, ''); |
66 | - logger.info('Sending reply to ' + destination + ': ' + message); | |
67 | - var chat_id = chat_ids[destination]; | |
66 | + var chat_id = chat_ids[destination]; | |
67 | + | |
68 | + if (!chat_id) { | |
69 | + logger.warn('Can not find approriate chat id for ' + destination + '. Abort sending message.'); | |
70 | + return; | |
71 | + } | |
72 | + | |
73 | + logger.info('Sending reply to ' + destination + '(' + chat_id + '): ' + message); | |
74 | + | |
68 | 75 | bot.sendMessage(chat_id, message); |
69 | 76 | } |
70 | 77 | |
... | ... | @@ -72,9 +79,9 @@ function createHttpResponseServer(){ |
72 | 79 | var httpServer = http.createServer(function(request,response){ |
73 | 80 | var qs = url.parse(request.url, true).query; |
74 | 81 | logger.info('Incoming request from SMSIN server:', {qs: qs}) |
75 | - //logger.info(qs); | |
82 | + //logger.info(qs); | |
76 | 83 | response.end('OK'); |
77 | - | |
84 | + | |
78 | 85 | sendMessage(qs.PhoneNumber, qs.text); |
79 | 86 | }); |
80 | 87 | httpServer.listen(config.globals.listen_port, function(){ |
... | ... | @@ -89,21 +96,21 @@ bot.getMe().then(function (me) { |
89 | 96 | |
90 | 97 | bot.on('text', function (msg) { |
91 | 98 | logger.info(msg); |
92 | - | |
99 | + | |
93 | 100 | var now = Math.floor(new Date().getTime()/1000); |
94 | - | |
101 | + | |
95 | 102 | if (now - msg.date > config.globals.message_max_age){ |
96 | 103 | var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; |
97 | 104 | logger.info(message) |
98 | 105 | bot.sendMessage(msg.chat.id, message); |
99 | 106 | return; |
100 | 107 | } |
101 | - | |
108 | + | |
102 | 109 | var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; |
103 | - | |
110 | + | |
104 | 111 | chat_ids[from] = msg.chat.id; |
105 | 112 | //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from) |
106 | - | |
113 | + | |
107 | 114 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); |
108 | 115 | |
109 | 116 | var request_opts = { |
... | ... | @@ -115,16 +122,27 @@ bot.on('text', function (msg) { |
115 | 122 | SMSCID: config.globals.smscid |
116 | 123 | } |
117 | 124 | }; |
118 | - | |
125 | + | |
119 | 126 | request(request_opts, function(err, response, body) { |
120 | 127 | if (err) { |
121 | 128 | logger.info('Request error: ' + err); |
122 | 129 | return; |
123 | 130 | } |
124 | - | |
131 | + | |
125 | 132 | logger.info('Response: ' + response); |
126 | 133 | logger.info('Body: ' + body); |
127 | - | |
134 | + | |
128 | 135 | }); |
129 | 136 | }); |
130 | 137 | |
138 | + | |
139 | +function dumpChatIds() { | |
140 | + var i = 0; | |
141 | + for (var key in chat_ids) { | |
142 | + logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key]) | |
143 | + } | |
144 | +} | |
145 | + | |
146 | +if (config.dump_chat_ids_interval) { | |
147 | + setInterval(dumpChatIds, config.dump_chat_ids_interval * 1000); | |
148 | +} |