Skip to content
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

Repo now out of date #478

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions forward-message-sendgrid/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

### WARNING !!!
### This Repo is now out of date.
dooley1001 marked this conversation as resolved.
Show resolved Hide resolved

With the release of Node v18, the Node.js ecosystem are migrating over from the old CommonJS (CJS) standard to the newer, ES Modules (ESM) standard. You can read about the differences in far more detail in this [Blog Post.](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1). The following snippets may causes errors.
dooley1001 marked this conversation as resolved.
Show resolved Hide resolved


# Forward SMS message as an email (SendGrid)

The SendGrid Function will forward incoming SMS messages to an email address using the [SendGrid API](https://sendgrid.com/).
Expand Down
56 changes: 56 additions & 0 deletions forward-message-sendgrid/functions/forward-mms-sendgrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const got = require('got');
const request = require('request-promise-native');

exports.handler = function(context, event, callback) {
imagePath = event.MediaUrl0;

//read in the image here:
request({
url: imagePath,
method: 'GET',
encoding: null
})
.then(result => {

let imageBuffer = Buffer.from(result);
let imageBase64 = imageBuffer.toString('base64');

//now create the email message
const msg = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New SMS message from: ${event.From}`,
dooley1001 marked this conversation as resolved.
Show resolved Hide resolved
content: [
{
type: 'text/plain',
value: event.Body
}
],
attachments: [
{
content: imageBase64,
filename: "owl.png",
type: "image/png",
disposition: "attachment",
content_id: "my_image"
}
]
};

//send mail
got.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(msg)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
callback(err);
});
});
};
dooley1001 marked this conversation as resolved.
Show resolved Hide resolved