From 5c0d0697feea60601c9b8c4e624c526a451f872f Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:44:48 +0000 Subject: More stuff --- src/app.js | 30 +++++++++++++++++++++++++++++- src/db.js | 9 ++++++++- src/sendTextMessage.js | 12 ++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/sendTextMessage.js (limited to 'src') diff --git a/src/app.js b/src/app.js index 9067aa6..7c7ac83 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,7 @@ const bodyParser = require('body-parser'); const express = require('express'); +const { isSubscribed, getSubscriptions, getSubscriptionFromNumber } = require('./db'); +const { sendTextMessage } = require('./sendTextMessage'); const { MessagingResponse } = require('twilio').twiml; const app = express(); @@ -8,7 +10,33 @@ app.use(bodyParser.urlencoded({ extended: false })); app.post('/sms', (req, res) => { /** @type {string} */ - const body = req.body; + const body = req.body.Body.trim(); + /** @type {string} */ + const senderPhoneNumber = req.body.From; + /** @type {string} */ + const senderCountry = req.body.FromCountry; + + console.debug(` + Received a text from ${senderCountry} on number ${senderPhoneNumber}. + Message content: ${body}. End of message + `); + + if (isSubscribed(senderPhoneNumber)) { + const senderSubscription = getSubscriptionFromNumber(senderPhoneNumber); + // TODO: enable filter. Just disabling so I can get the forwarded text to my own phone to check it works. + const otherSubscriptions = getSubscriptions()/* .filter(sub => sub.number != senderPhoneNumber) */; + + const forwardedMessage = `${senderSubscription.name} said: ${body}`; + sendTextMessage(otherSubscriptions, forwardedMessage); + + const abbreviatedBody = body.length() > 10 ? `${body.slice(0, 8)}...` : body; + const confirmationMessage = ` + Your message was forwarded to ${otherSubscriptions.length()} + recipients: "${abbreviatedBody}". Only you can see this message. + `; + sendTextMessage([senderSubscription], confirmationMessage); + } + const twiml = new MessagingResponse(); twiml.message(JSON.stringify(body)); res.type('text/xml').send(twiml.toString()); diff --git a/src/db.js b/src/db.js index 145b144..95a9917 100644 --- a/src/db.js +++ b/src/db.js @@ -16,7 +16,7 @@ export function subscribe(number, name) { * @param {string} number */ export function isSubscribed(number) { - return subscriptions.some(s => s.number == number); + return subscriptions.some(s => s.number === number); } export function getSubscriptions() { @@ -26,4 +26,11 @@ export function getSubscriptions() { clone.push({ ...subscription }); } return clone; +} + +/** + * @param {string} number + */ +export function getSubscriptionFromNumber(number) { + return subscriptions.find(sub => sub.number === number); } \ No newline at end of file diff --git a/src/sendTextMessage.js b/src/sendTextMessage.js new file mode 100644 index 0000000..b53a8a3 --- /dev/null +++ b/src/sendTextMessage.js @@ -0,0 +1,12 @@ +/** + * @param {Array<{name: string, number: string}>} subscriptions + * @param {string} message + */ +export function sendTextMessage(subscriptions, message) { + for (const subscription of subscriptions) { + const recipientNumber = subscription.number; + console.warn(` + TODO: send text message to ${recipientNumber}. Body: ${message}. End of message + `); + } +} \ No newline at end of file -- cgit v1.2.3