Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
43b7800ed6
|
|||
|
30b60899d5
|
|||
|
99f9328afd
|
|||
|
1c7268e1a3
|
|||
|
0e1fbfeb1b
|
|||
|
4530c7105d
|
|||
|
aace2e98e1
|
|||
|
f61500f3e6
|
|||
|
05e441f6a1
|
|||
|
368f297c6b
|
|||
|
5257967f88
|
|||
|
1e438ae73e
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ussd-tg-proxy",
|
||||
"version": "0.2.1",
|
||||
"version": "0.3.1",
|
||||
"description": "ussd-tg-proxy",
|
||||
"main": "src/index.js",
|
||||
"repository": "https://git.grassecon.net/grassrootseconomics/ussd-tg-proxy.git",
|
||||
|
||||
12
src/bot.js
12
src/bot.js
@@ -77,19 +77,17 @@ bot.on("msg:contact", async (ctx) => {
|
||||
log.debug(ctx.update, "msg:contact received");
|
||||
const contact = ctx.msg.contact;
|
||||
|
||||
if (ctx.msg.reply_to_message.from.is_bot && ctx.from.id === contact.user_id) {
|
||||
if (contact.phone_number.slice(0, 4) !== "+254") {
|
||||
return ctx.reply("Sarafu is only available in Kenya at the moment.");
|
||||
}
|
||||
|
||||
await cache.set(contact.user_id, contact.phone_number.slice(1));
|
||||
if (
|
||||
ctx.from.id === contact.user_id &&
|
||||
util.isKenyanNumber(contact.phone_number)
|
||||
) {
|
||||
await cache.set(contact.user_id, contact.phone_number);
|
||||
|
||||
return ctx.reply(
|
||||
"Phone number successfully linked. /start the bot again to access your Sarafu account."
|
||||
);
|
||||
}
|
||||
|
||||
log.info(ctx.update.user, "contact spoof attempted");
|
||||
return ctx.reply("Could not verify sent contact.");
|
||||
});
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ async function proxy(sessionId, phone, input = "") {
|
||||
url: config.get("USSD_ENDPOINT"),
|
||||
method: "POST",
|
||||
parse: "string",
|
||||
timeout: 3000,
|
||||
form: {
|
||||
sessionId: sessionId,
|
||||
phoneNumber: phone,
|
||||
@@ -20,7 +21,10 @@ async function proxy(sessionId, phone, input = "") {
|
||||
try {
|
||||
const { body } = await phin(requestOptions);
|
||||
log.debug({ body: body }, "response body");
|
||||
return 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.";
|
||||
|
||||
15
src/util.js
15
src/util.js
@@ -1,5 +1,5 @@
|
||||
// this regex extracts ussd reply options
|
||||
const regex = /\s(\d{1,2}).\s/g;
|
||||
const regex = /\s(\d{1,2})\.\s/g;
|
||||
|
||||
// TODO: converts the text to a telegram keyboard
|
||||
function createKeyboard(input) {
|
||||
@@ -15,7 +15,20 @@ function parseUssdResponse(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,
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ const test = require("tap").test;
|
||||
const util = require("../src/util");
|
||||
|
||||
test("util", (t) => {
|
||||
t.plan(4);
|
||||
t.plan(8);
|
||||
|
||||
t.test("keyboard from single digit reply options", (t) => {
|
||||
const rawText = `CON Balance 80.0 GFT
|
||||
@@ -66,4 +66,32 @@ test("util", (t) => {
|
||||
t.same(output, expect);
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test("kenyan number formatted", (t) => {
|
||||
const number = "+254700123456";
|
||||
|
||||
t.equal(util.isKenyanNumber(number), true);
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test("kenyan number unformatted", (t) => {
|
||||
const number = "254700123456";
|
||||
|
||||
t.equal(util.isKenyanNumber(number), true);
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test("non kenyan number formatted", (t) => {
|
||||
const number = "+441632960198";
|
||||
|
||||
t.equal(util.isKenyanNumber(number), false);
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test("non kenyan number unformatted", (t) => {
|
||||
const number = "12029182132";
|
||||
|
||||
t.equal(util.isKenyanNumber(number), false);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user