Skip to content

Updated functions file with comments #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion masked-number/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 58 additions & 1 deletion masked-number/functions/relay-sms.protected.js
Original file line number Diff line number Diff line change
@@ -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 },
Expand Down
11 changes: 10 additions & 1 deletion masked-number/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Loading