ussd-tg-proxy/src/util.js

35 lines
676 B
JavaScript

// this regex extracts ussd reply options
const regex = /\s(\d{1,2})\.\s/g;
// TODO: converts the text to a telegram keyboard
function createKeyboard(input) {
return Array.from(input.matchAll(regex), (m) => [m[1]]);
}
// parses ussd responses to code and text
function parseUssdResponse(input) {
return {
code: input.slice(0, 3),
text: input.slice(4),
keyboard: createKeyboard(input),
};
}
function isKenyanNumber(input) {
if (input.substr(0, 1) === "+") {
input = input.split("+")[1];
}
if (input.substr(0, 3) === "254") {
return true;
}
return false;
}
module.exports = {
createKeyboard,
parseUssdResponse,
isKenyanNumber,
};