From 81940d8409c6d0d55df695e1058f51c960255613 Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao <_accounts@nareshkumarrao.com> Date: Thu, 29 Jul 2021 15:59:11 +0200 Subject: [PATCH] implemented inform contacts --- src/app.js | 6 +-- src/db/db.js | 3 ++ src/routes/TelegramWebhookRoute.js | 82 ++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 src/routes/TelegramWebhookRoute.js diff --git a/src/app.js b/src/app.js index 2df4cfd..aacc8fc 100644 --- a/src/app.js +++ b/src/app.js @@ -7,6 +7,7 @@ const { LoginRoute } = require("./routes/LoginRoute"); const { CodeRoute } = require("./routes/CodeRoute"); const { VerifyRoute } = require("./routes/VerifyRoute"); const { corsOpts, sessionOpts } = require("./session"); +const { TelegramWebhookRoute } = require("./routes/TelegramWebhookRoute"); console.log(`Node Environment: ${process.env.NODE_ENV}`); @@ -16,10 +17,7 @@ app.use(session(sessionOpts)); app.use(cors(corsOpts)); app.use(express.json()); -app.post(`/${process.env.TELEGRAM_SECRET}`, (req, res) => { - res.send(); -}); - +app.post(`/${process.env.TELEGRAM_SECRET}`, TelegramWebhookRoute); app.post("/login", LoginRoute); app.get("/code", CodeRoute); app.post("/verify", VerifyRoute); diff --git a/src/db/db.js b/src/db/db.js index d0915c3..88f3469 100644 --- a/src/db/db.js +++ b/src/db/db.js @@ -60,6 +60,9 @@ const User = sequelize.define("User", { verification: { type: DataTypes.STRING, }, + isInfected: { + type: DataTypes.BOOLEAN, + }, }); Contact.sync(); diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js new file mode 100644 index 0000000..9c1e795 --- /dev/null +++ b/src/routes/TelegramWebhookRoute.js @@ -0,0 +1,82 @@ +const { Op } = require("sequelize"); +const { User, Contact } = require("../db/db"); +const { sendTelegramMessage } = require("../telegram"); + +function TelegramWebhookRoute(req, res) { + + try{ + const messageText = req.body.message.text; + const telegramID = req.body.message.from.id; + if (messageText.toLowerCase() == "/covidpositive") { + userInfected(telegramID, (result) => { + if(result.saved){ + sendTelegramMessage(telegramID, "Thanks for informing us. We will notify the people you were in contact with!", ()=>{}); + informContacts(telegramID, ()=>{}); + }else{ + sendTelegramMessage(telegramID, "Sorry, something went wrong.", ()=>{}); + } + }); + } + } + catch(e){ + console.log("Could not get Telegram Message"); + } + + + res.send(); +} + +function informContacts(telegramID, doneCallback){ + User.findOne({ + where: { + telegram: telegramID, + } + }).then(user => { + if(user){ + const userRowID = user.id; + Contact.findAll({ + where: { + [Op.or]: [{user: userRowID}, {with: userRowID}], + } + }) + .then(result => { + result.forEach(contact => { + const otherPersonID = contact.user == userRowID ? contact.with : contact.user; + User.findOne({ + where: { + id: otherPersonID, + } + }).then(otherPerson => { + sendTelegramMessage(otherPerson.telegram, "You're infected.", ()=>{}); + }); + }); + }); + } + }); + +} + +function userInfected(telegramID, doneCallback) { + User.findOne({ + where: { + telegram: telegramID, + }, + }).then((user) => { + if (!user) { + done({saved: false}); + } else { + user.isInfected = true; + user.save().then(result => { + if(result){ + + doneCallback({saved: true}); + } + }).catch(err=>{doneCallback({saved: false})}); + } + }) + .catch(err=>{ + doneCallback({saved: false}); + }); +} + +exports.TelegramWebhookRoute = TelegramWebhookRoute; \ No newline at end of file