Commit 8e8c4659f307c3aed5708ca00375ede3fc209f78
1 parent
81cfbaf151
Exists in
master
use strict
Showing 1 changed file with 24 additions and 26 deletions Inline Diff
index.js
1 | var iniparser = require('iniparser'); | 1 | "use strict"; |
2 | var config = iniparser.parseSync('./config.ini'); | 2 | |
3 | var telegram = require('node-telegram-bot-api'); | 3 | const iniparser = require('iniparser'); |
4 | var request = require('request'); | 4 | const config = iniparser.parseSync('./config.ini'); |
5 | var http = require('http'); | 5 | const telegram = require('node-telegram-bot-api'); |
6 | var url = require('url'); | 6 | const request = require('request'); |
7 | var strftime = require('strftime'); | 7 | const http = require('http'); |
8 | var winston = require('winston'); | 8 | const url = require('url'); |
9 | 9 | const strftime = require('strftime'); | |
10 | var logger = new (winston.Logger)({ | 10 | const winston = require('winston'); |
11 | |||
12 | const logger = new (winston.Logger)({ | ||
11 | transports: [ | 13 | transports: [ |
12 | new (winston.transports.Console)({ | 14 | new (winston.transports.Console)({ |
13 | timestamp: function() { | 15 | timestamp: function() { |
14 | return strftime('%F %T', new Date()); | 16 | return strftime('%F %T', new Date()); |
15 | }, | 17 | }, |
16 | formatter: function(options) { | 18 | formatter: function(options) { |
17 | // Return string will be passed to logger. | 19 | // Return string will be passed to logger. |
18 | return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') + | 20 | 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) : '' ); | 21 | (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' ); |
20 | } | 22 | } |
21 | }), | 23 | }), |
22 | new (winston.transports.DailyRotateFile)({ | 24 | new (winston.transports.DailyRotateFile)({ |
23 | filename: 'log', | 25 | filename: 'log', |
24 | timestamp: function() { | 26 | timestamp: function() { |
25 | return strftime('%F %T', new Date()); | 27 | return strftime('%F %T', new Date()); |
26 | }, | 28 | }, |
27 | formatter: function(options) { | 29 | formatter: function(options) { |
28 | // Return string will be passed to logger. | 30 | // Return string will be passed to logger. |
29 | return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') + | 31 | 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) : '' ); | 32 | (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' ); |
31 | } | 33 | } |
32 | }) | 34 | }) |
33 | ] | 35 | ] |
34 | }); | 36 | }); |
35 | 37 | ||
36 | 38 | ||
37 | var chat_ids = {}; | 39 | const chat_ids = {}; |
38 | 40 | ||
39 | 41 | const options = { | |
40 | var bot; | ||
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 | const 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 | let 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 | const httpServer = http.createServer(function(request,response){ |
80 | var qs = url.parse(request.url, true).query; | 80 | let 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); | ||
83 | response.end('OK'); | 82 | response.end('OK'); |
84 | 83 | ||
85 | sendMessage(qs.PhoneNumber, qs.text); | 84 | sendMessage(qs.PhoneNumber, qs.text); |
86 | }); | 85 | }); |
87 | httpServer.listen(config.globals.listen_port, function(){ | 86 | httpServer.listen(config.globals.listen_port, function(){ |
88 | logger.info("listening on " + config.globals.listen_port); | 87 | logger.info("listening on " + config.globals.listen_port); |
89 | }) | 88 | }) |
90 | } | 89 | } |
91 | createHttpResponseServer(); | 90 | createHttpResponseServer(); |
92 | 91 | ||
93 | bot.getMe().then(function (me) { | 92 | bot.getMe().then(function (me) { |
94 | logger.info('Hi my name is %s!', me.username); | 93 | logger.info('Hi my name is %s!', me.username); |
95 | }); | 94 | }); |
96 | 95 | ||
97 | bot.on('text', function (msg) { | 96 | bot.on('text', function (msg) { |
98 | logger.info('Incoming message', {msg: msg}); | 97 | logger.info('Incoming message', {msg: msg}); |
99 | 98 | ||
100 | if (!msg.from.username) { | 99 | if (!msg.from.username) { |
101 | var replyMessage = "Pesan anda diabaikan, anda belum memiliki username pada telegram: " + msg.text; | 100 | var replyMessage = "Pesan anda diabaikan, anda belum memiliki username pada telegram: " + msg.text; |
102 | logger.info(replyMessage, {msg: msg}); | 101 | logger.info(replyMessage, {msg: msg}); |
103 | bot.sendMessage(msg.chat.id, replyMessage); | 102 | bot.sendMessage(msg.chat.id, replyMessage); |
104 | return; | 103 | return; |
105 | } | 104 | } |
106 | 105 | ||
107 | var now = Math.floor(new Date().getTime()/1000); | 106 | let now = Math.floor(new Date().getTime()/1000); |
108 | 107 | ||
109 | if (now - msg.date > config.globals.message_max_age){ | 108 | if (now - msg.date > config.globals.message_max_age){ |
110 | var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; | 109 | let message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; |
111 | logger.info(message) | 110 | logger.info(message) |
112 | bot.sendMessage(msg.chat.id, message); | 111 | bot.sendMessage(msg.chat.id, message); |
113 | return; | 112 | return; |
114 | } | 113 | } |
115 | 114 | ||
116 | var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; | 115 | let from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; |
117 | 116 | ||
118 | chat_ids[from] = msg.chat.id; | 117 | chat_ids[from] = msg.chat.id; |
119 | //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from) | ||
120 | 118 | ||
121 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); | 119 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); |
122 | 120 | ||
123 | var request_opts = { | 121 | var request_opts = { |
124 | url: config.globals.aaa, | 122 | url: config.globals.aaa, |
125 | qs: { | 123 | qs: { |
126 | PhoneNumber: from, | 124 | PhoneNumber: from, |
127 | Text: msg.text, | 125 | Text: msg.text, |
128 | Res_Port: config.globals.listen_port, | 126 | Res_Port: config.globals.listen_port, |
129 | SMSCID: config.globals.smscid | 127 | SMSCID: config.globals.smscid |
130 | } | 128 | } |
131 | }; | 129 | }; |
132 | 130 | ||
133 | request(request_opts, function(err, response, body) { | 131 | request(request_opts, function(err, response, body) { |
134 | if (err) { | 132 | if (err) { |
135 | logger.info('Request error: ' + err); | 133 | logger.info('Request error: ' + err); |
136 | return; | 134 | return; |
137 | } | 135 | } |
138 | 136 | ||
139 | logger.info('Response: ' + response); | 137 | logger.info('Response: ' + response); |
140 | logger.info('Body: ' + body); | 138 | logger.info('Body: ' + body); |
141 | 139 | ||
142 | }); | 140 | }); |
143 | }); | 141 | }); |
144 | 142 | ||
145 | 143 | ||
146 | function dumpChatIds() { | 144 | function dumpChatIds() { |
147 | var i = 0; | 145 | let i = 0; |
148 | for (var key in chat_ids) { | 146 | for (let key in chat_ids) { |
149 | logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key]) | 147 | logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key]) |
150 | } | 148 | } |
151 | } | 149 | } |
152 | 150 | ||
153 | if (config.dump_chat_ids_interval) { | 151 | if (config.dump_chat_ids_interval) { |
154 | setInterval(dumpChatIds, config.dump_chat_ids_interval * 1000); | 152 | setInterval(dumpChatIds, config.dump_chat_ids_interval * 1000); |