ussd-tg-proxy/src/request.js

35 lines
792 B
JavaScript

const phin = require("phin");
const config = require("./config");
const log = require("./log");
async function proxy(sessionId, phone, input = "") {
const requestOptions = {
url: config.get("USSD_ENDPOINT"),
method: "POST",
parse: "string",
timeout: 3000,
form: {
sessionId: sessionId,
phoneNumber: phone,
serviceCode: config.get("USSD_CODE"),
text: input,
},
};
log.debug(requestOptions, "request options");
try {
const { body } = await phin(requestOptions);
log.debug({ body: body }, "response body");
if (body.length > 1) {
return body;
}
throw new Error("empty body");
} catch (error) {
log.error(error);
return "ERR Something went wrong, try again later.";
}
}
module.exports = { proxy };