From fa09c2c2c762463764a4312640e81439ce71350a Mon Sep 17 00:00:00 2001 From: sbrij001 Date: Thu, 5 Sep 2024 15:43:06 -0400 Subject: [PATCH] updated functions file with comments so it is more readable --- masked-number/.env | 2 +- .../functions/relay-sms.protected.js | 59 ++++++++++++++++++- masked-number/package.json | 11 +++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/masked-number/.env b/masked-number/.env index fb964af7..aed0a93b 100644 --- a/masked-number/.env +++ b/masked-number/.env @@ -2,7 +2,7 @@ # format: phone_number # required: true # link: https://www.twilio.com/docs/glossary/what-e164 -MY_PHONE_NUMBER=+12223334444 +MY_PHONE_NUMBER=+13525739399 # description: The path to the webhook # configurable: false diff --git a/masked-number/functions/relay-sms.protected.js b/masked-number/functions/relay-sms.protected.js index c3d9f801..38658c8b 100644 --- a/masked-number/functions/relay-sms.protected.js +++ b/masked-number/functions/relay-sms.protected.js @@ -1,32 +1,89 @@ +/* + * Masking your phone number for SMS Forwarding and Responding + * + * Description: + * This function acts as a message-forwarding service + * If the message comes from the user, it forwards the message to another number. + * If it comes from someone else, it forwards it to the user. + * + * Contents: + * 1. Initialize Client and Response + * 2. Sender Check + * 3. Extracting Recipient and Message + * 4. Sending the Message + * 5. Handling Messages from Other Senders + */ + +/* + * 1. Initialize client and response + * + * Here you can initialize a Twilio client (client) to interact with Twilio's API. + * + * A twiml object is created to generate TwiML (Twilio Markup Language) responses, + * which are instructions that tell Twilio how to respond to an incoming message. + */ + +// Defines the main function handler for the Twilio Function. exports.handler = async function (context, event, callback) { const client = context.getTwilioClient(); const twiml = new Twilio.twiml.MessagingResponse(); +/* + * 2. Checking the Sender + * + * Here you can checks if the message came from a specific number. + * This condition ensures that only the user with this phone number can send outgoing messages. + */ if (event.From === context.MY_PHONE_NUMBER) { + // Looks for a : in the message body to separate the recipient's phone number from the message text. const separatorPosition = event.Body.indexOf(':'); + // If there is no :, a response is sent to the sender with a message explaining the required format: recipientPhoneNumber: message. if (separatorPosition < 1) { twiml.message( 'You need to specify a recipient number and a ":" before the message. For example, "+12223334444: message".' ); + + /* + * 3. Extracting Recipient and Message + * + * Here you can processes the incoming message body + * to extract the recipient's phone number and the actual message content + */ + } else { + // Extracts the recipient's phone number from the part of the message before the : and removes any extra whitespace. const recipientNumber = event.Body.substr(0, separatorPosition).trim(); + // Extracts the actual message from the part of the message after the ':' const messageBody = event.Body.substr(separatorPosition + 1).trim(); + /* + * 4. Sending the Message + * + * Here a try-catch block attempts to send an SMS message using the Twilio API + */ try { + // Sends the SMS to recipientNumber with the message messageBody. await client.messages.create({ to: recipientNumber, from: event.To, body: messageBody, }); - + // If successful, callback(null) is called to signal that the function executed successfully without returning any TwiML. return callback(null); + // If there is an error (e.g., an invalid phone number), a failure message is sent to the sender explaining that the phone number might be incorrect. } catch (err) { twiml.message( 'There was an issue with the phone number you entered; please verify it is correct and try again.' ); } } + /* + * 5. Handling Messages from Other Senders + * + * If the message is not from context.MY_PHONE_NUMBER, + * the function forwards it to the number stored in context.MY_PHONE_NUMBER, + */ } else { twiml.message( { to: context.MY_PHONE_NUMBER }, diff --git a/masked-number/package.json b/masked-number/package.json index 249bce8d..687bc06a 100644 --- a/masked-number/package.json +++ b/masked-number/package.json @@ -2,5 +2,14 @@ "name": "masked-number", "version": "1.0.0", "private": true, - "dependencies": {} + "description": "Uses a Twilio phone number to relay SMS messages to and from your phone; since the other party only sees your Twilio number, this effectively allows you to mask your phone number for privacy purposes.", + "main": "index.js", + "directories": { + "test": "tests" + }, + "scripts": { + "test": "jest" + }, + "author": "", + "license": "ISC" }