Skip to content

shivani sms forwarding #560

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 3 commits 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
9 changes: 0 additions & 9 deletions masked-number/.env

This file was deleted.

133 changes: 133 additions & 0 deletions masked-number/masked-number-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Twilio Serverless
.twiliodeployinfo

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
1 change: 1 addition & 0 deletions masked-number/masked-number-demo/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
42 changes: 42 additions & 0 deletions masked-number/masked-number-demo/.twilioserverlessrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"commands": {},
"environments": {},
"projects": {},
// "assets": true /* Upload assets. Can be turned off with --no-assets */,
// "assetsFolder": null /* Specific folder name to be used for static assets */,
// "buildSid": null /* An existing Build SID to deploy to the new environment */,
// "createEnvironment": false /* Creates environment if it couldn't find it. */,
// "cwd": null /* Sets the directory of your existing Serverless project. Defaults to current directory */,
// "detailedLogs": false /* Toggles detailed request logging by showing request body and query params */,
// "edge": null /* Twilio API Region */,
// "env": null /* Path to .env file for environment variables that should be installed */,
// "environment": "dev" /* The environment name (domain suffix) you want to use for your deployment. Alternatively you can specify an environment SID starting with ZE. */,
// "extendedOutput": false /* Show an extended set of properties on the output */,
// "force": false /* Will run deployment in force mode. Can be dangerous. */,
// "forkProcess": true /* Disable forking function processes to emulate production environment */,
// "functionSid": null /* Specific Function SID to retrieve logs for */,
// "functions": true /* Upload functions. Can be turned off with --no-functions */,
// "functionsFolder": null /* Specific folder name to be used for static functions */,
// "inspect": null /* Enables Node.js debugging protocol */,
// "inspectBrk": null /* Enables Node.js debugging protocol, stops execution until debugger is attached */,
// "legacyMode": false /* Enables legacy mode, it will prefix your asset paths with /assets */,
// "live": true /* Always serve from the current functions (no caching) */,
// "loadLocalEnv": false /* Includes the local environment variables */,
// "loadSystemEnv": false /* Uses system environment variables as fallback for variables specified in your .env file. Needs to be used with --env explicitly specified. */,
// "logCacheSize": null /* Tailing the log endpoint will cache previously seen entries to avoid duplicates. The cache is topped at a maximum of 1000 by default. This option can change that. */,
// "logLevel": "info" /* Level of logging messages. */,
// "logs": true /* Toggles request logging */,
// "ngrok": null /* Uses ngrok to create a public url. Pass a string to set the subdomain (requires a paid-for ngrok account). */,
// "outputFormat": "" /* Output the results in a different format */,
// "overrideExistingProject": false /* Deploys Serverless project to existing service if a naming conflict has been found. */,
// "port": "3000" /* Override default port of 3000 */,
// "production": false /* Promote build to the production environment (no domain suffix). Overrides environment flag */,
// "properties": null /* Specify the output properties you want to see. Works best on single types */,
// "region": null /* Twilio API Region */,
"runtime": "node18" /* The version of Node.js to deploy the build to. (node18) */,
// "serviceName": null /* Overrides the name of the Serverless project. Default: the name field in your package.json */,
// "serviceSid": null /* SID of the Twilio Serverless Service to deploy to */,
// "sourceEnvironment": null /* SID or suffix of an existing environment you want to deploy from. */,
// "tail": false /* Continuously stream the logs */,
// "template": null /* undefined */,
}
105 changes: 105 additions & 0 deletions masked-number/masked-number-demo/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Get started with your Twilio Functions!</title>

<link rel="icon" href="https://twilio-labs.github.io/function-templates/static/v1/favicon.ico">
<link rel="stylesheet" href="https://twilio-labs.github.io/function-templates/static/v1/ce-paste-theme.css">

<script src="https://twilio-labs.github.io/function-templates/static/v1/ce-helpers.js" defer></script>
<script>
window.addEventListener('DOMContentLoaded', (_event) => {
inputPrependBaseURL();
});
</script>
</head>
<body>
<div class="page-top">
<header>
<div id="twilio-logo">
<a href="https://www.twilio.com/" target="_blank" rel="noopener">
<svg class="logo" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 60 60">
<title>Twilio Logo</title><path class="cls-1" d="M30,15A15,15,0,1,0,45,30,15,15,0,0,0,30,15Zm0,26A11,11,0,1,1,41,30,11,11,0,0,1,30,41Zm6.8-14.7a3.1,3.1,0,1,1-3.1-3.1A3.12,3.12,0,0,1,36.8,26.3Zm0,7.4a3.1,3.1,0,1,1-3.1-3.1A3.12,3.12,0,0,1,36.8,33.7Zm-7.4,0a3.1,3.1,0,1,1-3.1-3.1A3.12,3.12,0,0,1,29.4,33.7Zm0-7.4a3.1,3.1,0,1,1-3.1-3.1A3.12,3.12,0,0,1,29.4,26.3Z"/></svg>
</a>
</div>
<nav>
<span>Your Twilio application</span>
<aside>
<svg class="icon" role="img" aria-hidden="true" width="100%" height="100%" viewBox="0 0 20 20" aria-labelledby="NewIcon-1577"><path fill="currentColor" fill-rule="evenodd" d="M6.991 7.507c.003-.679 1.021-.675 1.019.004-.012 2.956 1.388 4.41 4.492 4.48.673.016.66 1.021-.013 1.019-2.898-.011-4.327 1.446-4.48 4.506-.033.658-1.01.639-1.018-.02-.03-3.027-1.382-4.49-4.481-4.486-.675 0-.682-1.009-.008-1.019 3.02-.042 4.478-1.452 4.49-4.484zm.505 2.757l-.115.242c-.459.9-1.166 1.558-2.115 1.976l.176.08c.973.465 1.664 1.211 2.083 2.22l.02.05.088-.192c.464-.973 1.173-1.685 2.123-2.124l.039-.018-.118-.05c-.963-.435-1.667-1.117-2.113-2.034l-.068-.15zm10.357-8.12c.174.17.194.434.058.625l-.058.068-1.954 1.905 1.954 1.908a.482.482 0 010 .694.512.512 0 01-.641.056l-.07-.056-1.954-1.908-1.954 1.908a.511.511 0 01-.71 0 .482.482 0 01-.058-.626l.058-.068 1.954-1.908-1.954-1.905a.482.482 0 010-.693.512.512 0 01.64-.057l.07.057 1.954 1.905 1.954-1.905a.511.511 0 01.71 0z"></path></svg>
Live
</aside>
</nav>
</header>
</div>
<main>
<div class="content">
<h1>
<img src="https://twilio-labs.github.io/function-templates/static/v1/success.svg" />
<div>
<p>Welcome!</p>
<p>Your live application with Twilio is ready to use!</p>
</div>
</h1>
<section>
<h2>Get started with your application</h2>
<p>
Follow these steps to try out your new app:
</p>
<p>
This app masks your phone number for SMS messages by relaying them through a
Twilio phone number.
</p>
<ol class="steps">
<li>
Text your Twilio phone number with a recipient phone number, a
<code>:</code>, and a message. For example, to send the message "hello" to
the number "+12223334444", text your Twilio phone number with:
<code>+12223334444: hello</code>.
</li>
<li>Your recipient should receive your message from your Twilio phone number.</li>
<li>
When that recipient sends a reply to your Twilio phone number, it will be
relayed to your personal phone number.
</li>
</ol>
</section>

<section>
<!-- APP_INFO_V2 -->
</section>
<section>
<h2>Troubleshooting</h2>
<ul>
<li>
Check the
<a href="https://www.twilio.com/console/phone-numbers/incoming"
target="_blank"
rel="noopener">
phone number configuration
</a>
and make sure the Twilio phone number you want for your app has a SMS webhook
configured to point at the following URL
<form>
<label for="twilio-webhook">Webhook URL</label>
<input type="text" id="twilio-webhook" class="function-root" readonly=true value="/relay-sms">
</form>
</li>
<li>
Ensure that <code>MY_PHONE_NUMBER</code> is set to the phone number you want
to relay messages to, in
<a href="https://www.twilio.com/docs/glossary/what-e164" target="_blank" rel="noopener">
E.164 format
</a>.
</li>
</ul>
</section>
</div>
</main>
<footer>
<span class="statement">We can't wait to see what you build.</span>
</footer>
</body>
</html>
95 changes: 95 additions & 0 deletions masked-number/masked-number-demo/functions/relay-sms.protected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 },
`${event.From}: ${event.Body}`
);
}

return callback(null, twiml);
};
Loading
Loading