ussd-tg-proxy/src/util.js

35 lines
676 B
JavaScript
Raw Normal View History

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