Twilio 将短信转发到电子邮件和另一个 phone

Twilio Forward SMS to both email and another phone

我的出发点是关于使用 node.js、sendgrid 和 Twilio 函数将传入的 SMS 消息转发到电子邮件的答案:

我也看了这个教程: https://support.twilio.com/hc/en-us/articles/223134287-Forwarding-SMS-messages-to-another-phone-number

我可以将这两个操作合并到一个 Twilio 函数中吗?我更喜欢将它们组合成一个函数,因为我没有看到配置 Twilio 数字以执行两个单独的 "Messaging actions" 以响应传入消息的方法。我只能选择一个函数来 运行 .

我希望我只需要在上面给出的 中添加几行代码:

let twiml = new Twilio.twiml.MessagingResponse();
twiml.message(`${event.From}: ${event.Body}`, {
    to: '+13105555555'
});
callback(null, twiml);

在函数中,我想先转发短信到另一个phone号码,再转发到邮箱。

Twilio 函数环境变量已设置。

这是我的代码:

const https = require('https');

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

    let sms_twiml = new Twilio.sms_twiml.MessagingResponse();
        sms_twiml.message(`${event.From}: ${event.Body}`, {
        to: context.TO_SMS_NUMBER
    });
    //executing a callback terminates the function
    //callback(null, sms_twiml); 

    let postData = JSON.stringify({
        personalizations: [{
            to: [{
                email: context.TO_EMAIL_ADDRESS
            }]
        }],
        from: { 
            email: context.FROM_EMAIL_ADDRESS 
        },
        subject: `New SMS message from: ${event.From}`,
        content: [{
            type: 'text/plain',
            value: event.Body
        }]
    });

    let postOptions = {
        host: 'api.sendgrid.com',
        port: '443',
        path: '/v3/mail/send',
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ${context.SENDGRID_API_KEY}',
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData),
        }
    };

    let req = https.request(postOptions, function (res) {
        let email_twiml = new Twilio.email_twiml.MessagingResponse();
        callback(null, email_twiml);
    });

    req.write(postData);
    req.end();

};

我遇到错误 - 11200

HTTP retrieval failure There was a failure attempting to retrieve the contents of this URL. An 11200 error is an indicator of a connection failure between Twilio and your service.

试试这个代码,它是 this blog example 的额外一行。

(这一行:twiml.message({to: '+1555xxxxxxx'}, event.Body); // Forward SMS to this Number

const got = require('got');

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

  const requestBody = {
    personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
    from: { email: context.FROM_EMAIL_ADDRESS },
    subject: `New SMS message from: ${event.From}`,
    content: [
      {
        type: 'text/plain',
        value: event.Body
      }
    ]
  };

  got
    .post('https://api.sendgrid.com/v3/mail/send', {
      headers: {
        Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
        "Content-Type": 'application/json'
      },
      body: JSON.stringify(requestBody)
    })
    .then(response => {
      console.log(response);
      let twiml = new Twilio.twiml.MessagingResponse();
      twiml.message({to: '+1555xxxxxxx'}, event.Body); // Forward SMS to this Number
      callback(null, twiml);
    })
    .catch(err => {
      console.log(err);
      callback(err);
    });
};