core.js
3.53 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
"use strict";
const strftime = require("strftime");
const config = require("./config");
const logger = require("./logger");
const bot = require("./xmpp");
const archive = require("./message-archive");
const forwarder = require("./evo-forwarder");
var isReady = false;
function generateGenericRefId(command) {
return strftime("%Y%m%d") + '_' + command.code + '_' + command.destination;
}
function isTopupMessage(command) {
if (!command.code) {
return false;
}
if (command.code.search(/\d/) >= 0) {
return true;
}
else {
return false;
}
}
function messageParser(partner, msg) {
const command = {
user_id: partner,
password: null,
code: null,
destination: null,
ref_id: null,
raw: msg,
isTopupMessage: null
}
const token = msg.split('.');
command.code = token[0].toUpperCase();
command.isTopupMessage = isTopupMessage(command);
if (command.isTopupMessage) {
logger.verbose("Assumpting message is topup request");
command.destination = token[1];
command.password = token[2];
command.ref_id = generateGenericRefId(command);
if (token[3]) {
command.ref_id = command.ref_id + token[3];
}
}
logger.verbose("Message parsed", {cmd: command});
return command;
}
function sendImmediateReply(partner, msg) {
const replyMessage = "Pesan anda telah diterima dan akan segera diproses: " + msg;
bot.sendMessage(partner, replyMessage);
}
function sendNotReadyMessage(partner, msg) {
if (!config.not_ready_message_prefix) {
return;
}
const replyMessage = config.not_ready_message_prefix.trim() + ' ' + msg;
bot.sendMessage(partner, replyMessage);
}
function onOnline() {
const wait_secs = config.wait_after_online_secs || 60;
logger.info('Bot is online, waiting for ' + wait_secs + " seconds before ready to processing message");
setTimeout(
function() {
logger.info('Bot is ready to process message');
isReady = true;
},
wait_secs * 1000
)
}
function onMessage(partner, msg) {
if (!msg) {
return;
}
if (!isReady) {
logger.info('Bot is not ready yet to process message');
sendNotReadyMessage(partner, msg);
return;
}
if (!archive.isReady()) {
logger.info('Archive DB is not ready yet to process message');
sendNotReadyMessage(partner, msg);
return;
}
msg = msg.trim();
logger.verbose('Going to process message', {partner: partner, msg: msg});
sendImmediateReply(partner, msg);
const command = messageParser(partner, msg);
if (command.isTopupMessage) {
onTopupMessage(command);
} else {
forwarder.toEvoClassic(command);
}
}
function onTopupMessage(command) {
archive.find(command, function(err, data) {
if (err) {
logger.warn('Fail on duplicate check', {err: err, command: command});
return;
}
if (data) {
logger.verbose('Duplicate trx, forwarding to status check', {command: command})
if (config.check_status_on_dupe) {
forwarder.toEvoH2HCheckStatus(command);
}
else {
forwarder.toEvoH2HRequest(command);
}
}
else {
archive.insert(command, function(err) {
forwarder.toEvoH2HRequest(command);
});
}
});
}
exports.onMessage = onMessage;
exports.onOnline = onOnline;