【问题标题】:Twilio Forward SMS to both email and another phoneTwilio 将 SMS 转发到电子邮件和另一部手机
【发布时间】:2019-11-10 02:19:27
【问题描述】:

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

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

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

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

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

在该功能中,我想先将短信转发到另一个电话号码,然后再将短信转发到电子邮件。

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 检索失败 尝试检索此 URL 的内容时失败。 11200 错误表明 Twilio 与您的服务之间的连接失败。

【问题讨论】:

    标签: sms twilio sendgrid


    【解决方案1】:

    试试这段代码,它是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);
        });
    };
    

    【讨论】:

    • 有效!谢谢你。现在我已经整理好了,我发现基于got 的示例对我有用,但使用https 的示例对我不起作用。示例我无法上班:stackoverflow.com/a/50728459/10783877
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多