summaryrefslogtreecommitdiff
path: root/src/app.js
blob: e363916442ac9b4f50c5a499ec899f3170e06c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const bodyParser = require('body-parser');
const express = require('express');
const { isSubscribed, getSubscriptions, getSubscriptionFromNumber, subscribe } = require('./db');
const { sendTextMessage } = require('./sendTextMessage');
const { MessagingResponse } = require('twilio').twiml;

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/sms', (req, res) => {
  /** @type {string} */
  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);
  } 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();
  res.type('text/xml').send(twiml.toString());
});

app.listen(3000, () => {
  console.log('Express server listening on port 3000');
});