summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2024-03-07 21:44:48 +0000
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2024-03-07 21:44:48 +0000
commit5c0d0697feea60601c9b8c4e624c526a451f872f (patch)
treedb8d4cf2c30d3f8f6da1f42e2d1e2746994befe8
parent9f44e87b092e9149c47466e4578f794fe4376524 (diff)
More stuff
-rw-r--r--src/app.js30
-rw-r--r--src/db.js9
-rw-r--r--src/sendTextMessage.js12
3 files changed, 49 insertions, 2 deletions
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