diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.js | 10 | ||||
| -rw-r--r-- | src/sendTextMessage.js | 12 |
2 files changed, 14 insertions, 8 deletions
@@ -20,7 +20,7 @@ app.post('/sms', (req, res) => { console.debug(` Received a text from ${senderCountry} on number ${senderPhoneNumber}. Message content: ${body}. End of message - `.replace(/ +/g, ' ')); + `.replace(/\s+/g, ' ')); if (isSubscribed(senderPhoneNumber)) { const senderSubscription = getSubscriptionFromNumber(senderPhoneNumber); @@ -30,11 +30,11 @@ app.post('/sms', (req, res) => { const forwardedMessage = `${senderSubscription.name} said: ${body}`; sendTextMessage(otherSubscriptions, forwardedMessage); - const abbreviatedBody = body.length > 10 ? `${body.slice(0, 8)}...` : body; + const abbreviatedBody = body.length > 16 ? `${body.slice(0, 14)}...` : body; const confirmationMessage = ` Your message was forwarded to ${otherSubscriptions.length} recipients: "${abbreviatedBody}". Only you can see this message. - `.replace(/ +/g, ' '); + `.replace(/\s+/g, ' '); sendTextMessage([senderSubscription], confirmationMessage); } else { if (body.toLocaleLowerCase().startsWith('my name is ') || body.toLocaleLowerCase().startsWith('my name is: ')) { @@ -52,14 +52,14 @@ app.post('/sms', (req, res) => { 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. - `.replace(/ +/g, ' '); + `.replace(/\s+/g, ' '); sendTextMessage([subscription], welcomeMessage); } else { const twiml = new MessagingResponse(); twiml.message(` You are not subscribed to this texting group. To subscribe, send a text to this number with this syntax: my name is <your name> - `.replace(/ +/g, ' ')); + `.replace(/\s+/g, ' ')); res.type('text/xml').send(twiml.toString()); return; } diff --git a/src/sendTextMessage.js b/src/sendTextMessage.js index 03f8dd2..7653d0e 100644 --- a/src/sendTextMessage.js +++ b/src/sendTextMessage.js @@ -1,3 +1,7 @@ +const accountSid = process.env.TWILIO_ACCOUNT_SID; +const authToken = process.env.TWILIO_AUTH_TOKEN; +const client = require('twilio')(accountSid, authToken); + /** * @param {Array<{name: string, number: string}>} subscriptions * @param {string} message @@ -5,9 +9,11 @@ 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 - `); + client.messages.create({ + body: message, + from: '+447481347623', // my Twilio phone number I bought + to: subscription.number, + }).then(message => console.debug(`Sent message ${message.sid} to ${message.to}`)); } } |
