Commit 025274bdbf9d56f2c1b64672c3cb534e3a649a0c
1 parent
6d3c240f9e
Exists in
master
Logging Lebih Rapih closed #1
Showing 3 changed files with 47 additions and 11 deletions Inline Diff
.gitignore
1 | node_modules/ | 1 | node_modules/ |
2 | config.ini | 2 | config.ini |
3 | log_* |
index.js
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'); | ||
8 | var winston = require('winston'); | ||
9 | |||
10 | var logger = new (winston.Logger)({ | ||
11 | transports: [ | ||
12 | new (winston.transports.Console)({ | ||
13 | timestamp: function() { | ||
14 | return strftime('%F %T', new Date()); | ||
15 | }, | ||
16 | formatter: function(options) { | ||
17 | // Return string will be passed to logger. | ||
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) : '' ); | ||
20 | } | ||
21 | }), | ||
22 | new (winston.transports.DailyRotateFile)({ | ||
23 | filename: 'log_', | ||
24 | timestamp: function() { | ||
25 | return strftime('%F %T', new Date()); | ||
26 | }, | ||
27 | formatter: function(options) { | ||
28 | // Return string will be passed to logger. | ||
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) : '' ); | ||
31 | } | ||
32 | }) | ||
33 | ] | ||
34 | }); | ||
7 | 35 | ||
8 | 36 | ||
9 | var chat_ids = {}; | 37 | var chat_ids = {}; |
10 | 38 | ||
11 | var options = { | 39 | var options = { |
12 | polling: true | 40 | polling: true |
13 | }; | 41 | }; |
14 | 42 | ||
15 | var bot = new telegram(config.globals.token, options); | 43 | var bot = new telegram(config.globals.token, options); |
16 | 44 | ||
45 | function deleteChatId(from) { | ||
46 | delete chat_ids[from]; | ||
47 | } | ||
48 | |||
17 | function sendMessage(destination, message) { | 49 | function sendMessage(destination, message) { |
18 | //destination = destination.replace(/@TELEGRAM$/, ''); | 50 | //destination = destination.replace(/@TELEGRAM$/, ''); |
19 | console.log('Sending reply to ' + destination + ': ' + message); | 51 | logger.info('Sending reply to ' + destination + ': ' + message); |
20 | var chat_id = chat_ids[destination]; | 52 | var chat_id = chat_ids[destination]; |
21 | bot.sendMessage(chat_id, message); | 53 | bot.sendMessage(chat_id, message); |
22 | } | 54 | } |
23 | 55 | ||
24 | function createHttpResponseServer(){ | 56 | function createHttpResponseServer(){ |
25 | var httpServer = http.createServer(function(request,response){ | 57 | var httpServer = http.createServer(function(request,response){ |
26 | var qs = url.parse(request.url, true).query; | 58 | var qs = url.parse(request.url, true).query; |
27 | console.log('Incoming message from SMSIN server:') | 59 | logger.info('Incoming request from SMSIN server:', {qs: qs}) |
28 | console.log(qs); | 60 | //logger.info(qs); |
29 | response.end('OK'); | 61 | response.end('OK'); |
30 | 62 | ||
31 | sendMessage(qs.PhoneNumber, qs.text); | 63 | sendMessage(qs.PhoneNumber, qs.text); |
32 | }); | 64 | }); |
33 | httpServer.listen(config.globals.listen_port, function(){ | 65 | httpServer.listen(config.globals.listen_port, function(){ |
34 | console.log("listening on " + config.globals.listen_port); | 66 | logger.info("listening on " + config.globals.listen_port); |
35 | }) | 67 | }) |
36 | } | 68 | } |
37 | createHttpResponseServer(); | 69 | createHttpResponseServer(); |
38 | 70 | ||
39 | bot.getMe().then(function (me) { | 71 | bot.getMe().then(function (me) { |
40 | console.log('Hi my name is %s!', me.username); | 72 | logger.info('Hi my name is %s!', me.username); |
41 | }); | 73 | }); |
42 | 74 | ||
43 | bot.on('text', function (msg) { | 75 | bot.on('text', function (msg) { |
44 | console.log(msg); | 76 | logger.info(msg); |
45 | 77 | ||
46 | var now = Math.floor(new Date().getTime()/1000); | 78 | var now = Math.floor(new Date().getTime()/1000); |
47 | 79 | ||
48 | if (now - msg.date > config.globals.message_max_age){ | 80 | if (now - msg.date > config.globals.message_max_age){ |
49 | var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; | 81 | var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.'; |
50 | console.log(message) | 82 | logger.info(message) |
51 | bot.sendMessage(msg.chat.id, message); | 83 | bot.sendMessage(msg.chat.id, message); |
52 | return; | 84 | return; |
53 | } | 85 | } |
54 | 86 | ||
55 | var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; | 87 | var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix; |
56 | 88 | ||
57 | chat_ids[from] = msg.chat.id; | 89 | chat_ids[from] = msg.chat.id; |
90 | //setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from) | ||
58 | 91 | ||
59 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); | 92 | bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text); |
60 | 93 | ||
61 | var request_opts = { | 94 | var request_opts = { |
62 | url: config.globals.aaa, | 95 | url: config.globals.aaa, |
63 | qs: { | 96 | qs: { |
64 | PhoneNumber: from, | 97 | PhoneNumber: from, |
65 | Text: msg.text, | 98 | Text: msg.text, |
66 | Res_Port: config.globals.listen_port, | 99 | Res_Port: config.globals.listen_port, |
67 | SMSCID: config.globals.smscid | 100 | SMSCID: config.globals.smscid |
68 | } | 101 | } |
69 | }; | 102 | }; |
70 | 103 | ||
71 | request(request_opts, function(err, response, body) { | 104 | request(request_opts, function(err, response, body) { |
72 | if (err) { | 105 | if (err) { |
73 | console.log('Request error: ' + err); | 106 | logger.info('Request error: ' + err); |
74 | return; | 107 | return; |
75 | } | 108 | } |
76 | 109 | ||
77 | console.log('Response: ' + response); | 110 | logger.info('Response: ' + response); |
78 | console.log('Body: ' + body); | 111 | logger.info('Body: ' + body); |
79 | 112 | ||
80 | }); | 113 | }); |
81 | }); | 114 | }); |
82 | 115 | ||
83 | 116 |
package.json
1 | { | 1 | { |
2 | "name": "node-sate24-telegram", | 2 | "name": "node-sate24-telegram", |
3 | "version": "0.0.1", | 3 | "version": "0.0.1", |
4 | "description": "ST24 Telegram Center", | 4 | "description": "ST24 Telegram Center", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "echo \"Error: no test specified\" && exit 1" | 7 | "test": "echo \"Error: no test specified\" && exit 1" |
8 | }, | 8 | }, |
9 | "repository": { | 9 | "repository": { |
10 | "type": "git", | 10 | "type": "git", |
11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-center-telegram.git" | 11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-center-telegram.git" |
12 | }, | 12 | }, |
13 | "keywords": [ | 13 | "keywords": [ |
14 | "st24", | 14 | "st24", |
15 | "reload97", | 15 | "reload97", |
16 | "telegram" | 16 | "telegram" |
17 | ], | 17 | ], |
18 | "author": "husni", | 18 | "author": "husni", |
19 | "license": "ISC", | 19 | "license": "ISC", |
20 | "dependencies": { | 20 | "dependencies": { |
21 | "iniparser": "^1.0.5", | 21 | "iniparser": "^1.0.5", |
22 | "node-telegram-bot-api": "^0.12.1", | 22 | "node-telegram-bot-api": "^0.12.1", |
23 | "request": "^2.61.0" | 23 | "request": "^2.61.0", |
24 | "strftime": "^0.9.2", | ||
25 | "winston": "^1.0.1" | ||
24 | } | 26 | } |
25 | } | 27 | } |
26 | 28 |