Commit 81cfbaf1511aa1143a9153e20c3100e25d755794

Authored by Adhidarma Hadiwinoto
1 parent 49a1346c13
Exists in master

penanganan pengirim pesan tanpa username

Showing 1 changed file with 8 additions and 1 deletions Inline Diff

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 var strftime = require('strftime'); 7 var strftime = require('strftime');
8 var winston = require('winston'); 8 var winston = require('winston');
9 9
10 var logger = new (winston.Logger)({ 10 var logger = new (winston.Logger)({
11 transports: [ 11 transports: [
12 new (winston.transports.Console)({ 12 new (winston.transports.Console)({
13 timestamp: function() { 13 timestamp: function() {
14 return strftime('%F %T', new Date()); 14 return strftime('%F %T', new Date());
15 }, 15 },
16 formatter: function(options) { 16 formatter: function(options) {
17 // Return string will be passed to logger. 17 // Return string will be passed to logger.
18 return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') + 18 return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') +
19 (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' ); 19 (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
20 } 20 }
21 }), 21 }),
22 new (winston.transports.DailyRotateFile)({ 22 new (winston.transports.DailyRotateFile)({
23 filename: 'log', 23 filename: 'log',
24 timestamp: function() { 24 timestamp: function() {
25 return strftime('%F %T', new Date()); 25 return strftime('%F %T', new Date());
26 }, 26 },
27 formatter: function(options) { 27 formatter: function(options) {
28 // Return string will be passed to logger. 28 // Return string will be passed to logger.
29 return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') + 29 return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') +
30 (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' ); 30 (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
31 } 31 }
32 }) 32 })
33 ] 33 ]
34 }); 34 });
35 35
36 36
37 var chat_ids = {}; 37 var chat_ids = {};
38 38
39 39
40 var bot; 40 var bot;
41 var options = { 41 var options = {
42 webHook: { 42 webHook: {
43 port: config.globals.webhook_port, 43 port: config.globals.webhook_port,
44 key: __dirname+'/key.pem', 44 key: __dirname+'/key.pem',
45 cert: __dirname+'/crt.pem' 45 cert: __dirname+'/crt.pem'
46 } 46 }
47 }; 47 };
48 48
49 bot = new telegram(config.globals.token, options); 49 bot = new telegram(config.globals.token, options);
50 bot.setWebHook(config.globals.webhook_prefix + config.globals.token, __dirname+'/crt.pem'); 50 bot.setWebHook(config.globals.webhook_prefix + config.globals.token, __dirname+'/crt.pem');
51 51
52 /* 52 /*
53 var options = { 53 var options = {
54 polling: true 54 polling: true
55 }; 55 };
56 bot = new telegram(config.globals.token, options); 56 bot = new telegram(config.globals.token, options);
57 */ 57 */
58 58
59 59
60 function deleteChatId(from) { 60 function deleteChatId(from) {
61 delete chat_ids[from]; 61 delete chat_ids[from];
62 } 62 }
63 63
64 function sendMessage(destination, message) { 64 function sendMessage(destination, message) {
65 //destination = destination.replace(/@TELEGRAM$/, ''); 65 //destination = destination.replace(/@TELEGRAM$/, '');
66 var chat_id = chat_ids[destination]; 66 var chat_id = chat_ids[destination];
67 67
68 if (!chat_id) { 68 if (!chat_id) {
69 logger.warn('Can not find approriate chat id for ' + destination + '. Abort sending message.'); 69 logger.warn('Can not find approriate chat id for ' + destination + '. Abort sending message.');
70 return; 70 return;
71 } 71 }
72 72
73 logger.info('Sending reply to ' + destination + '(' + chat_id + '): ' + message); 73 logger.info('Sending reply to ' + destination + '(' + chat_id + '): ' + message);
74 74
75 bot.sendMessage(chat_id, message); 75 bot.sendMessage(chat_id, message);
76 } 76 }
77 77
78 function createHttpResponseServer(){ 78 function createHttpResponseServer(){
79 var httpServer = http.createServer(function(request,response){ 79 var httpServer = http.createServer(function(request,response){
80 var qs = url.parse(request.url, true).query; 80 var qs = url.parse(request.url, true).query;
81 logger.info('Incoming request from SMSIN server:', {qs: qs}) 81 logger.info('Incoming request from SMSIN server:', {qs: qs})
82 //logger.info(qs); 82 //logger.info(qs);
83 response.end('OK'); 83 response.end('OK');
84 84
85 sendMessage(qs.PhoneNumber, qs.text); 85 sendMessage(qs.PhoneNumber, qs.text);
86 }); 86 });
87 httpServer.listen(config.globals.listen_port, function(){ 87 httpServer.listen(config.globals.listen_port, function(){
88 logger.info("listening on " + config.globals.listen_port); 88 logger.info("listening on " + config.globals.listen_port);
89 }) 89 })
90 } 90 }
91 createHttpResponseServer(); 91 createHttpResponseServer();
92 92
93 bot.getMe().then(function (me) { 93 bot.getMe().then(function (me) {
94 logger.info('Hi my name is %s!', me.username); 94 logger.info('Hi my name is %s!', me.username);
95 }); 95 });
96 96
97 bot.on('text', function (msg) { 97 bot.on('text', function (msg) {
98 logger.info(msg); 98 logger.info('Incoming message', {msg: msg});
99
100 if (!msg.from.username) {
101 var replyMessage = "Pesan anda diabaikan, anda belum memiliki username pada telegram: " + msg.text;
102 logger.info(replyMessage, {msg: msg});
103 bot.sendMessage(msg.chat.id, replyMessage);
104 return;
105 }
99 106
100 var now = Math.floor(new Date().getTime()/1000); 107 var now = Math.floor(new Date().getTime()/1000);
101 108
102 if (now - msg.date > config.globals.message_max_age){ 109 if (now - msg.date > config.globals.message_max_age){
103 var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; 110 var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.';
104 logger.info(message) 111 logger.info(message)
105 bot.sendMessage(msg.chat.id, message); 112 bot.sendMessage(msg.chat.id, message);
106 return; 113 return;
107 } 114 }
108 115
109 var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; 116 var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix;
110 117
111 chat_ids[from] = msg.chat.id; 118 chat_ids[from] = msg.chat.id;
112 //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from) 119 //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from)
113 120
114 bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); 121 bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text);
115 122
116 var request_opts = { 123 var request_opts = {
117 url: config.globals.aaa, 124 url: config.globals.aaa,
118 qs: { 125 qs: {
119 PhoneNumber: from, 126 PhoneNumber: from,
120 Text: msg.text, 127 Text: msg.text,
121 Res_Port: config.globals.listen_port, 128 Res_Port: config.globals.listen_port,
122 SMSCID: config.globals.smscid 129 SMSCID: config.globals.smscid
123 } 130 }
124 }; 131 };
125 132
126 request(request_opts, function(err, response, body) { 133 request(request_opts, function(err, response, body) {
127 if (err) { 134 if (err) {
128 logger.info('Request error: ' + err); 135 logger.info('Request error: ' + err);
129 return; 136 return;
130 } 137 }
131 138
132 logger.info('Response: ' + response); 139 logger.info('Response: ' + response);
133 logger.info('Body: ' + body); 140 logger.info('Body: ' + body);
134 141
135 }); 142 });
136 }); 143 });
137 144
138 145
139 function dumpChatIds() { 146 function dumpChatIds() {
140 var i = 0; 147 var i = 0;
141 for (var key in chat_ids) { 148 for (var key in chat_ids) {
142 logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key]) 149 logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key])
143 } 150 }
144 } 151 }
145 152
146 if (config.dump_chat_ids_interval) { 153 if (config.dump_chat_ids_interval) {
147 setInterval(dumpChatIds, config.dump_chat_ids_interval * 1000); 154 setInterval(dumpChatIds, config.dump_chat_ids_interval * 1000);
148 } 155 }
149 156