Commit 25308709eb2feeb5c62759e971deb4be80b2492a

Authored by Adhidarma Hadiwinoto
1 parent b482e82b13
Exists in master

configuration sample

Showing 3 changed files with 36 additions and 21 deletions Side-by-side Diff

... ... @@ -0,0 +1,15 @@
  1 +{
  2 + "webhook_port": 28232,
  3 + "webhook_prefix": "",
  4 + "token": "",
  5 + "listen_port": 23232,
  6 + "msisdn_suffix": "@telegram.org",
  7 + "message_max_age": 120,
  8 + "greeting_prefix": "",
  9 + "evo_url": "",
  10 + "smsc": "",
  11 + "send_ping_to_evo": 0,
  12 + "ping_pin": "",
  13 + "ping_from": "",
  14 +
  15 +}
... ... @@ -25,11 +25,11 @@ function formatTimestamp(ms) {
25 25  
26 26 function onMessage(sender, message, ts, sendMessage) {
27 27 var opts = {
28   - url: config.globals.evo_url,
  28 + url: config.evo_url,
29 29 qs: {
30 30 msg: message,
31 31 msisdn: sender,
32   - smsc: config.globals.smsc,
  32 + smsc: config.smsc,
33 33 ts: formatTimestamp(ts)
34 34 }
35 35 };
... ... @@ -64,15 +64,15 @@ function onMessage(sender, message, ts, sendMessage) {
64 64 });
65 65  
66 66 // kirim ping 1 detik setelah pesan agar segera diproses
67   - if (config.globals.send_ping_to_evo == '1') {
  67 + if (config.send_ping_to_evo == '1') {
68 68 setTimeout(function() {
69 69  
70 70 var pingOpts = {
71   - url: config.globals.evo_url,
  71 + url: config.evo_url,
72 72 qs: {
73   - msg: 'S.' + config.globals.ping_pin,
74   - msisdn: config.globals.ping_ym_id + config.globals.msisdn_suffix,
75   - smsc: config.globals.smsc,
  73 + msg: 'S.' + config.ping_pin,
  74 + msisdn: config.ping_from + config.msisdn_suffix,
  75 + smsc: config.smsc,
76 76 ts: strftime('%F %T')
77 77 }
78 78 };
... ... @@ -94,12 +94,12 @@ function onHttpIncomingMessage(request, response) {
94 94 logger.verbose("onHttpIncomingMessage()", {qs: qs});
95 95  
96 96 // abaikan balikan ping
97   - if (qs.to == config.globals.ping_ym_id) {
  97 + if (qs.to == config.ping_from) {
98 98 return;
99 99 }
100 100  
101   - var destination = qs.to.replace(config.globals.msisdn_suffix, '');
102   - logger.info('Sending YM message from ' + config.globals.username + ' to ' + destination + ': ' + qs.msg);
  101 + var destination = qs.to.replace(config.msisdn_suffix, '');
  102 + logger.info('Sending message to ' + destination + ': ' + qs.msg);
103 103 sendMessage(destination, qs.msg);
104 104 response.end('OK');
105 105 }
... ... @@ -108,8 +108,8 @@ function createHttpServer() {
108 108 logger.verbose('createHttpServer()');
109 109  
110 110 var httpServer = http.createServer(onHttpIncomingMessage);
111   - httpServer.listen(config.globals.listen_port, function(){
112   - logger.info("HTTP server listening on " + config.globals.listen_port);
  111 + httpServer.listen(config.listen_port, function(){
  112 + logger.info("HTTP server listening on " + config.listen_port);
113 113 });
114 114 }
115 115  
... ... @@ -13,14 +13,14 @@ var chat_ids = {};
13 13  
14 14 var options = {
15 15 webHook: {
16   - port: config.globals.webhook_port,
  16 + port: config.webhook_port,
17 17 key: __dirname+'/key.pem',
18 18 cert: __dirname+'/crt.pem'
19 19 }
20 20 };
21 21  
22   -var bot = new telegram(config.globals.token, options);
23   -bot.setWebHook(config.globals.webhook_prefix + config.globals.token, __dirname+'/crt.pem');
  22 +var bot = new telegram(config.token, options);
  23 +bot.setWebHook(config.webhook_prefix + config.token, __dirname+'/crt.pem');
24 24  
25 25 function deleteChatId(from) {
26 26 delete chat_ids[from];
... ... @@ -41,8 +41,8 @@ function createHttpResponseServer(){
41 41  
42 42 sendMessage(qs.PhoneNumber, qs.text);
43 43 });
44   - httpServer.listen(config.globals.listen_port, function(){
45   - logger.info("listening on " + config.globals.listen_port);
  44 + httpServer.listen(config.listen_port, function(){
  45 + logger.info("listening on " + config.listen_port);
46 46 })
47 47 }
48 48 createHttpResponseServer();
... ... @@ -54,11 +54,11 @@ bot.getMe().then(function (me) {
54 54 bot.on('text', function (msg) {
55 55 logger.info(msg);
56 56  
57   - var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix;
  57 + var from = msg.from.username.toUpperCase() + config.msisdn_suffix;
58 58  
59 59 var now = Math.floor(new Date().getTime()/1000);
60 60  
61   - if (now - msg.date > config.globals.message_max_age){
  61 + if (now - msg.date > config.message_max_age){
62 62 var message = "Pesan anda diabaikan, silahkan diulang beberapa saat lagi jika diperlukan: " + msg.text;
63 63 logger.info(message, {msg: msg})
64 64 //bot.sendMessage(msg.chat.id, message);
... ... @@ -69,8 +69,8 @@ bot.on('text', function (msg) {
69 69 chat_ids[from] = msg.chat.id;
70 70  
71 71 var greeting_prefix = "Pesan anda telah diterima:";
72   - if (config.globals.greeting_prefix) {
73   - greeting_prefix = config.globals.greeting_prefix;
  72 + if (config.greeting_prefix) {
  73 + greeting_prefix = config.greeting_prefix;
74 74 }
75 75  
76 76 bot.sendMessage(msg.chat.id, greeting_prefix + ' ' + msg.text);