index.js
3.58 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
var telegram = require('node-telegram-bot-api');
var logger = require('./logger.js').start();
var http = require('http');
var evo = require('./evo-im.js');
var config = require('./config.json');
var chat_ids = {};
var botUsername = 'UNKNOWN';
var options = {};
if (!Number(config.polling)) {
options.webHook = {
port: config.webhook_port,
key: __dirname+'/key.pem',
cert: __dirname+'/crt.pem'
}
} else {
options.polling = true;
}
var bot = new telegram(config.token, options);
if (!Number(config.polling)) {
bot.setWebHook(config.webhook_prefix + config.token, {certificate: __dirname+'/crt.pem'});
}
function sendMessage(destination, message, retry) {
if (retry === null || retry === undefined) {
retry = 10;
}
var chat_id = getChatId(destination);
if (!chat_id) {
logger.warn('Can not find approriate chat id for ' + destination + '. Abort sending message.');
return;
}
logger.info('Sending reply from ' + botUsername + ' to ' + destination + '(' + chat_id + '): ' + message);
try {
bot.sendMessage(chat_id, message);
}
catch(e) {
logger.warn('Exception on sendMessage ' + e, {destination: destination, message: message, retry: retry, error: e});
if (retry) {
setTimeout(sendMessage, 10000, destination, message, --retry);
}
}
}
evo.start({
config: config,
logger: logger,
sendMessage: sendMessage
});
function deleteChatId(from) {
delete chat_ids[from];
}
function updateChatId(from, chat_id) {
chat_ids[from.toLowerCase()] = chat_id;
logger.verbose('Chat id ' + chat_id + ' ' + from);
}
function getChatId(partner) {
try {
return chat_ids[partner.toLowerCase()];
}
catch(e) {
return;
}
}
function hasIgnoreKeywords(message) {
if (message.trim().indexOf('/') == 0) {
return true;
}
return false;
}
bot.getMe().then(function (me) {
logger.info('Hi my name is %s!', me.username);
botUsername = me.username;
});
bot.on('text', function (msg) {
logger.info('Incoming message for ' + botUsername + ' (' + msg.chat.id + '): ' + msg.text, {message: 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 from = msg.from.username.toUpperCase() + config.msisdn_suffix;
updateChatId(from, msg.chat.id);
var now = Math.floor(new Date().getTime()/1000);
var age = now - msg.date;
if (now - msg.date > config.message_max_age){
var message = "Pesan anda diabaikan, silahkan diulang beberapa saat lagi jika diperlukan: " + msg.text;
logger.info(message, {msg: msg, age: age, max_age: config.message_max_age});
sendMessage(from, message);
return;
}
if (hasIgnoreKeywords(msg.text)) {
logger.info("Ignoring message");
return;
}
var greeting_prefix = "Pesan anda telah diterima:";
if (config.greeting_prefix) {
greeting_prefix = config.greeting_prefix;
}
bot.sendMessage(msg.chat.id, greeting_prefix + ' ' + msg.text);
setTimeout(
evo.onMessage,
config.delay_on_message,
from, msg.text, msg.date * 1000
);
});
function dumpChatIds() {
var i = 0;
for (var key in chat_ids) {
logger.verbose('DUMPED CHAT IDS #' + ++i + ' ' + key + ': ' + chat_ids[key])
}
logger.verbose('TOTAL ' + i + ' chat ids');
}
if (config.dump_chat_ids_interval) {
setInterval(dumpChatIds, config.dump_chat_ids_interval);
}