ussd-tg-proxy/src/request.js

31 lines
703 B
JavaScript
Raw Normal View History

2022-01-14 12:46:59 +01:00
const phin = require("phin");
const config = require("./config");
const log = require("./log");
2022-01-14 12:46:59 +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,
serviceCode: config.get("USSD_CODE"),
2022-01-14 12:46:59 +01:00
text: input,
},
};
log.debug(requestOptions, "request options");
2022-01-14 12:46:59 +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 };