0
votes

Current we send one-way SMS via MS Flow and Twilio which works fine. I have been exploring how to handle incoming SMS, so I followed a guide and managed to utilise Sendgrid to forward incoming SMS to my e-mail address which also works.

However, I am looking to have the original sender receive the SMS via e-mail. I can give each staff member their own phone number which would define each individual but I need a way of Twilio or Sendgrid doing a lookup prior to sending the reply e-mail so it knows where to send it i.e.

User 1 = 01234455678, 
User 2 = 01234543245,
User 3 = 06546546445,...etc.

I guess I could re-create the same process for each number but it would require lots of Twilio Functions and Variables which doesn't seem like a great way to accomplish this?

Sorry, I a not much of a coder and try to use on-line guides and forums where I can.

Many Thanks, JP

1

1 Answers

0
votes

You can try something like this, hosting the mapping on Twilio Assets as a Private asset but you could also pull this information into Studio via the HTTP Request Widget if you hosted it on an external server (a bit more advanced). In my case I called my file mapping.json which has the format:

[
  {
  "name": "John Doe",
  "phone": "+14075551212",
  "email": "[email protected]"
  },
  {
  "name": "Susan Doe",
  "phone": "+19545551212",
  "email": "[email protected]"
  },
  {
  "name": "Nadia Doe",
  "phone": "+14705551212",
  "email": "[email protected]"
  },
  {
  "name": "Carl Doe",
  "phone": "+18025551212",
  "email": "[email protected]"
  }  
]

Then you would use the Run Function widget and send in 3 key:value pairs (Function Parameters):

From - {{trigger.message.From}} To - {{trigger.message.To}} Body - {{trigger.message.Body}}

Your Twilio Function would then consume these parameters and the contents of the private asset to handle the mapping. Make sure to configure your Twilio Functions environment with the Sendgrid NPM package, @sendgrid/mail version 7.0.1 and you configure the two Sendgrid specific environmental variables below with their respective values (accessed via the context object in the JavaScript):

SENDGRID_API_KEY

FROM_EMAIL_ADDRESS

const fs = require('fs');
const sgMail = require('@sendgrid/mail');

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

    let from = event.From;
    let to = event.To;
    let body = event.Body;

    let fileName = 'mapping.json';
    let file = Runtime.getAssets()[fileName].path;
    let text = fs.readFileSync(file);
    let mappings  = JSON.parse(text);

    // Filter array to match to number
    let result = mappings.filter(record => record.phone === to);

    if (result.length) {

    sgMail.setApiKey(context.SENDGRID_API_KEY);
        // Define message params
        const msg = {
          to: result[0].email,
          from: context.FROM_EMAIL_ADDRESS,
          text: body,
          subject: `New SMS from: ${from}`,
        };
        // Send message
        sgMail.send(msg)
        .then(response => {
            console.log("Success.");
            callback();
        })
        .catch(err => {
            console.log("Not Success.");
            callback(err);
        });
    } else {
    console.log("** NO MATCH **");
    callback();
    }
}; 

Let me know how it goes.