Files
ussd-tg-proxy/src/request.js
Mohammed Sohail f61500f3e6 fix: add timeout to request
* incase the ussd-server is down or unresponsive, return a message to tg
2022-01-18 14:50:20 +03:00

32 lines
722 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");
return body;
} catch (error) {
log.error(error);
return "ERR Something went wrong, try again later.";
}
}
module.exports = { proxy };