diff --git a/src/routes/TelegramWebhookRoute.js b/src/routes/TelegramWebhookRoute.js index f1c6a76..e0c78d6 100644 --- a/src/routes/TelegramWebhookRoute.js +++ b/src/routes/TelegramWebhookRoute.js @@ -4,59 +4,66 @@ const { strings_en } = require("../strings"); const { sendTelegramMessage } = require("../telegram"); function TelegramWebhookRoute(req, res) { - - try{ - if(req.body.message.connected_website){ - sendTelegramMessage(req.body.message.from.id, "Thanks for using OurSejahtera! Let's stay safer together <3"); - }else{ - 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, strings_en.telegram_inform_positive); - informContacts(telegramID); - }else{ - sendTelegramMessage(telegramID, "Sorry, something went wrong."); - } - }); - } - } - }catch(e){ - console.log("Could not get Telegram Message"); + try { + if (req.body.message.connected_website) { + sendTelegramMessage( + req.body.message.from.id, + "Thanks for using OurSejahtera! Let's stay safer together <3" + ); + } else { + 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, + strings_en.telegram_inform_positive + ); + 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({ +function informContacts(telegramID, doneCallback = () => {}) { + User.findOne({ + where: { + telegram: telegramID, + }, + }).then((user) => { + if (user) { + const userRowID = user.id; + Contact.findAll({ 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, strings_en.telegram_inform_infect); - }); - }); - }); - } - }); - + [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, + strings_en.telegram_inform_infect + ); + }); + }); + }); + } + }); } function userInfected(telegramID, doneCallback) { diff --git a/src/strings.js b/src/strings.js index 22c95de..210f529 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,16 +1,19 @@ const strings_en = { - telegram_inform_infect: "ATTENTION! Someone you have been \ + telegram_inform_infect: + "ATTENTION! Someone you have been \ in contact with has reported being tested POSITIVE with \ COVID19. Please maintain a self-quarantine until getting \ tested, and follow all local guidelines.", - telegram_inform_positive: "Thanks for informing us. We will \ + telegram_inform_positive: + "Thanks for informing us. We will \ notify the people you were in contact with! Please follow all \ local COVID19 guidelines.", - telegram_qr_scanned: "Someone scanned your QR code. You will \ + telegram_qr_scanned: + "Someone scanned your QR code. You will \ be notified if they report being tested positive with COVID19. If \ you are tested positive, please tell me /COVIDPOSITIVE", }; -exports.strings_en = strings_en; \ No newline at end of file +exports.strings_en = strings_en; diff --git a/src/telegram.js b/src/telegram.js index 537ada8..4fb241f 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -16,7 +16,7 @@ function setTelegramWebHook(done) { }); } -function sendTelegramMessage(telegramID, message, done=()=>{}) { +function sendTelegramMessage(telegramID, message, done = () => {}) { const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`; axios .post(url, {