Commit 28c46ce1c23f45c582beb5149bc0b49944fd8352

Authored by Adhidarma Hadiwinoto
1 parent 151464213d
Exists in master

webhook prefix

Showing 1 changed file with 1 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: 443, 43 port: 443,
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('m2m-out26.reload97.com/bot' + 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 logger.info('Sending reply to ' + destination + ': ' + message); 66 logger.info('Sending reply to ' + destination + ': ' + message);
67 var chat_id = chat_ids[destination]; 67 var chat_id = chat_ids[destination];
68 bot.sendMessage(chat_id, message); 68 bot.sendMessage(chat_id, message);
69 } 69 }
70 70
71 function createHttpResponseServer(){ 71 function createHttpResponseServer(){
72 var httpServer = http.createServer(function(request,response){ 72 var httpServer = http.createServer(function(request,response){
73 var qs = url.parse(request.url, true).query; 73 var qs = url.parse(request.url, true).query;
74 logger.info('Incoming request from SMSIN server:', {qs: qs}) 74 logger.info('Incoming request from SMSIN server:', {qs: qs})
75 //logger.info(qs); 75 //logger.info(qs);
76 response.end('OK'); 76 response.end('OK');
77 77
78 sendMessage(qs.PhoneNumber, qs.text); 78 sendMessage(qs.PhoneNumber, qs.text);
79 }); 79 });
80 httpServer.listen(config.globals.listen_port, function(){ 80 httpServer.listen(config.globals.listen_port, function(){
81 logger.info("listening on " + config.globals.listen_port); 81 logger.info("listening on " + config.globals.listen_port);
82 }) 82 })
83 } 83 }
84 createHttpResponseServer(); 84 createHttpResponseServer();
85 85
86 bot.getMe().then(function (me) { 86 bot.getMe().then(function (me) {
87 logger.info('Hi my name is %s!', me.username); 87 logger.info('Hi my name is %s!', me.username);
88 }); 88 });
89 89
90 bot.on('text', function (msg) { 90 bot.on('text', function (msg) {
91 logger.info(msg); 91 logger.info(msg);
92 92
93 var now = Math.floor(new Date().getTime()/1000); 93 var now = Math.floor(new Date().getTime()/1000);
94 94
95 if (now - msg.date > config.globals.message_max_age){ 95 if (now - msg.date > config.globals.message_max_age){
96 var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; 96 var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.';
97 logger.info(message) 97 logger.info(message)
98 bot.sendMessage(msg.chat.id, message); 98 bot.sendMessage(msg.chat.id, message);
99 return; 99 return;
100 } 100 }
101 101
102 var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; 102 var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix;
103 103
104 chat_ids[from] = msg.chat.id; 104 chat_ids[from] = msg.chat.id;
105 //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from) 105 //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from)
106 106
107 bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); 107 bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text);
108 108
109 var request_opts = { 109 var request_opts = {
110 url: config.globals.aaa, 110 url: config.globals.aaa,
111 qs: { 111 qs: {
112 PhoneNumber: from, 112 PhoneNumber: from,
113 Text: msg.text, 113 Text: msg.text,
114 Res_Port: config.globals.listen_port, 114 Res_Port: config.globals.listen_port,
115 SMSCID: config.globals.smscid 115 SMSCID: config.globals.smscid
116 } 116 }
117 }; 117 };
118 118
119 request(request_opts, function(err, response, body) { 119 request(request_opts, function(err, response, body) {
120 if (err) { 120 if (err) {
121 logger.info('Request error: ' + err); 121 logger.info('Request error: ' + err);
122 return; 122 return;
123 } 123 }
124 124
125 logger.info('Response: ' + response); 125 logger.info('Response: ' + response);
126 logger.info('Body: ' + body); 126 logger.info('Body: ' + body);
127 127
128 }); 128 });
129 }); 129 });
130 130
131 131