bdm1981 / emailtosms-gateway Goto Github PK
View Code? Open in Web Editor NEWTwilio Email to SMS Gateway
License: MIT License
Twilio Email to SMS Gateway
License: MIT License
When an email is sent with a recipient name, it shows as the following:
Jason <[email protected]>
The current regex code in index.js does not correctly extract the phone number and hence sending the SMS fails.
When I replace the regex code which extracts the phone number with the following seems to do the trick:
/\d+(?=@)/g
I am very much a beginner at this stuff, but so far it is working in my tests.
For convenience, I have copied the NEW index.js below
Thanks!
const parser = require("lambda-multipart-parser");
const client = require("twilio")(
process.env.ACCOUNT_SID,
process.env.AUTH_TOKEN
);
/**
*
* @param {String} from - FROM parameter passed from SendGrid
* @returns {String} Containing email address
*/
const extractEmails = from => {
return from.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
};
/**
*
* @param {String} from - FROM parameter passed from SendGrid
* @returns {Boolean}
*/
const validateSender = from => {
let emails = extractEmails(from);
let isValid = false;
let domains = process.env.DOMAINS.split(",");
domains.forEach(domain => {
if (emails[0].indexOf(domain) >= 0) {
isValid = true;
}
});
return isValid;
};
exports.handler = async event => {
let data = await parser.parse(event);
console.log("input data: ", data);
if (validateSender(data.from)) {
// parse the to number from the left-hand side of the email address
let regexp = /\d+(?=@)/g;
let to = data.to.match(regexp)[0];
let body = `Subject: ${data.subject}\nMessage: ${data.text}`;
let message;
try {
message = await client.messages.create({
body: body,
from: process.env.FROM,
to: to
});
} catch(err) {
console.log(err)
}
console.log("Message Output: ", message);
const response = {
statusCode: 200,
body: JSON.stringify({ input: data, output: message })
};
return response;
} else {
const response = {
statusCode: 403,
body: "Unauthorized Sender"
};
return response;
}
};
Dear Sir,
I am trying to test this code but am getting the following error below. Can you assist?
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'content-type' of undefined",
"trace": [
"TypeError: Cannot read property 'content-type' of undefined",
" at /var/task/node_modules/lambda-multipart-parser/index.js:26:42",
" at new Promise ()",
" at Object.parse (/var/task/node_modules/lambda-multipart-parser/index.js:23:26)",
" at Runtime.exports.handler (/var/task/index.js:35:27)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
There is a way to send to multiple numbers at same time?
If I try to send for instance number@gateway;number2@gateway;number...n
NUMBER1 receives the message repeated the same amount of destinations and the others does not receive nothing.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.