index.js
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var iniparser = require('iniparser');
var config = iniparser.parseSync('./config.ini');
var telegram = require('node-telegram-bot-api');
var request = require('request');
var http = require('http');
var url = require('url');
var strftime = require('strftime');
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: function() {
return strftime('%F %T', new Date());
},
formatter: function(options) {
// Return string will be passed to logger.
return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
}),
new (winston.transports.DailyRotateFile)({
filename: 'log',
timestamp: function() {
return strftime('%F %T', new Date());
},
formatter: function(options) {
// Return string will be passed to logger.
return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
})
]
});
var chat_ids = {};
var bot;
var options = {
webHook: {
port: config.globals.webhook_port,
key: __dirname+'/key.pem',
cert: __dirname+'/crt.pem'
}
};
bot = new telegram(config.globals.token, options);
bot.setWebHook(config.globals.webhook_prefix + config.globals.token, __dirname+'/crt.pem');
/*
var options = {
polling: true
};
bot = new telegram(config.globals.token, options);
*/
function deleteChatId(from) {
delete chat_ids[from];
}
function sendMessage(destination, message) {
//destination = destination.replace(/@TELEGRAM$/, '');
var chat_id = chat_ids[destination];
if (!chat_id) {
logger.warn('Can not find approriate chat id for ' + destination + '. Abort sending message.');
return;
}
logger.info('Sending reply to ' + destination + '(' + chat_id + '): ' + message);
bot.sendMessage(chat_id, message);
}
function createHttpResponseServer(){
var httpServer = http.createServer(function(request,response){
var qs = url.parse(request.url, true).query;
logger.info('Incoming request from SMSIN server:', {qs: qs})
//logger.info(qs);
response.end('OK');
sendMessage(qs.PhoneNumber, qs.text);
});
httpServer.listen(config.globals.listen_port, function(){
logger.info("listening on " + config.globals.listen_port);
})
}
createHttpResponseServer();
bot.getMe().then(function (me) {
logger.info('Hi my name is %s!', me.username);
});
bot.on('text', function (msg) {
logger.info('Incoming message', {msg: msg});
if (!msg.from.username) {
var replyMessage = "Pesan anda diabaikan, anda belum memiliki username pada telegram: " + msg.text;
logger.info(replyMessage, {msg: msg});
bot.sendMessage(msg.chat.id, replyMessage);
return;
}
var now = Math.floor(new Date().getTime()/1000);
if (now - msg.date > config.globals.message_max_age){
var message = 'Pesan "' + msg.text + '" diabaikan. Silahkan diulang kembali.';
logger.info(message)
bot.sendMessage(msg.chat.id, message);
return;
}
var from = msg.from.username.toUpperCase() + config.globals.msisdn_suffix;
chat_ids[from] = msg.chat.id;
//setTimeout(deleteChatId, 1000 * 3600 * 24 * 2, from)
bot.sendMessage( msg.chat.id,'Pesan anda telah diterima: ' + msg.text);
var request_opts = {
url: config.globals.aaa,
qs: {
PhoneNumber: from,
Text: msg.text,
Res_Port: config.globals.listen_port,
SMSCID: config.globals.smscid
}
};
request(request_opts, function(err, response, body) {
if (err) {
logger.info('Request error: ' + err);
return;
}
logger.info('Response: ' + response);
logger.info('Body: ' + body);
});
});
function dumpChatIds() {
var i = 0;
for (var key in chat_ids) {
logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key])
}
}
if (config.dump_chat_ids_interval) {
setInterval(dumpChatIds, config.dump_chat_ids_interval * 1000);
}