Commit 7f830e2c0030b2239a3599f4d8fd52b052c80e4e

Authored by Adhidarma Hadiwinoto
1 parent 7676e51eb0
Exists in master

webhook

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