This repository has been archived on 2022-03-31. You can view files and clone it, but cannot push or open issues or pull requests.
cic-ussd-e2e/lib.js

42 lines
898 B
JavaScript

const crypto = require("crypto");
const phin = require("phin");
async function ussdClient(sessionId, input = "") {
const requestOptions = {
url: "https://ussd.grassecon.net",
method: "POST",
parse: "string",
timeout: 3000,
form: {
sessionId: sessionId,
// Get from a config file, then pass as a param
phoneNumber: "254711777734",
serviceCode: "*483*061#",
text: input,
},
};
try {
const { body } = await phin(requestOptions);
if (body.length > 1) {
return {
code: body.slice(0, 3),
text: body.slice(4),
};
}
throw new Error("EMPTY_BODY");
} catch (error) {
console.log(error);
}
}
function newSession() {
return crypto.randomBytes(16).toString("hex");
}
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
module.exports = { ussdClient, newSession, wait };