Commit 8e8c4659f307c3aed5708ca00375ede3fc209f78

Authored by Adhidarma Hadiwinoto
1 parent 81cfbaf151
Exists in master

use strict

Showing 1 changed file with 24 additions and 26 deletions Side-by-side Diff

1   -var iniparser = require('iniparser');
2   -var config = iniparser.parseSync('./config.ini');
3   -var telegram = require('node-telegram-bot-api');
4   -var request = require('request');
5   -var http = require('http');
6   -var url = require('url');
7   -var strftime = require('strftime');
8   -var winston = require('winston');
9   -
10   -var logger = new (winston.Logger)({
  1 +"use strict";
  2 +
  3 +const iniparser = require('iniparser');
  4 +const config = iniparser.parseSync('./config.ini');
  5 +const telegram = require('node-telegram-bot-api');
  6 +const request = require('request');
  7 +const http = require('http');
  8 +const url = require('url');
  9 +const strftime = require('strftime');
  10 +const winston = require('winston');
  11 +
  12 +const logger = new (winston.Logger)({
11 13 transports: [
12 14 new (winston.transports.Console)({
13 15 timestamp: function() {
... ... @@ -34,11 +36,9 @@ var logger = new (winston.Logger)({
34 36 });
35 37  
36 38  
37   -var chat_ids = {};
  39 +const chat_ids = {};
38 40  
39   -
40   -var bot;
41   -var options = {
  41 +const options = {
42 42 webHook: {
43 43 port: config.globals.webhook_port,
44 44 key: __dirname+'/key.pem',
... ... @@ -46,7 +46,7 @@ var options = {
46 46 }
47 47 };
48 48  
49   -bot = new telegram(config.globals.token, options);
  49 +const bot = new telegram(config.globals.token, options);
50 50 bot.setWebHook(config.globals.webhook_prefix + config.globals.token, __dirname+'/crt.pem');
51 51  
52 52 /*
... ... @@ -63,7 +63,7 @@ function deleteChatId(from) {
63 63  
64 64 function sendMessage(destination, message) {
65 65 //destination = destination.replace(/@TELEGRAM$/, '');
66   - var chat_id = chat_ids[destination];
  66 + let chat_id = chat_ids[destination];
67 67  
68 68 if (!chat_id) {
69 69 logger.warn('Can not find approriate chat id for ' + destination + '. Abort sending message.');
... ... @@ -76,10 +76,9 @@ function sendMessage(destination, message) {
76 76 }
77 77  
78 78 function createHttpResponseServer(){
79   - var httpServer = http.createServer(function(request,response){
80   - var qs = url.parse(request.url, true).query;
  79 + const httpServer = http.createServer(function(request,response){
  80 + let qs = url.parse(request.url, true).query;
81 81 logger.info('Incoming request from SMSIN server:', {qs: qs})
82   - //logger.info(qs);
83 82 response.end('OK');
84 83  
85 84 sendMessage(qs.PhoneNumber, qs.text);
... ... @@ -104,19 +103,18 @@ bot.on('text', function (msg) {
104 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 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 110 logger.info(message)
112 111 bot.sendMessage(msg.chat.id, message);
113 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 117 chat_ids[from] = msg.chat.id;
119   - //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from)
120 118  
121 119 bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text);
122 120  
... ... @@ -143,9 +141,9 @@ bot.on('text', function (msg) {
143 141 });
144 142  
145 143  
146   -function dumpChatIds() {
147   - var i = 0;
148   - for (var key in chat_ids) {
  144 +function dumpChatIds() {
  145 + let i = 0;
  146 + for (let key in chat_ids) {
149 147 logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key])
150 148 }
151 149 }