diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.js | 22 | ||||
| -rw-r--r-- | src/db.js | 4 |
2 files changed, 23 insertions, 3 deletions
@@ -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 <your name>"); + 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()); }); @@ -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; } /** |
