简体   繁体   English

Twilio 和 Sendgrid - 传入短信

[英]Twilio and Sendgrid - Incoming SMS

Current we send one-way SMS via MS Flow and Twilio which works fine.目前我们通过 MS Flow 和 Twilio 发送单向短信,效果很好。 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.我一直在探索如何处理收到的短信,所以我按照指南并设法利用 Sendgrid 将收到的短信转发到我的电子邮件地址,这也可以。

However, I am looking to have the original sender receive the SMS via e-mail.但是,我希望原始发件人通过电子邮件接收 SMS。 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 ie我可以给每个员工他们自己的电话号码,这将定义每个人,但我需要一种 Twilio 或 Sendgrid 在发送回复电子邮件之前进行查找,以便它知道将其发送到哪里,即

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?我想我可以为每个数字重新创建相同的过程,但它需要大量的 Twilio 函数和变量,这似乎不是实现这一目标的好方法?

Sorry, I a not much of a coder and try to use on-line guides and forums where I can.对不起,我不是一个编码员,并尽量使用在线指南和论坛。

Many Thanks, JP非常感谢,JP

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).您可以尝试类似的操作,将Twilio 资产上的映射作为私有资产托管,但您也可以通过 HTTP 请求小部件将此信息拉入 Studio,如果您将其托管在外部服务器上(更高级一点)。 In my case I called my file mapping.json which has the format:在我的情况下,我调用了我的文件 mapping.json,其格式如下:

[
  {
  "name": "John Doe",
  "phone": "+14075551212",
  "email": "jdoe@example.com"
  },
  {
  "name": "Susan Doe",
  "phone": "+19545551212",
  "email": "sdoe@example.com"
  },
  {
  "name": "Nadia Doe",
  "phone": "+14705551212",
  "email": "ndoe@example.com"
  },
  {
  "name": "Carl Doe",
  "phone": "+18025551212",
  "email": "cdoe@example.com"
  }  
]

Then you would use the Run Function widget and send in 3 key:value pairs (Function Parameters):然后您将使用 Run Function 小部件并发送 3 个键:值对(函数参数):

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

Your Twilio Function would then consume these parameters and the contents of the private asset to handle the mapping.然后,您的 Twilio Function 将使用这些参数和私有资产的内容来处理映射。 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): 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.让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM