2022-01-14 12:46:59 +01:00
|
|
|
const phin = require("phin");
|
|
|
|
|
2022-01-17 13:35:36 +01:00
|
|
|
const config = require("./config");
|
|
|
|
const log = require("./log");
|
2022-01-14 12:46:59 +01:00
|
|
|
|
2022-01-17 13:35:36 +01:00
|
|
|
async function proxy(sessionId, phone, input = "") {
|
|
|
|
const requestOptions = {
|
|
|
|
url: config.get("USSD_ENDPOINT"),
|
2022-01-14 12:46:59 +01:00
|
|
|
method: "POST",
|
|
|
|
parse: "string",
|
|
|
|
form: {
|
|
|
|
sessionId: sessionId,
|
|
|
|
phoneNumber: phone,
|
2022-01-17 13:35:36 +01:00
|
|
|
serviceCode: config.get("USSD_CODE"),
|
2022-01-14 12:46:59 +01:00
|
|
|
text: input,
|
|
|
|
},
|
2022-01-17 13:35:36 +01:00
|
|
|
};
|
|
|
|
log.debug(requestOptions, "request options");
|
2022-01-14 12:46:59 +01:00
|
|
|
|
2022-01-17 13:35:36 +01:00
|
|
|
try {
|
|
|
|
const { body } = await phin(requestOptions);
|
|
|
|
log.debug({ body: body }, "response body");
|
|
|
|
return body;
|
|
|
|
} catch (error) {
|
|
|
|
log.error(error);
|
|
|
|
return "ERR Something went wrong, try again later.";
|
|
|
|
}
|
2022-01-14 12:46:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { proxy };
|