diff --git a/masked-number/.env b/masked-number/.env deleted file mode 100644 index fb964af7..00000000 --- a/masked-number/.env +++ /dev/null @@ -1,9 +0,0 @@ -# description: Messages sent to your Twilio number will get forwarded to this E.164-formatted phone number -# format: phone_number -# required: true -# link: https://www.twilio.com/docs/glossary/what-e164 -MY_PHONE_NUMBER=+12223334444 - -# description: The path to the webhook -# configurable: false -TWILIO_SMS_WEBHOOK_URL=/relay-sms diff --git a/masked-number/masked-number-demo/functions/relay-sms.protected.js b/masked-number/masked-number-demo/functions/relay-sms.protected.js index c3d9f801..38658c8b 100644 --- a/masked-number/masked-number-demo/functions/relay-sms.protected.js +++ b/masked-number/masked-number-demo/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 },