evo-forwarder.js
3.65 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
"use strict";
const request = require("request");
const strftime = require('strftime');
const xmlparser = require('xml2js').parseString;
const config = require("./config");
const logger = require("./logger");
const bot = require("./xmpp");
const archive = require("./message-archive");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
function toEvo(opts, cb) {
logger.verbose('Requesting to EVO', {opts});
request(opts, function(err, response, body) {
if (err) {
logger.warn('Error forwarding to evo', {err: err});
if (cb) { cb(err); }
return;
}
if (response.statusCode != 200) {
logger.warn('Error forwarding to evo', {http_status: response.statusCode, body: body});
if (cb) { cb('ER_HTTP_STATUS_IS_NOT_200', response, body); }
return;
}
if (config.debug_evo_direct_response) {
logger.verbose('Got EVO direct response', {http_status: response.statusCode, body: body});
}
if (cb) { cb(null, response, body); }
})
}
function toEvoClassic(command) {
const opts = {
url: config.evo_classic_url,
qs: {
msg: command.raw,
msisdn: command.user_id,
smsc: config.smsc,
ts: strftime('%F %T'),
}
};
toEvo(opts);
}
function toEvoH2H(command, url, cb) {
const opts = {
url: url,
qs: {
code: command.code,
msisdn: command.destination,
user_id: command.user_id,
password: command.password,
ref_id: command.ref_id,
smsc: config.smsc
}
};
toEvo(opts, function(err, response, body) {
if (err || response.statusCode != 200) {
if (cb) { cb(err || 'ER_HTTP_STATUS_IS_NOT_200'); }
return;
}
xmlparser(body, function(xmlParserError, data) {
if (xmlParserError) {
logger.warn('XML Parser error', {err: xmlParserError, body: body});
if (cb) { cb('ER_XML_PARSER_ERROR'); }
return;
}
const request_status = getDirectAckFieldValue(data, "request_status");
const info = getDirectAckFieldValue(data, "info");
if (config.forward_direct_reply_on_error && request_status && info && (request_status != 'OK')) {
logger.warn('Evo direct response request status is not "OK"', {direct_response: data})
bot.sendMessage(command.user_id, request_status + ': ' + info)
}
if (cb) { cb(null, data); }
})
});
}
function getDirectAckFieldValue(response, field) {
try {
return response.direct_ack[field][0];
}
catch(e) {
return;
}
}
function toEvoH2HRequest(command) {
toEvoH2H(command, config.evo_h2h_request_url, function(err, data) {
if (err) {
return;
}
if (getDirectAckFieldValue(data, "code") == '01') {
toEvoH2HCheckStatus(command);
}
});
}
function toEvoH2HCheckStatus(command) {
toEvoH2H(command, config.evo_h2h_status_url, function(err, data) {
if (err) {
return;
}
const info = getDirectAckFieldValue(data, "info");
if (info) {
bot.sendMessage(command.user_id, info);
}
if (getDirectAckFieldValue(data, "topup_status") == "R") {
logger.verbose('Got R status, deleting trx from archive');
archive.remove(command.user_id, command.ref_id);
}
});
}
exports.toEvoClassic = toEvoClassic;
exports.toEvoH2HRequest = toEvoH2HRequest;
exports.toEvoH2HCheckStatus = toEvoH2HCheckStatus;