From d8287337edf14e507bcab31b91607133aff20b50 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:56:54 +0000 Subject: More stuff --- src/app.js | 22 ++++++++++++++++++++-- src/db.js | 4 +++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/app.js b/src/app.js index 7c7ac83..e363916 100644 --- a/src/app.js +++ b/src/app.js @@ -1,6 +1,6 @@ const bodyParser = require('body-parser'); const express = require('express'); -const { isSubscribed, getSubscriptions, getSubscriptionFromNumber } = require('./db'); +const { isSubscribed, getSubscriptions, getSubscriptionFromNumber, subscribe } = require('./db'); const { sendTextMessage } = require('./sendTextMessage'); const { MessagingResponse } = require('twilio').twiml; @@ -35,10 +35,28 @@ app.post('/sms', (req, res) => { recipients: "${abbreviatedBody}". Only you can see this message. `; sendTextMessage([senderSubscription], confirmationMessage); + } else { + if (body.toLocaleLowerCase().startsWith('my name is ') || body.toLocaleLowerCase().startsWith('my name is: ')) { + const senderName = body.matchAll(/^my name is:? (.*)$/)?.next()?.[1]; + + if (!senderName) { + const twiml = new MessagingResponse(); + twiml.message("Please provide a name. Syntax: my name is "); + res.type('text/xml').send(twiml.toString()); + return; + } + + const subscription = subscribe(senderPhoneNumber, senderName); + const welcomeMessage = ` + Well done, ${senderName}! You have successfully subscribed to the Glen + Coe meet texting group. You will now be able to send and receive messages + to and from the group. + `; + sendTextMessage([subscription], welcomeMessage); + } } 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 b668495..ba37a30 100644 --- a/src/db.js +++ b/src/db.js @@ -9,7 +9,9 @@ const subscriptions = []; * @param {string} name */ function subscribe(number, name) { - subscriptions.push({ name, number }); + const subscription = { name, number }; + subscriptions.push(subscription); + return subscription; } /** -- cgit v1.2.3